0% found this document useful (0 votes)
2 views

Stack Data Structure with example

The document provides a comprehensive overview of stacks, a data structure that operates on a last-in-first-out (LIFO) principle, detailing its operations such as push, pop, and checks for emptiness and fullness. It contrasts stacks with arrays, explains stack implementation using arrays or linked lists, and discusses applications including expression evaluation and parenthesis checking. Additionally, it covers the conversion of infix expressions to postfix notation and the evaluation of postfix expressions using stacks.

Uploaded by

kanimozhisasi345
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Stack Data Structure with example

The document provides a comprehensive overview of stacks, a data structure that operates on a last-in-first-out (LIFO) principle, detailing its operations such as push, pop, and checks for emptiness and fullness. It contrasts stacks with arrays, explains stack implementation using arrays or linked lists, and discusses applications including expression evaluation and parenthesis checking. Additionally, it covers the conversion of infix expressions to postfix notation and the evaluation of postfix expressions using stacks.

Uploaded by

kanimozhisasi345
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

IMPLEMENTATION OF STACKS

INTRODUCTION ABOUT STACKS


Stacks hold objects, usually all of the same type.
 It follows the concept of LIFO – last in first out.
 Stack is a linear list of items in which all additions and deletion are
restricted to one end.
STACKS VS ARRAYS
 An array is a contiguous block of memory.
 A stack is a first-in-last-out data structure with access only to the top
of the data.
 The values can be added and deleted on any side from an array.
 But in stack, insertion and deletion is possible on only one side of the
stack. The other side is sealed.
Eg: a[10] –array a[10] - stack
STACK OPERATIONS
The bottom of a stack is a sealed end. Stack may have a capacity which is
a limitation on the number of elements in a stack. The operations on stack
are
• Push: Places an object on the top of the stack.
• Pop: Removes an object from the top of the stack.
• IsEmpty: Reports whether the stack is empty or not.
• IsFull: Reports whether the stack exceeds limit or not.
c
b
a a

(i)stack (ii)push(s,a) (iii)push(s,d)-stack overflow

(iv)pop(s)-stack underflow
STACK IMPLEMENTATION
 Stack data structure is not inherently provided by many programming
languages.
 Stack is implemented using arrays or linked lists.
 Let S be a stack, n be the capacity, x be the element to be pushed, then
push and pop will be given as
Push(S,x) and Pop(S)
 Here we use “top” which keeps track of the top element in the stack.
 When top = = -1 , and pop() operation gives stack underflow as result.
 When top = = n-1, and push() operation gives stack overflow as result.
 The pop() operation just gives an illusion of deletion, but the elements
are retained. Only the top is decremented.
S[0:5] Pop(S) a b c d
e Top=4

Push(S,a a Pop(S) a
) Pop(S) Top=0
Top=0 Pop(S)
Pop(S)
Push(S,a) a b c d e
Push(S,b) f
Push(S,c) Top=5 Pop(S)
push(S,d) Top=-1
Push(S,e)
push(S,f)
Push(S,g a b c d e Pop(S)
) f
Top=5 = n-1 Top=-1 stack underflow
Stack overflow
STACK APPLICATIONS
 Evaluation of expression
Conversion of infix to postfix expression
Computation of postfix expression
 Parenthesis handling
 Backtracking
Conversion of decimal to other number system
Maze tracer
Undo operations
PARENTHESIS CHECKING
Procedure check()
Declare a character stack S.
Now traverse the expression.
a) If the current character is a starting bracket then push it to
stack.
b) If the current character is a closing bracket then pop from
stack and if the popped character is the matching starting bracket
then fine else parenthesis are not balanced.
After complete traversal, if there is some starting bracket left in stack
then “not balanced”
End procedure
Eg: [a+(b*c)+{(d-e)}]

