150 likes | 173 Views
Stacks and Queues. 2018, Fall Pusan National University Ki-Joune Li. top. Bottom. Stack. 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
E N D
Stacks and Queues 2018, Fall Pusan National University Ki-Joune Li
top Bottom Stack • 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
Stack and Function Call FunctionA Function B FunctionB Function C FunctionC return Function C return What happens and how to manage the switch of contexts Stack Frame
Stack::Stack(int size){ MaxSize=size; stack=new Type[MaxSize]; top=-1; } Boolean Stack::isFull() { if(top==MaxSize-1) return YES; else return NO; } Type Stack::pop() { if(isEmpty()==YES) stackEmpty(); else return stack[top--]; } void Stack::push(Type v) { if(isFull()==YES) stackFull(); else stack[++top]=v; } Operations and Implementation • Operations • Maintenance : creation of a new stack, deletion of stack • Push and Pop • IsEmpty and IsFull • Data Structures Class Stack { private: int top,MaxSize; Type *stack;// public: Stack(int size); Boolean isFull(), isEmpty(); Type pop(); void push(Type element); };
Ready Queue CPU Process 2 Process 8 Process 4 Process 9 Process 3 Queue • 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 rear front
Operations and Implementation • Operations • Maintenance : creation of a new queue, deletion of queue • Add and Delete • IsEmpty and IsFull • Data Structures Class Queue { private: int front,rear,MaxSize; Type *queue;// public: Queue(int size); Boolean isFull(), isEmpty(); Type delete(); void add(Type element); };
Operations and Implementation Initial State Adding one Deleting one 200-1 rear 2 1 1 1 1 front 0 0 0 0 rear rear rear front front front
Operations and Implementation What’s the problem ? 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; } Type Queue::delete() { if(isEmpty()==YES) queueEmpty(); else return queue[++front]; } void Queue::add(Type v) { if(isFull()==YES) queueFull(); else queue[++rear]=v; }
Circular Queue Class CircularQueue { private: int front,rear,MaxSize; Type *queue;// public: Queue(int size);Type delete(); void add(Type element); }; Queue::Queue(int size){ MaxSize=size; queue=new Type[MaxSize]; front=rear=1; } void Queue::delete(Type v) { if(front==rear) queueEmpty(); else { front=(front+1)%MaxSize; return queue[front]; } } 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; } } Example Typr Queue::delete() { if(front==rear) queueEmpty(); else { front=(front+1)%MaxSize; return queue[front]; } } front 3 0 1 2 … MaxSize-1 rear newRear front=1 rear=1 front=1 rear=1 newRear=2 front=1 rear=2 newRear=3 front=1 rear=MaxSize-1 newRear=0 front=1 rear=0 newRear=1 front=1 rear=2 front=1 rear=3 front=1 rear=0 front=2 rear=0 front=MaxSize-1 rear=0 front=0 rear=0 front=0 rear=0
Enter Maze Exit Application of Stack : Mazing Problem How to find the path ?
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
Infix Notation X = (((A / B) – C) + (D * E)) – (A * C) Infix to Postfix Postfix Notation X = (((A B / ) C – ) (D E *) +) (A C*) – Evaluation of Postfix Application of stack : Evaluation of Expressions How to evaluate this ? X = A / B – C + D * E – A * C X = A B / C – D E* + A C* –
- E B C D T5 T5 A A T1 T1 T2 T2 T2 T4 T4 T6 T1=A / B T2= T1 - C T6= T4 – T5 Evaluation of Expression in Postfix Notation X = A B / C – D E* + A C* – C A B / - D E = D … T6
Infix Notation X = A / ( B – C ) + D * E – A * C Postfix Notation X = A B C – /D E * + A C* – Infix to Postfix A / ( B - C ) + D * E - - - ( ( ( ( * / / / / / / + + - A B C - / D E * +