Presentation is loading. Please wait.

Presentation is loading. Please wait.

2019, Fall Pusan National University Ki-Joune Li

Similar presentations


Presentation on theme: "2019, Fall Pusan National University Ki-Joune Li"— Presentation transcript:

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

2 Stack Stack Example 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 top Bottom

3 Operations – Interface
Maintenance: creation of a new stack, deletion of stack Push: insert an element on the top Pop: delete an element from the top IsEmpty and IsFull: check whether stack is empty or full

4 Stack – Initial Condition and first insertion
A. Construct a stack 1. Prepare an array 1 Max-1 * Initially empty  when top = ? 2. top: index to top element  initially 0 or ? B. Insert the first element 1 Max-1 Copy the element to slot at the next of top top=-1

5 Implementation Internal Implementation – Data Structure
Stack::Stack(int size){ MaxSize=size; stack=new Type[MaxSize]; top=-1; } Boolean Stack::isFull() { if(top==MaxSize-1) return YES; else return NO; } Class Stack { private: int top,MaxSize; Type *stack;// public: Stack(int size); Boolean isFull(), isEmpty(); Type pop(); void push(Type element); }; Type Stack::pop() { if(isEmpty()==YES) stackEmpty(); else return stack[top--]; } void Stack::push(Type v) { if(isFull()==YES) stackFull(); else stack[++top]=v; }

6 Queue Queue Example A Container First-In First-Out (FIFO) rear
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 Process 3 Process 9 Process 2 Process 8 Process 4 Ready Queue CPU

7 Operations – Interface
Maintenance : creation of a new queue, deletion of queue Add and Delete IsEmpty and IsFull

8 Queue – Implementation: Initial Condition
A. Construct a queue 1. Prepare an array 1 Max-1 * Initially empty  front = ? and rear = ? 2. front: index to the new element  initially 0 or ? rear: index to the last element  initially 0 or ?

9 Implementation Data Structures 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; } Class Queue { private: int front,rear,MaxSize; Type *queue;// public: Queue(int size); Boolean isFull(), isEmpty(); Type delete(); void add(Type element); }; Type Queue::delete() { if(isEmpty()==YES) queueEmpty(); else return queue[++front]; } void Queue::add(Type v) { if(isFull()==YES) queueFull(); else queue[++rear]=v; }

10 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; } }

11 Example 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 1 2 3 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=0 rear=0 front=0 rear=0 front=MaxSize-1 rear=0

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

13 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

14 Application of stack : Evaluation of Expressions
How to evaluate this ? X = A / B – C + D * E – A * C Infix to Postfix X = (((A / B) – C) + (D * E)) – (A * C) Infix Notation X = (((A B / ) C – ) (D E *) +) (A C *) – Postfix Notation Evaluation of Postfix X = A B / C – D E * + A C * –

15 Evaluation of Expression in Postfix Notation
X = A B / C – D E * + A C * – A B / C - D E T4 - T5 T6= T4 – T5 T6 = A A B T1=A / B T1 T1 C T2= T1 - C T2 T2 T2 E D T4 T5 D T6

16 Infix to Postfix Infix Notation Postfix Notation
X = A / ( B – C ) + D * E – A * C Infix Notation A / ( B - C ) + D * E - - - ( ( ( ( * / / / / / / + + - A B C - / D E * + X = A B C – / D E * + A C* – Postfix Notation


Download ppt "2019, Fall Pusan National University Ki-Joune Li"

Similar presentations


Ads by Google