SlideShare a Scribd company logo
1 of 44
STAC
K
What is a stack?
• In array insertion and deletion can take place at both end, i.e.
start and end,
• But if insertion and deletion are restricted from one end, must
used STACKS and QUEUES.
• Stack is a special type of data structure.
• Stores a set of elements in a particular order
• Compared to a container
• Stack principle: LAST IN FIRST OUT
• = LIFO
• It means: the last element inserted is the first one to
be removed
Examples:
Characteristics of a Stack
Structure
• A stack is a collection of elements, which can be
stored and retrieved one at a time.
• Elements are retrieved in reverse order of their time
of storage, i.e. the latest element stored is the next
element to be retrieved.
• A stack is sometimes referred to as a Last-In-First-
Out (LIFO) or First-In-Last-Out (FILO) structure.
Elements previously stored cannot be retrieved until
the latest element (usually referred to as the 'top'
element) has been retrieved.
▓ Stack Terminologies
▒ Push (inserting An element In Stack)
▒ Pop (Deleting an element in stack)
▒ Top ( The last entered value)
▒ Max Stack ( maximum value, a stack can hold)
▒ Isempty ( either stack has any value or not)
• Isempty()=True-> empty
• Isempty()=False-> contain elements
▒ Overflow (stack is full, elements can’t be pushed
due to absence of space)
Operations perform on stack
Primary operations: Push and Pop
Push
◦ Add an element to the top of the stack.
Pop
◦ Remove the element at the top of the stack.
Stack-Related Terms
▓ Top
░ A pointer that points the top element in the stack.
▓ Stack Underflow
░ When there is no element in the stack or stack holds
elements less than its capacity, the status of stack is
known as stack underflow.
TOP=NULL
▓ Stack Overflow
░ When the stack contains equal number of elements
as per its capacity and no more elements can be
added, the status of stack is known as stack overflow.
TOP=MAXSTK
How the stack routines work
empty stack; push(a), push(b); pop
empty stack
top = 0
a
push(a)
top = 1
b
a
push(b)
top = 2
a
pop()
top = 1
2
Top
0
1
2
3
2
Top
0
1
2
3
Poped an item
2
Top
0
1
2
3
42
2
Top
0
1
2
3
34
2
Top
0
1
2
3
52
STACK OVERFLOW
Operation performed
On stack
• Tarversing
• Insertion
• Deletion
Algorithm for Insertion
• Start
• If TOP()=maxstack();
Return TRUE
Exit
• Top=Top+1;
• Top=Top[new value];
• Exit
Algorithm for deletion
• Start
• If TOP()=isempty();
return TRUE;
exit
• Top=Top-1;
• exit
Algorithm for traversing
• Start
• Print maxstack();
• exit
EVALUATION
OF
EXPRESSION
Expression evaluation
• An expression is a series of operators and
operands
• Operators: +, -, *, /, %, ^ (exponentiation)
• Operands: the values going to be operated
• Arithmetic expression is made up
– Operands (Numeric Variables or Constants)
– Arithmetic Operators (+, -, *, /)
– Power Operator (^)
– Parentheses
• The Expression is always evaluated from left
to right
• Order in which the expression is evaluated is:
– If the expression has parenthesis, then they are
evaluated first
– Exponential (^) is given highest priority
– Multiplication (*) and division (/) have the next
highest priority
– Addition (+) and subtraction (-) have the lowest
priority
• Evaluation of Expression
Steps to evaluate the following expression:
(2^3 + 6) * 2 – 9 / 3
= (8 + 6) * 2 – 9 / 3
= 14 * 2 – 9 / 3
= 28 – 3
= 25
• Stacks are useful in evaluation of arithmetic
expressions. Consider the expression
• 5 * 3 +2 + 6 * 4
The expression can be evaluated by first
multiplying 5 and 3, storing the result in
A, adding 2 and A, saving the result in A. We
then multiply 6 and 4 and save the answer in B.
We finish off by adding A and B and leaving the
final answer in A.
• A = 15 2 + = 17
• B = 6 4 * = 24
• A = 17 24 + = 41
5 * 3 +2 + 6 * 4
• We can write this sequence of operations as follows:
5 3 * 2 + 6 4 * +
• This notation is known as postfix notation. We shall shortly
show how this form can be generated using a stack.
Basically there are 3 types of notations for expressions. The
standard form is known as the infix form. The other two are
postfix and prefix forms.
• Infix: The usual form, operator is between operands
• Prefix: the operator is in front of the operand(s)
• Postfix: the operand(s) is(are) in front of the operator
Infix, Prefix (Polish) and Postfix
(Reverse Polish) Notation
Infix Prefix
(Polish Notation)
Postfix
(Reverse Polish
Notation)
A+B +AB AB+
A*B *AB AB*
A/B /AB AB/
A-B -AB AB-
Evalution of postfix expression:
6523+8*+3+*
Infix to Postfix Conversion
**Convert infix expression
A+B*C+(D*E+F)*G
into postfix
A + B * C + ( D * E + F ) * G
1. Scanned from left to right. First
operand read is A and passed to
output
Stack
Output: A
Example (Infix to Postfix Conversion)
A + B * C + ( D * E + F ) * G
2. Next the ‘+’ operator is read, at this
stage, stack is empty. Therefore no
operators are popped and ‘+’ is
pushed into the stack. Thus the
stack and output will be:
+
Stack
Output: A
Example (Infix to Postfix Conversion)
A + B * C + ( D * E + F ) * G
3. Next the ‘B’ operand is read and
passed to the output. Thus the stack
and output will be:
+
Stack
Output: AB
Example (Infix to Postfix Conversion)
A + B * C + ( D * E + F ) * G
4. Next the ‘*’ operator is read, The
stack has ‘+’ operator which has
lower precedence than the ‘*’
operator. Therefore no operators
are popped and ‘*’ is pushed into
the stack. Thus the stack and output
will be:
*
+
Stack
Output: AB
Example (Infix to Postfix Conversion)
A + B * C + ( D * E + F ) * G
5. Next the ‘C’ operand is read and
passed to the output. Thus the stack
and output will be:
*
+
Stack
Output: ABC
Example (Infix to Postfix Conversion)
A + B * C + ( D * E + F ) * G
6. Next the ‘+’ operator is read, The
stack has ‘*’ operator which has
higher precedence than the ‘+’
operator. The Stack is popped and
passed to output. Next stack has ‘+’
operator which has same
precedence than the ‘+’ operator.
The Stack is popped and passed to
output. Now stack is empty,
therefore no operators are popped
and ‘+’ is pushed into the stack.
Thus the stack and output will be:
+
Stack
Output: ABC*+
Example (Infix to Postfix Conversion)
A + B * C + ( D * E + F ) * G
7. Next the left parenthesis ‘(’ is
read, Since all operators have lower
precedence than the left
parenthesis, it is pushed into the
stack. Thus the stack and output will
be:
(
+
Stack
Output: ABC*+
Example (Infix to Postfix Conversion)
A + B * C + ( D * E + F ) * G
8. Next the ‘D’ operand is read and
passed to the output. Thus the stack
and output will be:
(
+
Stack
Output: ABC*+D
Example (Infix to Postfix Conversion)
A + B * C + ( D * E + F ) * G
9. Next the ‘*’ operator is read. Now,
left parenthesis ‘(‘ has higher
precedence than ‘*’; it can not be
popped from the stack until a right
parenthesis ‘)’ has been read. Thus
the stack is not popped and ‘*’ is
pushed into the stack. Thus the
stack and output will be:
*
(
+
Stack
Output: ABC*+D
Example (Infix to Postfix Conversion)
A + B * C + ( D * E + F ) * G
10.Next the ‘E’ operand is read and
passed to the output. Thus the stack
and output will be:
*
(
+
Stack
Output: ABC*+DE
Example (Infix to Postfix Conversion)
A + B * C + ( D * E + F ) * G
11.Next the ‘+’ operator is read, The
stack has ‘*’ operator which has
higher precedence than the ‘+’
operator. The Stack is popped and
passed to output. Next stack has
left parenthesis ‘(’ which has not
been popped and ‘+’ operator is
pushed into the stack. Thus the
stack and output will be:
+
(
+
Stack
Output: AB*+DE*
Example (Infix to Postfix Conversion)
A + B * C + ( D * E + F ) * G
12.Next the ‘F’ operand is read and
passed to the output. Thus the stack
and output will be:
+
(
+
Stack
Output: ABC*+DE*F
Example (Infix to Postfix Conversion)
A + B * C + ( D * E + F ) * G
13.Next the ‘)’ has encountered now
popped till ‘( ‘ and passed to the
output. Thus the stack and output
will be:
+
Stack
Output: ABC*+DE*F+
Example (Infix to Postfix Conversion)
A + B * C + ( D * E + F ) * G
14.Next the ‘*’ operator is read, The
stack has ‘+’ operator which has
lower precedence than the ‘*’
operator. Therefore no operators
are popped and ‘*’ is pushed into
the stack. Thus the stack and output
will be:
*
+
Stack
Output: ABC*+DE*F+
Example (Infix to Postfix Conversion)
A + B * C + ( D * E + F ) * G
15.Next the ‘G’ operand is read and
passed to the output. Thus the stack
and output will be:
*
+
Stack
Output: ABC*+DE*F+G
Example (Infix to Postfix Conversion)
A + B * C + ( D * E + F ) * G
16.The end of expression is
encountered. The operators are
popped from the stacked and
passed to the output in the same
sequence in which these are
popped. Thus the stack and output
will be:
Stack
Output: ABC*+DE*F+G*+
Summary
• A stack is one of the most basic data structures in the study.
• It only has one end for insert and delete.
• An item in a stack is LIFO while it is FIFO in a queue
• Stack is used in many hidden areas of computer systems
• One popular important application is expression evaluation
• An expression can be in three forms: infix, prefix, and
postfix
• It is common to evaluate an expression by using a postfix
format

More Related Content

What's hot (20)

Stack application
Stack applicationStack application
Stack application
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix Notations
 
Polish
PolishPolish
Polish
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj Meena
 
Expression evaluation
Expression evaluationExpression evaluation
Expression evaluation
 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADT
 
Stack
StackStack
Stack
 
Stack
StackStack
Stack
 
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
 
Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
 
Circular queues
Circular queuesCircular queues
Circular queues
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
Queue implementation
Queue implementationQueue implementation
Queue implementation
 
Stack - Data Structure
Stack - Data StructureStack - Data Structure
Stack - Data Structure
 
Stacks
StacksStacks
Stacks
 
stack presentation
stack presentationstack presentation
stack presentation
 

Viewers also liked

Infix postfixcoversion
Infix postfixcoversionInfix postfixcoversion
Infix postfixcoversionPdr Patnaik
 
Makalah Kunjungan Binary Tree
Makalah Kunjungan Binary TreeMakalah Kunjungan Binary Tree
Makalah Kunjungan Binary TreeMuhammad Iqbal
 
Stack tumpukan
Stack tumpukan Stack tumpukan
Stack tumpukan biedoen
 
01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seoJinTaek Seo
 
MAKALAH STACK (TUMPUKAN )
MAKALAH STACK (TUMPUKAN )MAKALAH STACK (TUMPUKAN )
MAKALAH STACK (TUMPUKAN )istiqlal
 
Computer notes - Postfix
Computer notes  - PostfixComputer notes  - Postfix
Computer notes - Postfixecomputernotes
 
Evaluation of postfix expression
Evaluation of postfix expressionEvaluation of postfix expression
Evaluation of postfix expressionAkhil Ahuja
 
Infix to-postfix examples
Infix to-postfix examplesInfix to-postfix examples
Infix to-postfix examplesmua99
 
Infix prefix postfix expression -conversion
Infix  prefix postfix expression -conversionInfix  prefix postfix expression -conversion
Infix prefix postfix expression -conversionSyed Mustafa
 
Conversion from infix to prefix using stack
Conversion from infix to prefix using stackConversion from infix to prefix using stack
Conversion from infix to prefix using stackHaqnawaz Ch
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examplesgreatqadirgee4u
 
Stack data structure
Stack data structureStack data structure
Stack data structureTech_MX
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application Tech_MX
 

Viewers also liked (18)

Infix postfixcoversion
Infix postfixcoversionInfix postfixcoversion
Infix postfixcoversion
 
Makalah Kunjungan Binary Tree
Makalah Kunjungan Binary TreeMakalah Kunjungan Binary Tree
Makalah Kunjungan Binary Tree
 
Stack tumpukan
Stack tumpukan Stack tumpukan
Stack tumpukan
 
Stack
StackStack
Stack
 
01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo
 
Team 4
Team 4Team 4
Team 4
 
MAKALAH STACK (TUMPUKAN )
MAKALAH STACK (TUMPUKAN )MAKALAH STACK (TUMPUKAN )
MAKALAH STACK (TUMPUKAN )
 
Computer notes - Postfix
Computer notes  - PostfixComputer notes  - Postfix
Computer notes - Postfix
 
Evaluation of postfix expression
Evaluation of postfix expressionEvaluation of postfix expression
Evaluation of postfix expression
 
12. Stack
12. Stack12. Stack
12. Stack
 
Infix to-postfix examples
Infix to-postfix examplesInfix to-postfix examples
Infix to-postfix examples
 
Infix prefix postfix expression -conversion
Infix  prefix postfix expression -conversionInfix  prefix postfix expression -conversion
Infix prefix postfix expression -conversion
 
Conversion from infix to prefix using stack
Conversion from infix to prefix using stackConversion from infix to prefix using stack
Conversion from infix to prefix using stack
 
Infix to postfix
Infix to postfixInfix to postfix
Infix to postfix
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examples
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Infix to postfix conversion
Infix to postfix conversionInfix to postfix conversion
Infix to postfix conversion
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application
 

Similar to Stack

Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTUREMandeep Singh
 
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptxApplication of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptxPrakash Zodge
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manualnikshaikh786
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxprakashvs7
 
Data structures stacks
Data structures   stacksData structures   stacks
Data structures stacksmaamir farooq
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxchandankumar364348
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-listpinakspatel
 
Lec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxLec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxhaaamin01
 
computer notes - Data Structures - 6
computer notes - Data Structures - 6computer notes - Data Structures - 6
computer notes - Data Structures - 6ecomputernotes
 

Similar to Stack (20)

Unit 3 stack
Unit   3 stackUnit   3 stack
Unit 3 stack
 
Data structures
Data structures Data structures
Data structures
 
Stack
StackStack
Stack
 
The Stack And Recursion
The Stack And RecursionThe Stack And Recursion
The Stack And Recursion
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
DS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptxDS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptx
 
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptxApplication of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
STACK1.pptx
STACK1.pptxSTACK1.pptx
STACK1.pptx
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptx
 
5.-Stacks.pptx
5.-Stacks.pptx5.-Stacks.pptx
5.-Stacks.pptx
 
Data structures stacks
Data structures   stacksData structures   stacks
Data structures stacks
 
Stacks
StacksStacks
Stacks
 
Unit 3 stack
Unit 3   stackUnit 3   stack
Unit 3 stack
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 
Stacks
StacksStacks
Stacks
 
DS MOD2 (1) (1).pptx
DS MOD2 (1) (1).pptxDS MOD2 (1) (1).pptx
DS MOD2 (1) (1).pptx
 
Lec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxLec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptx
 
computer notes - Data Structures - 6
computer notes - Data Structures - 6computer notes - Data Structures - 6
computer notes - Data Structures - 6
 

Stack

  • 2. What is a stack? • In array insertion and deletion can take place at both end, i.e. start and end, • But if insertion and deletion are restricted from one end, must used STACKS and QUEUES. • Stack is a special type of data structure. • Stores a set of elements in a particular order • Compared to a container • Stack principle: LAST IN FIRST OUT • = LIFO • It means: the last element inserted is the first one to be removed
  • 4. Characteristics of a Stack Structure • A stack is a collection of elements, which can be stored and retrieved one at a time. • Elements are retrieved in reverse order of their time of storage, i.e. the latest element stored is the next element to be retrieved. • A stack is sometimes referred to as a Last-In-First- Out (LIFO) or First-In-Last-Out (FILO) structure. Elements previously stored cannot be retrieved until the latest element (usually referred to as the 'top' element) has been retrieved.
  • 5. ▓ Stack Terminologies ▒ Push (inserting An element In Stack) ▒ Pop (Deleting an element in stack) ▒ Top ( The last entered value) ▒ Max Stack ( maximum value, a stack can hold) ▒ Isempty ( either stack has any value or not) • Isempty()=True-> empty • Isempty()=False-> contain elements ▒ Overflow (stack is full, elements can’t be pushed due to absence of space)
  • 6. Operations perform on stack Primary operations: Push and Pop Push ◦ Add an element to the top of the stack. Pop ◦ Remove the element at the top of the stack.
  • 7.
  • 8. Stack-Related Terms ▓ Top ░ A pointer that points the top element in the stack. ▓ Stack Underflow ░ When there is no element in the stack or stack holds elements less than its capacity, the status of stack is known as stack underflow. TOP=NULL ▓ Stack Overflow ░ When the stack contains equal number of elements as per its capacity and no more elements can be added, the status of stack is known as stack overflow. TOP=MAXSTK
  • 9. How the stack routines work empty stack; push(a), push(b); pop empty stack top = 0 a push(a) top = 1 b a push(b) top = 2 a pop() top = 1
  • 15. Operation performed On stack • Tarversing • Insertion • Deletion
  • 16.
  • 17. Algorithm for Insertion • Start • If TOP()=maxstack(); Return TRUE Exit • Top=Top+1; • Top=Top[new value]; • Exit
  • 18. Algorithm for deletion • Start • If TOP()=isempty(); return TRUE; exit • Top=Top-1; • exit
  • 19. Algorithm for traversing • Start • Print maxstack(); • exit
  • 21. Expression evaluation • An expression is a series of operators and operands • Operators: +, -, *, /, %, ^ (exponentiation) • Operands: the values going to be operated • Arithmetic expression is made up – Operands (Numeric Variables or Constants) – Arithmetic Operators (+, -, *, /) – Power Operator (^) – Parentheses • The Expression is always evaluated from left to right
  • 22. • Order in which the expression is evaluated is: – If the expression has parenthesis, then they are evaluated first – Exponential (^) is given highest priority – Multiplication (*) and division (/) have the next highest priority – Addition (+) and subtraction (-) have the lowest priority • Evaluation of Expression Steps to evaluate the following expression: (2^3 + 6) * 2 – 9 / 3 = (8 + 6) * 2 – 9 / 3 = 14 * 2 – 9 / 3 = 28 – 3 = 25
  • 23. • Stacks are useful in evaluation of arithmetic expressions. Consider the expression • 5 * 3 +2 + 6 * 4 The expression can be evaluated by first multiplying 5 and 3, storing the result in A, adding 2 and A, saving the result in A. We then multiply 6 and 4 and save the answer in B. We finish off by adding A and B and leaving the final answer in A. • A = 15 2 + = 17 • B = 6 4 * = 24 • A = 17 24 + = 41
  • 24. 5 * 3 +2 + 6 * 4 • We can write this sequence of operations as follows: 5 3 * 2 + 6 4 * + • This notation is known as postfix notation. We shall shortly show how this form can be generated using a stack. Basically there are 3 types of notations for expressions. The standard form is known as the infix form. The other two are postfix and prefix forms. • Infix: The usual form, operator is between operands • Prefix: the operator is in front of the operand(s) • Postfix: the operand(s) is(are) in front of the operator
  • 25. Infix, Prefix (Polish) and Postfix (Reverse Polish) Notation Infix Prefix (Polish Notation) Postfix (Reverse Polish Notation) A+B +AB AB+ A*B *AB AB* A/B /AB AB/ A-B -AB AB-
  • 26. Evalution of postfix expression:
  • 28. Infix to Postfix Conversion **Convert infix expression A+B*C+(D*E+F)*G into postfix A + B * C + ( D * E + F ) * G 1. Scanned from left to right. First operand read is A and passed to output Stack Output: A
  • 29. Example (Infix to Postfix Conversion) A + B * C + ( D * E + F ) * G 2. Next the ‘+’ operator is read, at this stage, stack is empty. Therefore no operators are popped and ‘+’ is pushed into the stack. Thus the stack and output will be: + Stack Output: A
  • 30. Example (Infix to Postfix Conversion) A + B * C + ( D * E + F ) * G 3. Next the ‘B’ operand is read and passed to the output. Thus the stack and output will be: + Stack Output: AB
  • 31. Example (Infix to Postfix Conversion) A + B * C + ( D * E + F ) * G 4. Next the ‘*’ operator is read, The stack has ‘+’ operator which has lower precedence than the ‘*’ operator. Therefore no operators are popped and ‘*’ is pushed into the stack. Thus the stack and output will be: * + Stack Output: AB
  • 32. Example (Infix to Postfix Conversion) A + B * C + ( D * E + F ) * G 5. Next the ‘C’ operand is read and passed to the output. Thus the stack and output will be: * + Stack Output: ABC
  • 33. Example (Infix to Postfix Conversion) A + B * C + ( D * E + F ) * G 6. Next the ‘+’ operator is read, The stack has ‘*’ operator which has higher precedence than the ‘+’ operator. The Stack is popped and passed to output. Next stack has ‘+’ operator which has same precedence than the ‘+’ operator. The Stack is popped and passed to output. Now stack is empty, therefore no operators are popped and ‘+’ is pushed into the stack. Thus the stack and output will be: + Stack Output: ABC*+
  • 34. Example (Infix to Postfix Conversion) A + B * C + ( D * E + F ) * G 7. Next the left parenthesis ‘(’ is read, Since all operators have lower precedence than the left parenthesis, it is pushed into the stack. Thus the stack and output will be: ( + Stack Output: ABC*+
  • 35. Example (Infix to Postfix Conversion) A + B * C + ( D * E + F ) * G 8. Next the ‘D’ operand is read and passed to the output. Thus the stack and output will be: ( + Stack Output: ABC*+D
  • 36. Example (Infix to Postfix Conversion) A + B * C + ( D * E + F ) * G 9. Next the ‘*’ operator is read. Now, left parenthesis ‘(‘ has higher precedence than ‘*’; it can not be popped from the stack until a right parenthesis ‘)’ has been read. Thus the stack is not popped and ‘*’ is pushed into the stack. Thus the stack and output will be: * ( + Stack Output: ABC*+D
  • 37. Example (Infix to Postfix Conversion) A + B * C + ( D * E + F ) * G 10.Next the ‘E’ operand is read and passed to the output. Thus the stack and output will be: * ( + Stack Output: ABC*+DE
  • 38. Example (Infix to Postfix Conversion) A + B * C + ( D * E + F ) * G 11.Next the ‘+’ operator is read, The stack has ‘*’ operator which has higher precedence than the ‘+’ operator. The Stack is popped and passed to output. Next stack has left parenthesis ‘(’ which has not been popped and ‘+’ operator is pushed into the stack. Thus the stack and output will be: + ( + Stack Output: AB*+DE*
  • 39. Example (Infix to Postfix Conversion) A + B * C + ( D * E + F ) * G 12.Next the ‘F’ operand is read and passed to the output. Thus the stack and output will be: + ( + Stack Output: ABC*+DE*F
  • 40. Example (Infix to Postfix Conversion) A + B * C + ( D * E + F ) * G 13.Next the ‘)’ has encountered now popped till ‘( ‘ and passed to the output. Thus the stack and output will be: + Stack Output: ABC*+DE*F+
  • 41. Example (Infix to Postfix Conversion) A + B * C + ( D * E + F ) * G 14.Next the ‘*’ operator is read, The stack has ‘+’ operator which has lower precedence than the ‘*’ operator. Therefore no operators are popped and ‘*’ is pushed into the stack. Thus the stack and output will be: * + Stack Output: ABC*+DE*F+
  • 42. Example (Infix to Postfix Conversion) A + B * C + ( D * E + F ) * G 15.Next the ‘G’ operand is read and passed to the output. Thus the stack and output will be: * + Stack Output: ABC*+DE*F+G
  • 43. Example (Infix to Postfix Conversion) A + B * C + ( D * E + F ) * G 16.The end of expression is encountered. The operators are popped from the stacked and passed to the output in the same sequence in which these are popped. Thus the stack and output will be: Stack Output: ABC*+DE*F+G*+
  • 44. Summary • A stack is one of the most basic data structures in the study. • It only has one end for insert and delete. • An item in a stack is LIFO while it is FIFO in a queue • Stack is used in many hidden areas of computer systems • One popular important application is expression evaluation • An expression can be in three forms: infix, prefix, and postfix • It is common to evaluate an expression by using a postfix format