1 / 15

Stacks and Queues

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

donsmith
Télécharger la présentation

Stacks and Queues

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Stacks and Queues 2018, Fall Pusan National University Ki-Joune Li

  2. 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

  3. 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

  4. 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); };

  5. 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

  6. 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); };

  7. 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

  8. 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; }

  9. 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; } }

  10. 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

  11. Enter Maze Exit Application of Stack : Mazing Problem How to find the path ?

  12. 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

  13. 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* –

  14. - 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

  15. 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 * +

More Related