Stacks and Queues 2014, Fall Pusan National University Ki-Joune Li
STEMPNU 2 Stack A Container Last-In First-Out (LIFO) Access only to the element at the top Push : Insert an element at the top Pop : Remove an element from the top Example Function Invocation Previous frame pointer (registers, local variables) Return Address, Parameters Bottom top
STEMPNU 3 Operations and Implementation Operations Maintenance : creation of a new stack, deletion of stack Push and Pop IsEmpty and IsFull Data Structures Class Stack { private: inttop,MaxSize; Type*stack;// public: Stack(int size); Boolean isFull(), isEmpty(); Typepop(); voidpush(Type element); }; Class Stack { private: inttop,MaxSize; Type*stack;// public: Stack(int size); Boolean isFull(), isEmpty(); Typepop(); voidpush(Type element); }; Stack::Stack(int size){ MaxSize=size; stack=new Type[MaxSize]; top=-1; } Stack::Stack(int size){ MaxSize=size; stack=new Type[MaxSize]; top=-1; } Boolean Stack::isFull() { if(top==MaxSize-1) return YES; else return NO; } Boolean Stack::isFull() { if(top==MaxSize-1) return YES; else return NO; } Type Stack::pop() { if(isEmpty()==YES) stackEmpty(); else return stack[top--]; } Type Stack::pop() { if(isEmpty()==YES) stackEmpty(); else return stack[top--]; } void Stack::push(Type v) { if(isFull()==YES) stackFull(); else stack[++top]=v; } void Stack::push(Type v) { if(isFull()==YES) stackFull(); else stack[++top]=v; }
STEMPNU 4 Queue A Container First-In First-Out (FIFO) Access only to the elements at the front and rear Add: Insert an element to the rear Delete: Remove an element from the front Example Process Scheduling front rear Process 3 Process 9 Process 2Process 8Process 4 Ready Queue CPU
STEMPNU 5 Operations and Implementation Operations Maintenance : creation of a new queue, deletion of queue Add and Delete IsEmpty and IsFull Data Structures Class Queue { private: intfront,rear,MaxSize; Type*queue;// public: Queue(int size); Boolean isFull(), isEmpty(); Typedelete(); voidadd(Type element); }; Class Queue { private: intfront,rear,MaxSize; Type*queue;// public: Queue(int size); Boolean isFull(), isEmpty(); Typedelete(); voidadd(Type element); }; Queue::Queue(int size){ MaxSize=size; queue=new Type[MaxSize]; front=rear=-1; } Queue::Queue(int size){ MaxSize=size; queue=new Type[MaxSize]; front=rear=-1; } Boolean Queue::isFull() { if(rear==MaxSize-1) return YES; else return NO; } Boolean Queue::isFull() { if(rear==MaxSize-1) return YES; else return NO; } Type Queue::delete() { if(isEmpty()==YES) queueEmpty(); else return queue[++front]; } Type Queue::delete() { if(isEmpty()==YES) queueEmpty(); else return queue[++front]; } void Queue::add(Type v) { if(isFull()==YES) queueFull(); else queue[++rear]=v; } void Queue::add(Type v) { if(isFull()==YES) queueFull(); else queue[++rear]=v; } What’s the problem ?
STEMPNU 6 Circular Queue Class CircularQueue { private: intfront,rear,MaxSize; Type*queue;// public: Queue(int size); Typedelete(); voidadd(Type element); }; Class CircularQueue { private: intfront,rear,MaxSize; Type*queue;// public: Queue(int size); Typedelete(); voidadd(Type element); }; void Queue::add(Type v) { newRear=(rear+1)%MaxSize; if(front==newRear) queueFull(); else { rear=newRear; queue[rear]=v; } } void Queue::add(Type v) { newRear=(rear+1)%MaxSize; if(front==newRear) queueFull(); else { rear=newRear; queue[rear]=v; } } void Queue::delete(Type v) { if(front==rear) queueEmpty(); else { front=(front+1)%MaxSize; return queue[front]; } } void Queue::delete(Type v) { if(front==rear) queueEmpty(); else { front=(front+1)%MaxSize; return queue[front]; } } Queue::Queue(int size){ MaxSize=size; queue=new Type[MaxSize]; front=rear=1; } Queue::Queue(int size){ MaxSize=size; queue=new Type[MaxSize]; front=rear=1; }
STEMPNU Example 7 012…MaxSize-1 front rear void Queue::add(Type v) { newRear=(rear+1)%MaxSize; if(front==newRear) queueFull(); else { rear=newRear; queue[rear]=v; } } void Queue::add(Type v) { newRear=(rear+1)%MaxSize; if(front==newRear) queueFull(); else { rear=newRear; queue[rear]=v; } } newRear front=1 rear=1 front=1 rear=1 newRear=2 front=1 rear=2 front=1 rear=2 newRear=3 front=1 rear=3 front=1 rear=MaxSize-1 newRear=0 front=1 rear=0 3 front=1 rear=0 newRear=1 Typr Queue::delete() { if(front==rear) queueEmpty(); else { front=(front+1)%MaxSize; return queue[front]; } } Typr Queue::delete() { if(front==rear) queueEmpty(); else { front=(front+1)%MaxSize; return queue[front]; } } front=2 rear=0 front=MaxSize-1 rear=0 front=0 rear=0 front=0 rear=0
STEMPNU 8 Application of Stack : Mazing Problem How to find the path ?
STEMPNU 9 Path Finding Algorithm for Mazing Problem Algorithm PathFinding(int p,int q,int maze[p][q]) // (p,q): coorinates of exit cell pathStack initialize Stack; pathStack.push((0,0)); while(pathStack.isEmpty()==NO) { (i,j) pathStack.getTop(); // read but not remove while(there is an unvisited cell (m,n) from (i,j)) { pathStack.push((m,n)); if(m==p and n==q) { // path found pathStack.print(); // pop and print return; } (i,j) (m,n); } pathStack.pop(); } print “No path”; End Algorithm PathFinding Algorithm PathFinding(int p,int q,int maze[p][q]) // (p,q): coorinates of exit cell pathStack initialize Stack; pathStack.push((0,0)); while(pathStack.isEmpty()==NO) { (i,j) pathStack.getTop(); // read but not remove while(there is an unvisited cell (m,n) from (i,j)) { pathStack.push((m,n)); if(m==p and n==q) { // path found pathStack.print(); // pop and print return; } (i,j) (m,n); } pathStack.pop(); } print “No path”; End Algorithm PathFinding
STEMPNU 10 Evaluation of Postfix Infix to Postfix Application of stack : Evaluation of Expressions X = A / B – C + D * E – A * C How to evaluate this ? X = (((A / B) – C) + (D * E)) – (A * C) Infix Notation X = (((A B / ) C – ) (D E * ) +) (A C * ) – Postfix Notation X = A B / C – D E * + A C * –
STEMPNU 11 Evaluation of Expression in Postfix Notation X = A B / C – D E * + A C * – AA B T 1 =A / B /AB T1T1 C T1T1 C - T 2 = T 1 - C T2T2 T2T2 DE … T4T4 T5T5 T4T4 - T5T5 T 6 = T 4 – T 5 T6T6 T6T6 = D T2T2 E D
STEMPNU 12 Infix to Postfix X = A / ( B – C ) + D * E – A * C Infix Notation X = A B C – / D E * + A C* – Postfix Notation A A / / / ( ( B B / - ( - C C / ) ( - / ( - / / + + D D + * * E E+ - * -