Top Banner
Lecture Slides on Stack and Queue ADTs Prepared by Dr. Ramana IIT Jodhpur
25

Lecture Slides on Stack and Queue ADTs Prepared …home.iitj.ac.in/~ramana/dsa-lecture-stacks.pdfLecture Slides on Stack and Queue ADTs Prepared by Dr. Ramana IIT Jodhpur Stack ADT

Mar 24, 2018

Download

Documents

dangliem
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lecture Slides on Stack and Queue ADTs Prepared …home.iitj.ac.in/~ramana/dsa-lecture-stacks.pdfLecture Slides on Stack and Queue ADTs Prepared by Dr. Ramana IIT Jodhpur Stack ADT

Lecture Slides on Stack and Queue ADTs

Prepared by Dr. Ramana

IIT Jodhpur

Page 2: Lecture Slides on Stack and Queue ADTs Prepared …home.iitj.ac.in/~ramana/dsa-lecture-stacks.pdfLecture Slides on Stack and Queue ADTs Prepared by Dr. Ramana IIT Jodhpur Stack ADT

Stack ADT● Last element inserted is removed first● Operations

– PUSH (elemType e) - inserts an item – POP () - removes an element from top – TOP () - returns the element at the top– IsEmpty() - return true if stack is empty– IsFull() - returns true if stack is full– MakeNull() - to initialize stack variables– All operations are performed in O(1), constant time

Page 3: Lecture Slides on Stack and Queue ADTs Prepared …home.iitj.ac.in/~ramana/dsa-lecture-stacks.pdfLecture Slides on Stack and Queue ADTs Prepared by Dr. Ramana IIT Jodhpur Stack ADT

Implementation

● Using Arrays – variables– top - points the position to insert the next element– maxLength – maximum stack size– S[maxLength] – stores elements of type elementType

Page 4: Lecture Slides on Stack and Queue ADTs Prepared …home.iitj.ac.in/~ramana/dsa-lecture-stacks.pdfLecture Slides on Stack and Queue ADTs Prepared by Dr. Ramana IIT Jodhpur Stack ADT

IsFull and IsEmpty

● IsFull ()– If top > maxLentgh then return TRUE– Else return FALSE

● IsEmpty ()– If top == 1 then return TRUE – Else return FALSE

Page 5: Lecture Slides on Stack and Queue ADTs Prepared …home.iitj.ac.in/~ramana/dsa-lecture-stacks.pdfLecture Slides on Stack and Queue ADTs Prepared by Dr. Ramana IIT Jodhpur Stack ADT

PUSH and POP operations

● PUSH (elemType e)– If (IsFull()) then Print “Stack is full” – Else

● S[top] = e● top = top + 1

● POP()– If (IsEmpty()) then Print “Empty Stack”– Else

● top = top - 1

Page 6: Lecture Slides on Stack and Queue ADTs Prepared …home.iitj.ac.in/~ramana/dsa-lecture-stacks.pdfLecture Slides on Stack and Queue ADTs Prepared by Dr. Ramana IIT Jodhpur Stack ADT

TOP and MAKENULL operations

● Top() – If (IsEmpty())

● Return ERROR– Else

● Return S[top-1]● MakeNull(int n)

– top = 1– MaxLength = n

Page 7: Lecture Slides on Stack and Queue ADTs Prepared …home.iitj.ac.in/~ramana/dsa-lecture-stacks.pdfLecture Slides on Stack and Queue ADTs Prepared by Dr. Ramana IIT Jodhpur Stack ADT

Applications● String reversal ● Function calls implementation

– A stack is maintained to store the activation records– Each activation record (also known stack frame) stores

details of local variables and their values, return address (that is program counter value), parameters to be passed to function

● Arithmetic expression evaluation● Backtracking● Towers of Hanoi and many more......

Page 8: Lecture Slides on Stack and Queue ADTs Prepared …home.iitj.ac.in/~ramana/dsa-lecture-stacks.pdfLecture Slides on Stack and Queue ADTs Prepared by Dr. Ramana IIT Jodhpur Stack ADT

Arithmetic Express Evaluation

● Usual representation – infix● To be converted to postfix format as the precedences can

easily be embedded in evaluation ● Two phase

– Converting infix to postfix– Evaluating postfix expression

Page 9: Lecture Slides on Stack and Queue ADTs Prepared …home.iitj.ac.in/~ramana/dsa-lecture-stacks.pdfLecture Slides on Stack and Queue ADTs Prepared by Dr. Ramana IIT Jodhpur Stack ADT

Operator Precedence/ Associativity ● Low to high

– + - – * / %– ^ (exponetiation operator)

● Associativity – Left to right, + - * / % a * b / c = (a * b) / c,

if two operators of same priority are compared, then the left one's priority is higher than right one's.

– Right to left, ^ a ^ b ^ c = a^(b^c)– if two operators of same priority are compared, then the right one's

