Beruflich Dokumente
Kultur Dokumente
STACK
Stack is a ordered collection of homogeneous elements where
insertion and deletion take place at one end only.
stack = last-in, first-out (LIFO) list;
A stack grows by adding an item to its top, and shrinks by
removing an item from its top, so the last item added to (or push)
the stack is always the first to be removed (or pop).
Stack is a linear data structure.
An element of the stack is termed as ITEM.
Maximum number of elements of the stack can be termed as SIZE.
Main two operations are PUSH and POP.
Stack pointer(SP) points to the top of the stack.
IMPLEMENTING STACK
By using Single Dimensional Array
By using Single linked list
STACK
TOP
Index 3
Index 2
Index 1
Index 0
2
1
STACK : push
PUSH 1
1
TOP
STACK : push
PUSH 2
2
TOP
1
STACK : push
PUSH 3
3
TOP
2
1
STACK : push
PUSH 4
4
TOP
3
2
1
STACK : push
PUSH 5
TOP
5
4
3
2
1
STACK : push
PUSH 5
5
STACK
OVERFLOW
4
3
2
1
Algorithm: STACK_push
Algorithm Stack_push(ITEM)
Input: An ITEM to insert in to the stack
Output: ITEM on top of the stack if successful
Data structure: A Stack implemented using array
Steps:
1. If TOP>=SIZE-1 then
1. Print Stack is full..No insertion is possible
2. Exit
2. Else
1. TOP=TOP+1
2. A[TOP]=ITEM
3. EndIf
4. Stop
STACK : pop
POP
TOP
4
3
2
1
STACK : pop
TOP
3
2
1
STACK : pop
POP
TOP
3
2
1
STACK : pop
TOP
2
1
STACK : pop
POP
TOP
2
1
STACK : pop
2
TOP
1
STACK : pop
POP
TOP
1
STACK : pop
TOP
STACK : pop
POP
STACK
UNDERFLOW
TOP
Algorithm: STACK_pop
Algorithm Stack_pop
Input: A stack
Output: An element is removed from top of the stack if it is not empty
Data Structure: A stack implemented using array
Steps:
1. If TOP<0
1. Print Stack is empty..No POP
2. Exit
2. Else
1. ITEM=A[TOP]
2. TOP=TOP-1
3. EndIf
4. Stop
Algorithm: STACK_status
Algorithm: Stack_status
Input: A stack
Output: states whether stack is empty or full, available free space,
item at TOP
Data Structure: A stack implemented using array
Steps:
1. If TOP<0
2. Else
1. if TOP>=size-1
1. Print stack full
2. Else
1. Print element on top is , A[TOP]
2. FREE = (SIZE TOP-1)/SIZE *100
3. Print percentage of free stack is , free
3. End if
End if
stop
Multiple Stack
In several application, more than one stack may be used together.
One stack may be empty while other overflows.
Two stacks may be implemented using a single array whose SIZE =
SIZE1+ SIZE2
One stack can grow from one end, while the other grows from the other
end
Basic operations for these stacks are same to the normal stack.
TOPx
STACKx
TOPy
STACKy
Algorithm : Multiple
stack_push
Algorithm Stack_push(ITEM,NO)
Input: An ITEM to insert
Output: ITEM on top if successful
Data structure: A Stack , A[MAX]
1.
2.
3.
Steps:
4.
5.
If NO=1
1. If TOP1>=SIZE1-1 then
1. Print Stack is
full..No insertion is
possible
2. Exit
2. Else
1. TOP1=TOP1+1
2. A[TOP1]=ITEM
3. EndIf
Else
If NO=2
1. If TOP2<=(MAXSIZE2)
1. Print Stack is
full..No insertion is
possible
2. Exit
2. Else
1. TOP2=TOP2-1
2. A[TOP2]=ITEM
3. EndIf
End if
Stop
Algorithm : Multiple
stack_pop
Algorithm MStack_pop(NO)
Input: A stack
Output:element removed from
top
Data Structure: A stack, A[MAX]
1.
2.
3.
Steps:
4.
5.
If NO=1
1. If TOP1<0
1. Print Stack1 is
empty..No POP
2. Exit
2. Else
1. ITEM=A[TOP1]
2. TOP1=TOP1-1
3. EndIf
Else
If NO=2
1. If TOP2>MAX
1. Print Stack1 is
empty..No POP
2. Exit
2. Else
1. ITEM=A[TOP2]
2. TOP2=TOP2+1
3. EndIf
End if
Stop
Applications of stack
String Reversal
Infix to postfix conversion
Postfix evaluation
Output
Output
stack
TOP
Output
stack
TOP
Output
H
stack
TOP
Output
E
H
stack
TOP
Output
L
E
H
stack
TOP
Output
L
L
E
H
stack
TOP
Output
O
L
L
E
H
stack
TOP
Application: String
Reversal
Input String complete
H E L L O
So start popping
Input
Output
O
L
L
E
H
stack
TOP
Input
Output
L
L
E
H
stack
TOP
O L
Input
Output
L
E
H
stack
TOP
O L L
Input
Output
E
H
stack
TOP
O L L E
Input
Output
H
stack
TOP
O L L E H
Input
Output
STACK EMPTY
stack
TOP
Applications of stack
String Reversal
Infix to postfix conversion
Postfix evaluation
Notations
Three types of notation:
1. Infix notation: operator is written between the
operands (A+B)
2. Prefix notation: operator is written before the
operands, also called polish notation (+AB)
3. Postfix notation: operator is written after operands,
also known as suffix or reverse Polish notation (AB+)
Advantages of using postfix notation:
Human beings are quite used to work with infix notation, but
infix is much complex and required to remember set of
rules. (E.g. precedence and associativity)
Postfix is much easy to work, and no need for operator
precedence and other rules.
Algorithm
1)Push ( onto STACK and add ) to the end of
Q
2)Scan Q from left to right and repeat steps 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
Algorithm
5) If an operator # is encountered, then
a) Repeatedly pop from stack and which has
same or higher precedence than #add to P each
operator
b) Add # to STACK
6) If a right parenthesis is encountered, then
a) Reapeatedly pop from STACK and add to P each
operator until a left parenthesis is encountered
b) Remove the left parenthesis. [Do not add ( to P]
7) exit
Stack
Output
Empty
23
23*
/(
23*
/(
23*2
/(/(-
23*2
23*21
23*21-
23*21-/
5
*
+
+*
23*21-/5
23*21-/53
+*
23*21-/53
Empty
23*21-/53*+
So, the Postfix Expression is 23*21-/53*+
infix to postfix
Question: convert
A-B/(C*D^E)
infix to postfix
Easy method:manual conversion
(A-(B/(C*(D^E))))
infix to postfix
Easy
(A(A(A(A(A(A(AA
method:
(B/(C
(B/(C
(B/(C
(B/ C
(B/ C
B
C
B
C
B
C
manual conversion
*(D^E))))
* D E ^) ) )
* D E ^) ) )
D E ^* ) )
D E ^* ) )
D E ^* / )
D E ^* / )
D E ^* / -
infix to postfix
Implementation :
A-B/C^D*E
Input- Infix expression
infix to postfix
Implementation :
A-B/C^D*E
Input- Infix expression
TOP
infix to postfix
Implementation :
A-B/C^D*E
Input- Infix expression
A
Output- Postfix expression
TOP
infix to postfix
Implementation :
A
A-B/C^D*E
TOP
infix to postfix
Implementation :
AB
A-B/C^D*E
TOP
infix to postfix
Implementation :
AB
A-B / C^D*E
TOP
/
-
Topstack has
less
precedence
so push to
stack
infix to postfix
Implementation :
ABC
A-B / C^D*E
TOP
/
-
infix to postfix
Implementation :
ABC
A-B / C^D*E
TOP
^
/
-
Topstack has
less
precedence
so push to
stack
infix to postfix
Implementation :
ABCD
A-B / C^D*E
TOP
^
/
-
infix to postfix
Implementation :
ABCD
A-B / C^D*E
TOP
^
/
-
Topstack has
more
precedence
so POP stack
infix to postfix
Implementation :
ABCD^
A-B / C^D*E
TOP
/
-
Topstack has
more
precedence
so POP stack
infix to postfix
Implementation :
ABCD^/
A-B / C^D*E
TOP
*
-
Topstack has
less
precedence
so PUSH to
stack
infix to postfix
Implementation :
ABCD^/E
A-B / C^D*E
TOP
*
-
infix to postfix
Implementation :
ABCD^E
A-B / C^D*E
TOP
*
-
Reached end
of string, so
POP till stack
is empty
infix to postfix
Implementation :
ABCD^/E*
A-B / C^D*E
TOP
infix to postfix
Implementation :
A-B / C^D*E
Input- Infix expression
Stack empty
Conversion complete
TOP
infix:
A-B/C^D*E
Postfix: A B C D ^ / E * -
Precedence Table
symbol
In-stack
Priority
value
In-coming
Priority
value
+-
*/
operand
3
5
3
5
3
5
5*3
15
2
15
15+2
17
Expression
complete
Pop the final result
17
53*2+ = 17
Algorithm POSTFIX-EVAL
Input: An expression in postfix form
Output: Result of the expression
Data Structure: A Stack to store the operand and intermediate result and an array
Steps:
1. Postfix=READ(expression)
2. Length=STRLENGTH(expression),i=0
3. While(i<length)
1. Ch=postfix[i]
2. If(ch=operand)
1. PUSH(ch)
3. Else
1. Operator=ch
2. Oprnd2=POP()
3. Oprnd1=POP()
4. PERFORM (Result=oprnd1 operator oprnd2)
PUSH(result)
EndIf
i=i+1;
EndWhile
Result=POP()
1. Stop
Assignment Questions
1. Give an application of stack data structure.
1.
String Reversal
2. Infix to postfix conversion
3. Postfix evaluation