+ All Categories
Home > Documents > Lecture Slides on Stack and Queue ADTs Prepared...

Lecture Slides on Stack and Queue ADTs Prepared...

Date post: 24-Mar-2018
Category:
Upload: dangliem
View: 221 times
Download: 3 times
Share this document with a friend
25
Lecture Slides on Stack and Queue ADTs Prepared by Dr. Ramana IIT Jodhpur
Transcript

Lecture Slides on Stack and Queue ADTs

Prepared by Dr. Ramana

IIT Jodhpur

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

Implementation

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

IsFull and IsEmpty

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

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

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

TOP and MAKENULL operations

● Top() – If (IsEmpty())

● Return ERROR– Else

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

– top = 1– MaxLength = n

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

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

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.

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

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 -

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)

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

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

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

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( )

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

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

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

(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]

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

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.

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.

(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

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)


Recommended