Sie sind auf Seite 1von 84

STACK

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

Operations on the stack


PUSH: Insertion
Storing the element to the stack to the location SP points to
after incrementing the SP.
POP: Deletion
Deleting one item from the top of the stack.
ISEMPTY
Returns true or false after checking the stack is empty or not.
ISFULL
Returns true or false after checking the stack is full or not.
TRAVERSE
Display all elements in the stack

STACK
TOP
Index 3

Index 2

Index 1
Index 0

2
1

STACK : push & pop


PUSH
To insert an item into the stack
The index of the stack varies from 0 to SIZE-1
TOP point to the current top most item in the stack
POP
This operation is the remove an item from the top of the stack

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

1. Print stack empty

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

Application: String Reversal


H E L L O
Input

Output

Application: String Reversal


H E L L O
Input

Output

stack

TOP

Application: String Reversal


H E L L O
Input

Output

stack

TOP

Application: String Reversal


H E L L O
Input

Output

H
stack

TOP

Application: String Reversal


H E L L O
Input

Output

E
H
stack

TOP

Application: String Reversal


H E L L O
Input

Output

L
E
H
stack

TOP

Application: String Reversal


H E L L O
Input

Output

L
L
E
H
stack

TOP

Application: String Reversal


H E L L O
Input

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

Application: String Reversal


H E L L O

Input

Output

L
L
E
H
stack

TOP

Application: String Reversal


H E L L O

O L

Input

Output

L
E
H
stack

TOP

Application: String Reversal


H E L L O

O L L

Input

Output

E
H
stack

TOP

Application: String Reversal


H E L L O

O L L E

Input

Output

H
stack

TOP

Application: String Reversal


H E L L O

O L L E H

Input

Output

STACK EMPTY

stack

TOP

Applications of stack

String Reversal
Infix to postfix conversion
Postfix evaluation

What is prefix, infix and postfix ?

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.

Infix to postfix expression


Q-> arithmetic expression in infix notation
The following algorithm uses a stack to

temporarily hold operators and left


parenthesis.
Postfix expression P will be constructed from

left to right using the operands in Q and


operators which are removed from STACK

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

Suppose we want to convert 2*3/(2-1)+5*3 into Postfix form,


Expression

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

Output- Postfix expression

infix to postfix
Implementation :
A-B/C^D*E
Input- Infix expression

Output- Postfix 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

Output- Postfix expression

Input- Infix expression

TOP

infix to postfix
Implementation :
AB

A-B/C^D*E

Output- Postfix expression

Input- Infix expression

TOP

infix to postfix
Implementation :
AB

A-B / C^D*E

Output- Postfix expression

Input- Infix expression

TOP

/
-

Topstack has
less
precedence
so push to
stack

infix to postfix
Implementation :
ABC

A-B / C^D*E

Output- Postfix expression

Input- Infix expression

TOP

/
-

infix to postfix
Implementation :
ABC

A-B / C^D*E

Output- Postfix expression

Input- Infix expression

TOP

^
/
-

Topstack has
less
precedence
so push to
stack

infix to postfix
Implementation :
ABCD

A-B / C^D*E

Output- Postfix expression

Input- Infix expression

TOP

^
/
-

infix to postfix
Implementation :
ABCD

A-B / C^D*E

Output- Postfix expression

Input- Infix expression

TOP

^
/
-

Topstack has
more
precedence
so POP stack

infix to postfix
Implementation :
ABCD^

A-B / C^D*E

Output- Postfix expression

Input- Infix expression

TOP

/
-

Topstack has
more
precedence
so POP stack

infix to postfix
Implementation :
ABCD^/

A-B / C^D*E

Output- Postfix expression

Input- Infix expression

TOP

*
-

Topstack has
less
precedence
so PUSH to
stack

infix to postfix
Implementation :
ABCD^/E

A-B / C^D*E

Output- Postfix expression

Input- Infix expression

TOP

*
-

infix to postfix
Implementation :
ABCD^E

A-B / C^D*E

Output- Postfix expression

Input- Infix expression

TOP

*
-

Reached end
of string, so
POP till stack
is empty

infix to postfix
Implementation :
ABCD^/E*

A-B / C^D*E

Output- Postfix expression

Input- Infix expression

TOP

infix to postfix
Implementation :
A-B / C^D*E
Input- Infix expression

ABCD^/E*Output- Postfix 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

Postfix expression Evaluation


Postfix expression: 53*2+

Postfix expression Evaluation


Postfix expression: 53*2+

Postfix expression Evaluation


Postfix expression: 53*2+

3
5

Postfix expression Evaluation


Postfix expression: 53*2+

3
5

Postfix expression Evaluation


Postfix expression: 53*2+

3
5

Postfix expression Evaluation


Postfix expression: 53*2+

5*3

Postfix expression Evaluation


Postfix expression: 53*2+

15

Postfix expression Evaluation


Postfix expression: 53*2+

2
15

Postfix expression Evaluation


Postfix expression: 53*2+

15+2

Postfix expression Evaluation


Postfix expression: 53*2+

17

Postfix expression Evaluation


Postfix expression: 53*2+

Expression
complete
Pop the final result
17

53*2+ = 17

Postfix expression Evaluation


Evaluation of postfix expression also maintains stack but here stack
contains the operands instead of operators.
1.
2.
3.
4.
5.

Scan the postfix from left to right


If the symbol is operand, push it into the stack.
If the symbol is operator, pop last two elements of stack
and evaluate, after evaluation result push into the stack.
Continue this process until the end of the expression.
Finally pop the final result from the stack.

Algorithm: Postfix evaluation

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

2. Write algorithms to implement 2 stacks in a single array to use


same STACKFULL for the 2 stacks.
Multiple stacks

3. What is meant by stack? Implement stack using array. Write the 5


functions/ algorithms to perform different operations on stack. Give
its application.
1. What is a stack?
2. LIFO
3. Functions push(), pull(), status(), isempty(), isfull()

Das könnte Ihnen auch gefallen