[ Push [

[ ( Push (

[ ) and ( matches, Pop (

[ { Push {

[ { ( Push (

matches, pop (
[ {

[ Matches, pop {

Matches, pop [

Thus, parenthesis match here


Infix expression
 Infix notation: X + Y

 Operators are written in-between their

operands.
 This is the usual way we write expressions.

 An expression such as A * ( B + C ) / D is

usually taken to mean something like: "First


add B and C together, then multiply the
result by A, then divide by D to give the final
answer."
Postfix Expression
 Postfix notation (also known as "Reverse

Polish notation"): X Y +
 Operators are written after their operands.

 The order of evaluation of operators is always

left-to-right
Prefix Expression
 Prefix notation (also known as "Polish

notation"): + X Y
INFIX TO POSTFIX CONVERSION
 Scan the Infix expression left to right
 If the character x is an operand
 Output the character into the Postfix Expression
 If the character x is a left or right parenthesis
 If the character is “(“
 Push it into the stack
 if the character is “)”
 Repeatedly pop and output all the operators/characters
until “(“ is popped from the stack.
 If the character x is a is a regular operator
 Step 1: Check the character y currently at the top of the
stack.
 Step 2: If Stack is empty or y=‘(‘ or y is an operator of

lower precedence than x, then push x into stack.


 Step 3: If y is an operator of higher or equal
precedence than x, then pop and output y and push x into
the stack.

 When all characters in infix expression are processed repeatedly pop


the
INFIXStack
TO POSTFIX CONVERSION
Infix Expression
(a+b-c)*d–(e+f)

Postfix Expression
INFIXStack
TO POSTFIX CONVERSION
Infix Expression
a+b-c)*d–(e+f)

Postfix Expression

(
INFIXStack
TO POSTFIX CONVERSION
Infix Expression
+b-c)*d–(e+f)

Postfix Expression
a

(
INFIXStack
TO POSTFIX CONVERSION
Infix Expression
b-c)*d–(e+f)

Postfix Expression
a

+
(
INFIXStack
TO POSTFIX CONVERSION
Infix Expression
-c)*d–(e+f)

Postfix Expression
ab

+
(
INFIXStack
TO POSTFIX CONVERSION
Infix Expression
c)*d–(e+f)

Postfix Expression
ab+

-
(
INFIXStack
TO POSTFIX CONVERSION
Infix Expression
)*d–(e+f)

Postfix Expression
ab+c

-
(
INFIXStack
TO POSTFIX CONVERSION
Infix Expression
*d–(e+f)

Postfix Expression
ab+c-
INFIXStack
TO POSTFIX CONVERSION
Infix Expression
d–(e+f)

Postfix Expression
ab+c-

*
INFIXStack
TO POSTFIX CONVERSION
Infix Expression
–(e+f)

Postfix Expression
ab+c-d

*
INFIXStack
TO POSTFIX CONVERSION
Infix Expression
(e+f)

Postfix Expression
ab+c–d*

-
INFIXStack
TO POSTFIX CONVERSION
Infix Expression
e+f)

Postfix Expression
ab+c–d*

(
-
INFIXStack
TO POSTFIX CONVERSION
Infix Expression
+f)

Postfix Expression
ab+c–d*e

(
-
INFIXStack
TO POSTFIX CONVERSION
Infix Expression
f)

Postfix Expression

+ ab+c–d*e

(
-
INFIXStack
TO POSTFIX CONVERSION
Infix Expression
)

Postfix Expression

+ ab+c–d*ef

(
-
INFIXStack
TO POSTFIX CONVERSION
Infix Expression

Postfix Expression
ab+c–d*ef+

-
INFIXStack
TO POSTFIX CONVERSION
Infix Expression

Postfix Expression
ab+c–d*ef+-
Eg: (A*B)+C

Element Stack Prefix Action

( ( Push (

A ( A Print A

* ( * Push(*)

B ( * AB Print B

) AB* Pop *,( print *

+ Push +
+
C + AB*C Print C

$ AB*C+ Pop +
 Convert infix expression to postfix
expression
( ( A + B ) — C * ( D / E ) ) + F

 ( ( A + B ) * ( C – D ) + E ) / (F + G)
EVALUATION OF POSTFIX EXPRESSION
 Postfix expression is easily evaluated by the compiler by using stack.
 The procedure for the evaluation of postfix expression is given below:
procedure eval(E)
x=getnextchar(E);
case x:
x is an operand: push x into stack S.
x is an operator: pop elements, perform operation and
push result into stack.
x is null: pop stack and print result
end case
End procedure
Eg: AB*C+ A=2, B=3, C=5

Element Stack Action


A Push A
A
B Push B
A B
* Pop A and B,
6 A*B, push 6

C 6 Push C
C
+ Pop C and 6,
1
C+6, push 11
1

Result:11
POSFIX EVALUATION
231*+9–
4 5 7 2 + – *

3 4 + 2 * 7 /

You might also like