Stacks and Queues
Stacks and Queues
Topics to be covered : What are stacks and queues? Terminology How are they implemented? Example uses of stacks and queues
Stacks
A stack is a list in which all insertions and deletions are made at one end, called the top. The last element to be inserted into the stack will be the first to be removed. Thus stacks are sometimes referred to as Last In First Out (LIFO) lists. top
Stack Interface
The following operations can be applied to a stack: InitStack(Stack): Push (Item): Pop(Stack): Top(Stack): isEmpty(Stack): creates an empty stack pushes an item on the stack removes the first item from the stack returns the first item from the stack w/o removing it returns true is the stack is empty
Push
5 4 3 2 1
Push(41)
21 12 5
5 4 3 2 1
41 21 12 5
Pop
5 4 3 2 1
41 21 12 5
x = Pop()
5 4 3 2 1
21 12 5
Problem
The previous solution works on a fixed array. What if we want to have multiple stacks in a program? Copy code?
int StackArray2[50];// a second stack int top2=-1; // index of the top element of the stack void Push2(int elem) { top2++; StackArray[top2] = elem; } int Pop(){ }
Bad idea!
Definition: - An Abstract Data Type is some sort of data together with a set of functions (interface) that operate on the data. - Access is only allowed through that interface. - Implementation details are hidden from the user.
The Stack-ADT
#define STACKSIZE 50 struct Stack { int item[STACKSIZE]; int top; }; void void int int bool InitStack(Stack &st); Push(Stack &st, int elem); Pop (Stack &st); Top (Stack st); isEmpty(Stack st);
Stack specification
// push 13 onto the first stack // push 32 onto the second stack
int i = Pop(st2); // now popping st2 into i int j = Top(st1); // returns the top of st1 to j // without removing element };
Data Structures and Algorithms 10
Application of Stacks
e.g. Evaluation of arithmetic expressions: Usually, arithmetic expressions are written in infix notation, e.g. A+B*C An expression can as well be written in postfix notation (also called reverse polish notation): A+B becomes AB+ A*C becomes AC* A+B*C becomes ABC*+ (A+B)*C becomes AB+C*
Data Structures and Algorithms 11
Evaluating expressions
Given an expression in postfix notation. Using a stack they can be evaluated as follows: - Scan the expression from left to right - When a value (operand) is encountered, push it on the stack - When an operator is encountered, the first and second element from the stack are popped and the operator is applied - The result is pushed on the stack
Data Structures and Algorithms 12
Stack
13
- Simply push each letter onto the stack, then pop them back off again and hey presto!
Data Structures and Algorithms 14
STACKS
SKCATS
15
Queues
Definition: A Queue is an ordered collection of items from which items may be deleted at one end (called the front of the queue) and into which items may be inserted at the other end (the rear of the queue).
16
Queue Interface
The following operations can be applied to a queue: InitQueue(Queue): Join (Item): Leave(Queue): isEmpty(Queue): creates an empty queue inserts an item to the rear of the queue removes an item from the front of the queue returns true is the queue is empty
17
Queues (FIFO-lists)
21 55 7 12 Rear Join(36); Front
36 21 55 7 12 Rear Front
Elements can only be added to the rear of the queue and removed from the front of the queue.
18
Queues (contd.)
36 21 55 7 12 Rear Leave(); Front
36 21 55 7 Rear Front
19
Implementation of Queues
Removing an element from the queue is an expensive operation because all remaining elements have to be moved by one position. A more efficient implementation is obtained if we consider the array as being circular:
rear [2] e3 [1] e2 e1 [0] front
Data Structures and Algorithms 20
[n-2] [n-1]
21
Application of Queues
In a multitasking operating system, the CPU time is shared between multiple processes. At a given time, only one process is running, all the others are sleeping. The CPU time is administered by the scheduler. The scheduler keeps all current processes in a queue with the active process at the front of the queue.
next process
Process Queue
A
running process
22
Round-Robin Scheduling
Every process is granted a specific amount of CPU time, its quantum. If the process is still running after its quantum run out, it is suspended and put towards the end of the queue.
next process
D
Process Queue
A
running process
23
The Queue-ADT
#define QSIZE 50 struct { int int int }; void void int bool Queue items[QSIZE]; rear; front;
InitQueue(Queue &q); Join(Queue &q, int elem); Leave(Queue &q); isEmpty(Queue q); queue.h
24