KEMBAR78
Stacks in DATA STRUCTURE | PPTX
Stacks
mandeepsingh203@gmail.com
2
Stacks
• A stack is a list in which insertion and deletion take
place at the same end
– This end is called top
– The other end is called bottom
3
• Stacks are known as LIFO (Last In, First Out)
lists:
The last element inserted will be the first to be
retrieved, using Push and Pop
• Attributes of Stack
– MAXSTACK: which gives the maximum
number of elements that can be held by the stack.
– TOP: the index of the top element of stack
• Operations of Stack
– push: add an element to the top of stack
– pop: delete the element at the top of stack
4
Array Representation of Stack
• STACK: Linear Array
 TOP: Pointer variable, which contains the location of top element of the stack.
 MAXSTACK: A variable, which gives the maximum number of elements that
can be held by the stack.
 If TOP=0 or TOP=NULL Stack is empty and this condition is
known as underflow.
 If TOP= MAXSTACK Stack is Full and this condition is known
as Overflow.
STACK
TOP MAXSTACK
A B C
1 2 3 4 5 6 7 8 9
3 9
5
Push and Pop operations on Stack
PUSH(STACK,TOP,MAXSTACK,ITEM)
1. [STACK already filled]
If TOP=MAXSTACK, then Print
OVERFLOW and Return.
2. Set TOP=TOP+1
3. Set STACK[TOP]=ITEM.
4. Return.
top
empty stack
A
top
push an element
top
push another
A
B
top
pop
A
POP(STACK,TOP,ITEM)
1. If TOP=0, then print
UNDERFLOW and Return
2. Set ITEM=STACK[TOP].
3. Set TOP=TOP-1.
4. Return.
6
Linked Representation of Stacks
• Linked Stack is a stack that is implemented
using a singly linked list.
• INFO field of the node holds the element of
the stack.
• LINK field of the node holds pointer to the
neighboring element in the stack.
5 10 3 7
INFO LINK
5 10 3
7
PUSH operation in Linked Stack
1. If AVAIL == NULL then write OVERFLOW and
Exit
2. Set NEW = AVAIL and AVAIL=LINK[AVAIL]
3. Set INFO[NEW] = ITEM
4. Set LINK[NEW] = TOP
5. Set TOP = NEW
6. Exit
*AVAIL is the pointer to available free nodes list.
8
POP operation in Linked Stack
1. If TOP == NULL then write UNDERFLOW and
Exit
2. Set ITEM = INFO[TOP]
3. Set TEMP = TOP and TOP = LINK[TOP]
4. Set LINK[TEMP]=AVAIL and AVAIL=TEMP
5. Exit
*TEMP is the temporary node to save the deleted node
and add it to available free nodes list.
9
Arithmetic Expressions
Infix, Prefix and Postfix
If the operator is placed in between operands, it is called
infix notation
e.g. A+B, (A+B)*C, A+(B*C)
Prefix notation (Polish Notation) – operators preceded the
operands
e.g. +AB
Postfix Notation (Reverse Polish Notation) - Operator
symbol is placed after the operands
e.g. A+B= AB+
10
Evaluation of a postfix notation
1. Add a right parenthesis “)” at the end of P.
2. Scan P from left to right and repeat Step3 and 4 for each
element of P until the sentinel ”)” is encountered.
3. If an operand is encountered, push it on STACK.
4. If an operator X is encountered, then:
(a) Remove the two top elements of STACK, where A is the
top element and B is the next to top element.
(b) Evaluate B X A.
(c) Place the result of (b) back on STACK.
5. Set VALUE equal to the top element on the STACK.
6. Exit.
11
Example
• Consider the following arithmetic expression P
written in postfix notations :
P : 5, 6, 2, +, *, 12, 4, /, -
• We evaluate P by simulating algorithm. First
we add a sentinel right parenthesis at the end
of P to obtain
P : 5, 6, 2, +, *, 12, 4, /, -, )
(1) (2) (3) (4) (5) (6) (7) (8) (9) (10)
12
Symbol Scanned STACK
(1) 5 5
(2) 6 5, 6
(3) 2 5, 6, 2
(4) + 5, 8
(5) * 40
(6) 12 40, 12
(7) 4 40, 12, 4
(8) / 40, 3
(9) - 37
(10) )
13
Infix to Postfix Conversion
POLISH(Q,P)
1. Push “(” onto STACK and add “)” to the end of Q.
2. Scan Q from left to right and repeat step 3 to 6 for each element of Q
until the STACK is empty:
3. If an operand is encountered, add it to P.
4. If a left parenthesis is encountered, push it onto STACK.
5. If an operator X is encountered , then:
1. Repeatedly pop from STACK and add to P each operator
which has the same or higher precedence than X.
2. Add X to STACK.
6. If a right parenthesis is encountered, then:
1. Repeatedly pop from STACK and add to P each operator
until a left parenthesis is encountered.
2. Remove the left parenthesis.
7. Exit.
Example: Infix to Postfix Expression A+(B*C-(D/E↑F)*G)*H
Symbol Scanned
(1) A ( A
(2) + ( + A
(3) ( ( + ( A
(4) B ( + ( A B
(5) * ( + ( * A B
(6) C ( + ( * A B C
(7) - ( + ( - A B C *
(8) ( ( + ( - ( A B C *
(9) D ( + ( - ( A B C * D
(10) / ( + ( - ( / A B C * D
(11) E ( + ( - ( / A B C * D E
(12) ↑ ( + ( - ( / ↑ A B C * D E
(13) F ( + ( - ( / ↑ A B C * D E F
(14) ) ( + ( - A B C * D E F ↑ /
(15) * ( + ( - * A B C * D E F ↑ /
(16) G ( + ( - * A B C * D E F ↑ / G
(17) ) ( + A B C * D E F ↑ / G * -
(18) * ( + * A B C * D E F ↑ / G * -
(19) H ( + * A B C * D E F ↑ / G * - H
(20) ) A B C * D E F ↑ / G * - H * +
15
Stacks
• Real life analogy:
– Dish holders (stacks)
• Typical uses of stacks:
– Prefix-/Postfix- calculators
• Any list implementation could be used to implement a stack
– Arrays (static: the size of stack is given initially)
– Linked lists (dynamic: never becomes full)
16
QUICKSORT : Application of Stack
Starting with first element of list do the operations
1. Compare from right to find element less than selected
and interchange
2. Compare from left to find element greater than selected
and interchange
44,33,11,55,77,90,40,60,33,22,88,66
22,33,11,55,77,90,40,60,99,44,88,66
22,33,11,44,77,90,40,60,99,55,88,66
22,33,11,40,77,90,44,60,99,55,88,66
22,33,11,40,44,90,77,60,99,55,88,66
22,33,11,40,44,90,77,60,99,55,88,66
Two Sublists are created:
Before 44 and After 44
1717
Quick Sort Algorithm
QUICK (A, N, BEG, END, LOC)
1. [Initialize] Set LEFT= BEG, RIGHT=END and 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
iii) Go to step 3
[End of If structure]
18
3. [Scan from left to right]
a) Repeat while A[LEFT]<=A[LOC] and LEFT!=LOC
LEFT=LEFT+1
[End of loop]
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) Goto step 2
[End of If structure]
18
19
(QuickSort) This algorithm sorts an array A with N elements.
1. TOP=NULL.
2. If N>1 then TOP=TOP+1, LOWER[1]=1, UPPER[1]=N.
3. Repeat Steps 4 to 7 while TOP!=NULL.
4. Set BEG=LOWER[TOP], END=UPPER[TOP].
TOP=TOP-1
5. Call QUICK(A, N, BEG, END, LOC).
6. If BEG<LOC-1 then
TOP=TOP+1, LOWER[TOP]=BEG, UPPER[TOP]=LOC-1
[End of If structure]
7. If LOC+1<END then
TOP=TOP+1, LOWER[TOP]=LOC+1, UPPER[TOP]=END
[End of If Structure]
[End of Step 3 loop]
8. Exit.
19
20
RECURSION: Application of Stack
• To calculate factorial of a number
int fact (int n)
{
if ( n==1)
return 1;
return (n * fact (n-1) );
}
• To make Fibonacci Series is another example
of RECURSION.
21
Towers of Hanoi : Application of Stack
• Three pegs labeled A, B,C
• In A, there are finite number of disks with
decreasing size
• Objective- move the disks from A to C using B
as an auxiliary.
• The rules of the game are as follows
• Only one disk may be moved at a time.
• At no time can a larger disk be placed on a
smaller disk
22
A
B C
23
If n=3
A C A B C B A C B A B C A C
For n disks
1. Move the top n-1 disks from peg A to peg B
2. Move the top disk from peg A to C
3. Move the top n-1 disks from peg B to peg C
Tower(N-1, BEG, END, AUX)
Tower(1,BEG, AUX, END)
Tower(N-1,AUX,BEG,END)
24
Tower(4,A,B,C) A C
Tower(3, A,C,B)
Tower(3, B,A,C)
A B
Tower (2, A,B,C)
Tower(2, C,A,B)
B C
Tower(2, A,B,C)
Tower(2, B,C,A)
C B
Tower(1,C,B,A)
Tower(1,A,C,B)

Stacks in DATA STRUCTURE

  • 1.
  • 2.
    2 Stacks • A stackis a list in which insertion and deletion take place at the same end – This end is called top – The other end is called bottom
  • 3.
    3 • Stacks areknown as LIFO (Last In, First Out) lists: The last element inserted will be the first to be retrieved, using Push and Pop • Attributes of Stack – MAXSTACK: which gives the maximum number of elements that can be held by the stack. – TOP: the index of the top element of stack • Operations of Stack – push: add an element to the top of stack – pop: delete the element at the top of stack
  • 4.
    4 Array Representation ofStack • STACK: Linear Array  TOP: Pointer variable, which contains the location of top element of the stack.  MAXSTACK: A variable, which gives the maximum number of elements that can be held by the stack.  If TOP=0 or TOP=NULL Stack is empty and this condition is known as underflow.  If TOP= MAXSTACK Stack is Full and this condition is known as Overflow. STACK TOP MAXSTACK A B C 1 2 3 4 5 6 7 8 9 3 9
  • 5.
    5 Push and Popoperations on Stack PUSH(STACK,TOP,MAXSTACK,ITEM) 1. [STACK already filled] If TOP=MAXSTACK, then Print OVERFLOW and Return. 2. Set TOP=TOP+1 3. Set STACK[TOP]=ITEM. 4. Return. top empty stack A top push an element top push another A B top pop A POP(STACK,TOP,ITEM) 1. If TOP=0, then print UNDERFLOW and Return 2. Set ITEM=STACK[TOP]. 3. Set TOP=TOP-1. 4. Return.
  • 6.
    6 Linked Representation ofStacks • Linked Stack is a stack that is implemented using a singly linked list. • INFO field of the node holds the element of the stack. • LINK field of the node holds pointer to the neighboring element in the stack. 5 10 3 7 INFO LINK 5 10 3
  • 7.
    7 PUSH operation inLinked Stack 1. If AVAIL == NULL then write OVERFLOW and Exit 2. Set NEW = AVAIL and AVAIL=LINK[AVAIL] 3. Set INFO[NEW] = ITEM 4. Set LINK[NEW] = TOP 5. Set TOP = NEW 6. Exit *AVAIL is the pointer to available free nodes list.
  • 8.
    8 POP operation inLinked Stack 1. If TOP == NULL then write UNDERFLOW and Exit 2. Set ITEM = INFO[TOP] 3. Set TEMP = TOP and TOP = LINK[TOP] 4. Set LINK[TEMP]=AVAIL and AVAIL=TEMP 5. Exit *TEMP is the temporary node to save the deleted node and add it to available free nodes list.
  • 9.
    9 Arithmetic Expressions Infix, Prefixand Postfix If the operator is placed in between operands, it is called infix notation e.g. A+B, (A+B)*C, A+(B*C) Prefix notation (Polish Notation) – operators preceded the operands e.g. +AB Postfix Notation (Reverse Polish Notation) - Operator symbol is placed after the operands e.g. A+B= AB+
  • 10.
    10 Evaluation of apostfix notation 1. Add a right parenthesis “)” at the end of P. 2. Scan P from left to right and repeat Step3 and 4 for each element of P until the sentinel ”)” is encountered. 3. If an operand is encountered, push it on STACK. 4. If an operator X is encountered, then: (a) Remove the two top elements of STACK, where A is the top element and B is the next to top element. (b) Evaluate B X A. (c) Place the result of (b) back on STACK. 5. Set VALUE equal to the top element on the STACK. 6. Exit.
  • 11.
    11 Example • Consider thefollowing arithmetic expression P written in postfix notations : P : 5, 6, 2, +, *, 12, 4, /, - • We evaluate P by simulating algorithm. First we add a sentinel right parenthesis at the end of P to obtain P : 5, 6, 2, +, *, 12, 4, /, -, ) (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)
  • 12.
    12 Symbol Scanned STACK (1)5 5 (2) 6 5, 6 (3) 2 5, 6, 2 (4) + 5, 8 (5) * 40 (6) 12 40, 12 (7) 4 40, 12, 4 (8) / 40, 3 (9) - 37 (10) )
  • 13.
    13 Infix to PostfixConversion POLISH(Q,P) 1. Push “(” onto STACK and add “)” to the end of Q. 2. Scan Q from left to right and repeat step 3 to 6 for each element of Q until the STACK is empty: 3. If an operand is encountered, add it to P. 4. If a left parenthesis is encountered, push it onto STACK. 5. If an operator X is encountered , then: 1. Repeatedly pop from STACK and add to P each operator which has the same or higher precedence than X. 2. Add X to STACK. 6. If a right parenthesis is encountered, then: 1. Repeatedly pop from STACK and add to P each operator until a left parenthesis is encountered. 2. Remove the left parenthesis. 7. Exit.
  • 14.
    Example: Infix toPostfix Expression A+(B*C-(D/E↑F)*G)*H Symbol Scanned (1) A ( A (2) + ( + A (3) ( ( + ( A (4) B ( + ( A B (5) * ( + ( * A B (6) C ( + ( * A B C (7) - ( + ( - A B C * (8) ( ( + ( - ( A B C * (9) D ( + ( - ( A B C * D (10) / ( + ( - ( / A B C * D (11) E ( + ( - ( / A B C * D E (12) ↑ ( + ( - ( / ↑ A B C * D E (13) F ( + ( - ( / ↑ A B C * D E F (14) ) ( + ( - A B C * D E F ↑ / (15) * ( + ( - * A B C * D E F ↑ / (16) G ( + ( - * A B C * D E F ↑ / G (17) ) ( + A B C * D E F ↑ / G * - (18) * ( + * A B C * D E F ↑ / G * - (19) H ( + * A B C * D E F ↑ / G * - H (20) ) A B C * D E F ↑ / G * - H * +
  • 15.
    15 Stacks • Real lifeanalogy: – Dish holders (stacks) • Typical uses of stacks: – Prefix-/Postfix- calculators • Any list implementation could be used to implement a stack – Arrays (static: the size of stack is given initially) – Linked lists (dynamic: never becomes full)
  • 16.
    16 QUICKSORT : Applicationof Stack Starting with first element of list do the operations 1. Compare from right to find element less than selected and interchange 2. Compare from left to find element greater than selected and interchange 44,33,11,55,77,90,40,60,33,22,88,66 22,33,11,55,77,90,40,60,99,44,88,66 22,33,11,44,77,90,40,60,99,55,88,66 22,33,11,40,77,90,44,60,99,55,88,66 22,33,11,40,44,90,77,60,99,55,88,66 22,33,11,40,44,90,77,60,99,55,88,66 Two Sublists are created: Before 44 and After 44
  • 17.
    1717 Quick Sort Algorithm QUICK(A, N, BEG, END, LOC) 1. [Initialize] Set LEFT= BEG, RIGHT=END and 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 iii) Go to step 3 [End of If structure]
  • 18.
    18 3. [Scan fromleft to right] a) Repeat while A[LEFT]<=A[LOC] and LEFT!=LOC LEFT=LEFT+1 [End of loop] 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) Goto step 2 [End of If structure] 18
  • 19.
    19 (QuickSort) This algorithmsorts an array A with N elements. 1. TOP=NULL. 2. If N>1 then TOP=TOP+1, LOWER[1]=1, UPPER[1]=N. 3. Repeat Steps 4 to 7 while TOP!=NULL. 4. Set BEG=LOWER[TOP], END=UPPER[TOP]. TOP=TOP-1 5. Call QUICK(A, N, BEG, END, LOC). 6. If BEG<LOC-1 then TOP=TOP+1, LOWER[TOP]=BEG, UPPER[TOP]=LOC-1 [End of If structure] 7. If LOC+1<END then TOP=TOP+1, LOWER[TOP]=LOC+1, UPPER[TOP]=END [End of If Structure] [End of Step 3 loop] 8. Exit. 19
  • 20.
    20 RECURSION: Application ofStack • To calculate factorial of a number int fact (int n) { if ( n==1) return 1; return (n * fact (n-1) ); } • To make Fibonacci Series is another example of RECURSION.
  • 21.
    21 Towers of Hanoi: Application of Stack • Three pegs labeled A, B,C • In A, there are finite number of disks with decreasing size • Objective- move the disks from A to C using B as an auxiliary. • The rules of the game are as follows • Only one disk may be moved at a time. • At no time can a larger disk be placed on a smaller disk
  • 22.
  • 23.
    23 If n=3 A CA B C B A C B A B C A C For n disks 1. Move the top n-1 disks from peg A to peg B 2. Move the top disk from peg A to C 3. Move the top n-1 disks from peg B to peg C Tower(N-1, BEG, END, AUX) Tower(1,BEG, AUX, END) Tower(N-1,AUX,BEG,END)
  • 24.
    24 Tower(4,A,B,C) A C Tower(3,A,C,B) Tower(3, B,A,C) A B Tower (2, A,B,C) Tower(2, C,A,B) B C Tower(2, A,B,C) Tower(2, B,C,A) C B Tower(1,C,B,A) Tower(1,A,C,B)