priority is higher than left one's. ● Note only some binary operators are mentioned here. Generally a programming

language supports a rich set of operators.

Page 10: Lecture Slides on Stack and Queue ADTs Prepared …home.iitj.ac.in/~ramana/dsa-lecture-stacks.pdfLecture Slides on Stack and Queue ADTs Prepared by Dr. Ramana IIT Jodhpur Stack ADT

Converting infix to postfix (steps)1. Read infix expression and initialze stack to store operators

2. Start read one token, T, at a time for the expresssion

3. If T is an operand then write T to output

4. Else /* T is operator */

1. If prec(T) > prec( TOP() ) then PUSH (T)

2. Else if prec(T) == prec( TOP() ) AND T is right associative then PUSH (T)

3. Else

1.While ( prec(T) < prec(K=TOP()) 1.POP(), write K to output

2.PUSH (T)5. Repeat steps 2 to 4 until all tokens are read from infix exp

Page 11: Lecture Slides on Stack and Queue ADTs Prepared …home.iitj.ac.in/~ramana/dsa-lecture-stacks.pdfLecture Slides on Stack and Queue ADTs Prepared by Dr. Ramana IIT Jodhpur Stack ADT

Example: infix exp 5 + 3 * 4 – 2● Write 5 to output, OUT=5● PUSH (+), STACK = { + } ● Write 3 to output, OUT=5 3● PUSH (*), STACK = {+ * }● Write 4 to output, OUT=5 3 4● TOP(), POP ( ), OUT=5 3 4 *, STACK={ + }● TOP(), POP( ), OUT=5 3 4 * +, STACK={ }● PUSH ( - ), STACK = { - }● Write 2 to output, OUT=5 3 4 * + 2● No more tokens, POP all from stack and write to output, OUT = 5

3 4 * + 2 -

Page 12: Lecture Slides on Stack and Queue ADTs Prepared …home.iitj.ac.in/~ramana/dsa-lecture-stacks.pdfLecture Slides on Stack and Queue ADTs Prepared by Dr. Ramana IIT Jodhpur Stack ADT

Evaluating Postfix 5 3 4 * + 2 -● Operand stack to be maintained● PUSH (5)● PUSH (3)● PUSH (4)● O1 = TOP(), POP()● O2 = TOP(), POP()● temp = O1 * O2● PUSH (temp)

Page 13: Lecture Slides on Stack and Queue ADTs Prepared …home.iitj.ac.in/~ramana/dsa-lecture-stacks.pdfLecture Slides on Stack and Queue ADTs Prepared by Dr. Ramana IIT Jodhpur Stack ADT

(Cont.)● O1 = TOP(), POP()● O2 = TOP(), POP()● temp = O1 + O2 , 12 + 5● PUSH (temp)● PUSH (2)● O1 = TOP(), POP()● O2 = TOP(), POP()● temp = O1 + O2 , 17 – 2 ● PUSH(temp)● End of postfix expression, so result is in stack.

Page 14: Lecture Slides on Stack and Queue ADTs Prepared …home.iitj.ac.in/~ramana/dsa-lecture-stacks.pdfLecture Slides on Stack and Queue ADTs Prepared by Dr. Ramana IIT Jodhpur Stack ADT

Evaluating postfix expression (for binary operators)

1. Operand stack to be maintained

2. Read postfix expression1. If operand then PUSH

2. Else if operator

1.Rightoperand = TOP(); POP ()2.Leftoperand = TOP(); POP () 3.Temp = Leftoperand OP Rightoperand4.PUSH (Temp)

3. Repeat from step 2 until reaching the end of the postfix exp

4. Final result will apprear at top of the stack

Page 15: Lecture Slides on Stack and Queue ADTs Prepared …home.iitj.ac.in/~ramana/dsa-lecture-stacks.pdfLecture Slides on Stack and Queue ADTs Prepared by Dr. Ramana IIT Jodhpur Stack ADT

Dealing with parenthesis

● During infix to postfix conversion – Always PUSH left parenthesis – When right parenthesis is encountered, POP all items

from the stack until the corresponding left parenthesis and write the items to the output

Page 16: Lecture Slides on Stack and Queue ADTs Prepared …home.iitj.ac.in/~ramana/dsa-lecture-stacks.pdfLecture Slides on Stack and Queue ADTs Prepared by Dr. Ramana IIT Jodhpur Stack ADT

Queue ADT

● First in first out behaviour (FIFO)● Items are removed from front end and insertions happen at

rear end of QUEUE● Operations

– EnQueue (elemType e) – inserts element e at the end– DeQueue ( ) - deletes first element from queue– Front ( ) - returns first element on queue– IsEmpty(), IsFull(), MakeNull( )

Page 17: Lecture Slides on Stack and Queue ADTs Prepared …home.iitj.ac.in/~ramana/dsa-lecture-stacks.pdfLecture Slides on Stack and Queue ADTs Prepared by Dr. Ramana IIT Jodhpur Stack ADT

Applications

● Numerous real world applications– Queues in super market, petrol bunks, ticket counters,

and any other service stations● In computers

– Job scheduling in operating systems– Job scheduling in printers– Packet scheduling in network routers

Page 18: Lecture Slides on Stack and Queue ADTs Prepared …home.iitj.ac.in/~ramana/dsa-lecture-stacks.pdfLecture Slides on Stack and Queue ADTs Prepared by Dr. Ramana IIT Jodhpur Stack ADT

Implementation

● Two pointers – front – points first element to be deleted from queue– rear – points the next position where the newly arriving

element can be inserted– maxLength – maximum queue size– QUEUE[maxLength] - stores elements of type

elementType

Page 19: Lecture Slides on Stack and Queue ADTs Prepared …home.iitj.ac.in/~ramana/dsa-lecture-stacks.pdfLecture Slides on Stack and Queue ADTs Prepared by Dr. Ramana IIT Jodhpur Stack ADT

Empty and Full Queue● Empty queue condition

– If rear == front then return TRUE– Else return FALSE

● Full queue condition – If rear > maxLength then return TRUE– Else return FALSE

● EnQueue(elemType e)– If IsFull ( ) then Print “Queue Overflow”– Else

● Queue[rear] = e● rear = rear + 1

Page 20: Lecture Slides on Stack and Queue ADTs Prepared …home.iitj.ac.in/~ramana/dsa-lecture-stacks.pdfLecture Slides on Stack and Queue ADTs Prepared by Dr. Ramana IIT Jodhpur Stack ADT

(Cont.)● addOne(int i)

– Return i + 1● DeQueue( )

– If IsEmpty () then print “Queue Underflow”– Else

● front = addOne(front)● Front ( )

– If IsEmpty () return ERROR– Else return QUEUE[front]

Page 21: Lecture Slides on Stack and Queue ADTs Prepared …home.iitj.ac.in/~ramana/dsa-lecture-stacks.pdfLecture Slides on Stack and Queue ADTs Prepared by Dr. Ramana IIT Jodhpur Stack ADT

ExampleAction front rear

Initial Q 1 1

EnQueue ( A) A 1 2

EnQueue ( B) A B 1 3

EnQueue ( C) A B C 1 4

EnQueue ( D) A B C D 1 5

DeQueue () B C D 2 5

DeQueue () C D 3 5

EnQueue (X) C D Error QUEUE OVERFLOW as rear > maxQlength of 4

Page 22: Lecture Slides on Stack and Queue ADTs Prepared …home.iitj.ac.in/~ramana/dsa-lecture-stacks.pdfLecture Slides on Stack and Queue ADTs Prepared by Dr. Ramana IIT Jodhpur Stack ADT

Issues● EnQueue and DeQueue operations are expected to take

constant time. However, IsFull() may report “Queue Overflow” even before queue is really full.

● To overcome from this, either EnQueue or DeQueue operation to be made as O(n) operation

– Ex, when an element is dequeued, all elements can be shifted left by one position to use the position that became free

● Alternatively Circular Queues can be used to perform each operation in O(1) time and to ensure reporting “Queue Overflow” event only when queue is really full.

Page 23: Lecture Slides on Stack and Queue ADTs Prepared …home.iitj.ac.in/~ramana/dsa-lecture-stacks.pdfLecture Slides on Stack and Queue ADTs Prepared by Dr. Ramana IIT Jodhpur Stack ADT

Circular Queues

● Can be visualized as a queue that has no real end and it can loop / wrap around the buffer

● However, memory is not physically created as a ring, therefore a linear representation is used and “rear” and “front” pointers are warped around, if permitted and when reached maximum queue length.

Page 24: Lecture Slides on Stack and Queue ADTs Prepared …home.iitj.ac.in/~ramana/dsa-lecture-stacks.pdfLecture Slides on Stack and Queue ADTs Prepared by Dr. Ramana IIT Jodhpur Stack ADT

(Cont.)● addOne(int i)

– return ( i % maxLentgh) + 1● Empty queue condition

– If rear == front then return TRUE– Elese return FALSE

● Full queue condition – If addOne(rear) == front then return TRUE– Else return FALSE

Page 25: Lecture Slides on Stack and Queue ADTs Prepared …home.iitj.ac.in/~ramana/dsa-lecture-stacks.pdfLecture Slides on Stack and Queue ADTs Prepared by Dr. Ramana IIT Jodhpur Stack ADT

ENQUEUE & DEQUEUE● EnQueue(elemType e)

– If IsFull ( ) then Print “Queue Overflow”– Else

● Queue[rear] = e● rear = addOne(rear)

● DeQueue()– If IsEmpty ( ) then Print “Queue Underflow”– Else

● front = addOne(front)