Download presentation
Presentation is loading. Please wait.
1
STACKS
2
Stack- A stack is a linear data structure in which items may be added or removed only at one end . Accordingly, stacks are also called last-in-first-out or LIFO lists. The end at which element is added or removed is called the top of the stack. Two basic operations associated with stacks are : Push- Term used to denote insertion of an element onto a stack. Pop- Term used to describe deletion of an element from a stack. The order in which elements are pushed onto a stack is reverse of the order in which elements are popped from a stack
3
Representation of stacks
Stacks may be represented in memory in various ways usually by means of one-way list or a linear array In array representation of stack, stack is maintained by an array named STACK , a variable TOP which contains the location/index of top element of stack and a variable MAXSTK giving the maximum number of elements that can be held by the stack. The condition TOP=0 or TOP=NULL indicates that stack is empty. The operation of addition of an item on stack and operation of removing an item from a stack may be implemented respectively by sub algorithms, called PUSH and POP. Before executing operation PUSH on to a stack, one must first test whether there is room in the stack for the new item. If not, then we have the condition known as overflow. Analogously, in executing the POP operation, one must first test where there is an element in stack to be deleted. If not, then we have condition known as underflow.
4
ARRAY IMPLEMENTATION OF STACK
5
Step 1: [Stack already filled] If TOP==MAXSTK-1, then:
Algorithm: PUSH (STACK,TOP,MAXSTK,ITEM) This algorithm pushes an item onto the stack array. TOP stores the index of top element of the stack and MAXSTK stores the maximum size of the stack. Step 1: [Stack already filled] If TOP==MAXSTK-1, then: Write: ‘OVERFLOW’ Return Step 2: Set TOP:=TOP+1 Step 3: Set STACK[TOP]:=ITEM Step 4: Return
6
Step 2: Set ITEM:=STACK[TOP] Step 3: Set TOP:=TOP-1 Step 4: Return
Algorithm: POP(STACK,TOP,ITEM) This procedure deletes the top element of STACK array and assign it to variable ITEM Step 1: If TOP== -1,then: Write: ‘UNDERFLOW’ Return Step 2: Set ITEM:=STACK[TOP] Step 3: Set TOP:=TOP-1 Step 4: Return
7
A stack represented using a linked list is also known as linked stack.
The array based representation of stack suffers from following limitations: Size of stack must be known in advance Representing stack as an array prohibits the growth of stack beyond finite number of elements. In a linked list implementation of stack, each memory cell will contain the data part of the current element of stack and pointer that stores the address of its bottom element and the memory cell containing the bottom most element will have a NULL pointer
8
Push operation on linked list representation of stack
Algorithm: PUSH(INFO, LINK, TOP, ITEM, AVAIL) This algorithm pushes an element to the top of the stack Step 1: If AVAIL==NULL, then Write: ‘OVERFLOW’ Return Step 2: Set NEW=AVAIL and AVAIL = AVAIL->LINK Step 3: Set NEW->INFO =ITEM Step 4: Set NEW->LINK=TOP Set TOP=NEW Step 5: EXIT
9
POP operation on linked list representation of stack
Algorithm: POP(INFO, LINK, TOP, AVAIL, ITEM) This algorithm deletes an element from the top of the stack Step 1: If TOP==NULL , then: Write: ‘UNDERFLOLW’ AND Exit Step 2: Set ITEM = TOP->INFO Step 3: Set TEMP = TOP AND TOP= TOP->LINK Step 4: TEMP->LINK = AVAIL AND AVAIL = TEMP Step 5: EXIT
10
Application of stack Evaluation of arithmetic expression. For most common arithmetic operations, operator symbol is placed between its operands. This is called infix notation of an expression. To use stack to evaluate an arithmetic expression, we have to convert the expression into its prefix or postfix notation. Polish notation , refers to the notation in which operator symbol is placed before its two operands. This is also called prefix notation of an arithmetic expression. The fundamental property of polish notation is that the order in which operations are to be performed is completely determined by positions of operators and operands in expression. Accordingly, one never needs parentheses when writing expression in polish notation. Reverse Polish notation refers to notation in which operator is placed after its two operands. This notation is frequently called postfix notation. Examples of three notations are:
11
INFIX NOTATION: A+B PREFIX OR POLISH NOTATION: +AB POSTFIX OR REVERSE POLISH NOATATION: AB+ Convert the following infix expressions to prefix and postfix forms A+(B*C) (A+B)/(C+D) Prefix: A*BC Postfix: A BC*+ Prefix: / +AB+CD Postfix: AB+CD+/
12
Infix: Postfix/ Prefix
Example: Infix: Postfix/ Prefix A + B - C AB + C - - + ABC (A + B) * (C - D) AB + CD - * * + AB - CD A ^ B * C - D + E / F /(G + H) AB ^ C * D - EF / GH + / + + - * ^ ABCD / / EF + GH ((A + B) * C – (D - E)) ^ (F + G) AB + C * DE - - FG + ^ ^ - * + ABC – DE + FG A - B/(C * D ^ E) ABCDE ^ * / - - A / B * C ^ DE
13
The computer usually evaluates an arithmetic expression written in infix notation in two steps.
Converts expression to postfix notation Evaluates the postfix notation. Evaluation of postfix expression Suppose P is an arithmetic expression written in postfix notation. The following algorithm which uses a STACK to hold operands and evaluates P
14
Algorithm: This algorithm finds the VALUE of an arithmetic
expression P written in Postfix notation. Step 1: Add a right parentheses “ )” at the end of P Step 2: Scan P from left to right and repeat step 3 and 4 for each element of P until “)” is encountered Step 3: If an operand is encountered, put the operand on the stack Step 4: If an operator is encountered , then: (a) Remove the two top elements of stack, where A is top element and B is next-top-element. (b) Evaluate B A (c ) Place the result of (b) back on stack [End of if structure] [End of step 2 loop] Step 5: Set VALUE equal to the top element of stack Step 6: Exit
15
Transforming Infix Expression into Postfix Expression
The algorithm uses a stack to temporarily hold operators and left parentheses. The postfix expression P will be constructed from left to right using operands from Q and operators which are removed from STACK. The algorithm begins by pushing a left parentheses onto stack and adding a right parentheses at the end of Q Algorithm: POSTFIX (Q, P) Suppose Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix expression P Step 1: Push “(“ on to the STACK and add “)” to the end of Q Step 2: Scan Q from left to right and repeat step 3 to 6 for each element of Q until the STACK is empty Step 3: If an operand is encountered, add it to P Step 4: If left parentheses is encountered, add it to STACK
16
Step 5: If an operator is encountered, then:
(a) Repeatedly pop from STACK and add to P each operator which has same precedence or higher precedence than (b) Add to STACK Step 6: If a right parentheses is encountered , then: (a) Repeatedly pop from STACK and add to P each operator until a left parentheses is encountered (b) Remove the left parentheses [End of Step 2 Loop] Step 7: Exit
17
Example: Convert Q=A+(B*C) / D to its corresponding postfix form
Solution: put “)” at the end of Q and put “(“ on stack Starting from left: Operand A , put it on P Operator move to stack as no operator there ( move on stack Operand B, put it on P Operator * , move to stack as no operator Operand C , move to P ) , pop from stack and put on P until “ (“ is encountered. Pop “(“ also operator /, as precedence of / is higher than + on stack, no pop possible. Push / on stack Operand D , put it on P Right parentheses ), Pop all the elements and add the P until ( is encountered. Also remove ( from stack P= A B C* D / + * + ( / + ( ( + (
18
Example: Consider the following infix expression Q:
Q : A + ( B * C – ( D / E ^ F) * G) * H Convert Q into it’s postfix expression p using stack. Ans: P : A B C * D E F ^ / G * - H * + Example: Consider the following infix expression : A + B / C + D * (E – F) ^ G Convert Q into it’s postfix expression using stack. Ans: A B C / + D E F - G ^ * +
19
Practical applications of stack
Stacks are used for implementing function calls in a program Used for implementing recursion. Used for conversion of infix expression to its postfix or prefix form Used for evaluation of postfix expression. Used in sorting of arrays (quicksort and mergesort technique)
20
Infix to Postfix Example
A + B * C - D / E Solve by Yourself: Infix Stack Postfix Expression
21
Postfix Evaulation Operand: push
Operator: pop 2 operands, do the math, pop result back onto stack * Postfix Stack[top ] 2 3 + * 1 3 + * 1 2 + * * // 5 from 2 + 3 5 // 5 from 1 * 5
22
Ex: Evaluate the following postfix expression 1- p= 5, 6, 2, +, *, 12, 4, /, - Ans: 37 2- p=6, 2, 3, +, -, 3, 8, 2, /, +, *, 2, ^, 3, + Ans: 52 3- Convert infix to postfix and evaluate for a=3, b=4, c=5 Infix: (A+B) *C
23
Recursive Solution
24
Recursive Solution
25
Recursive Solution
26
Recursive Solution
27
Tower of Hanoi
28
Tower of Hanoi
29
Tower of Hanoi
30
Tower of Hanoi
31
Tower of Hanoi
32
Tower of Hanoi
33
Tower of Hanoi
34
Tower of Hanoi
35
Recursive Algorithm TOWER(N, BEG, AUX, END) 1- (If N==1)
1- Write BEG ->END 2- Return [End of If structure] 2- Call TOWER(N-1, BEG, END, AUX) 3- Write: BEG->END 4- Call TOWER(N-1, AUX, BEG, END) 5- Return
36
Let Gn(i) be a function from [0,…,2n-1]
Total no of moves = 2n-1
37
Application of Stack: Quick Sort
It is an algorithm of divide and conquer.
38
Example We are given array of n integers to sort: 40 20 10 80 60 50 7
30 70
39
Pick Pivot Element There are a number of ways to pick the pivot element. In this example, we will use the first element in the array: 40 20 10 80 60 50 7 30 70
40
Partition Result 7 20 10 30 40 50 60 80 70 [0] [1] [2] [3] [4] [5] [6] [7] [8]
41
Quick sort algorithm PARTITION(A, BEG, END, LOC)
1- Set LEFT = BEG, RIGHT=END, LOC=BEG 2- [Scan from right to left] (a) Repeat while A[LOC] <=A[RIGHT] and LOC != RIGHT RIGHT = RIGHT-1 [End of Loop] (b) If LOC == RIGHT then return (c) If A[LOC] > A[RIGHT] then (i) Interchange A[LOC] and A[RIGHT] (ii) Set LOC = RIGHT [End of if structure] 3- [Scan from left to right] (a) Repeat while A[LEFT] <=A[LOC] and LEFT != LOC LEFT = LEFT+1 (b) If LOC == LEFT then return (c) If A[LEFT] > A[LOC] then (i) Interchange A[LEFT] and A[LOC] (ii) Set LOC = LEFT (iii) Go to step 2.
42
Quick sort algorithm QUICKSORT() 1- TOP = -1
2- If N >1 then TOP = TOP + 1, LOWER[TOP] = 0, UPPER[TOP] = N-1 3- Repeat steps 4 to 7 while TOP != -1 4- [Pop sublist from stacks] Set BEG = LOWER[TOP] , END = UPPER[TOP] TOP = TOP – 1 5- Call PARTITION (A, BEG, END, LOC) 6- [ Push left sublist onto stacks when it has 2 or more elements] If BEG < LOC-1 then TOP = TOP + 1 LOWER[TOP] = BEG UPPER[TOP] = LOC-1 [End of if Structure] 7- [ Push right sublist onto stacks when it has 2 or more elements] If LOC+1 < END then TOP = TOP+1 LOWER[TOP] = LOC+1 UPPER[TOP] = END [End of step 3 loop] 8- Exit
43
Complexity of Quick Sort
Worst case O(n2) Average case O(n log n)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.