Stack ADT
1 Mrs.K.Arunasakthi, AP/CSE
21CS202 - STACK ADT
Mrs. K.Arunasakthi
Assistant Professor
Computer Science & Engineering,
Velammal college of Engineering & Technology, Madurai
Stack
2
 Basic principles
 Operation of stack
 Stack using Array
 Stack using Linked List
 Applications of stack
Mrs.K.Arunasakthi, AP/CSE
21CS202 - STACK ADT
Basic Idea
• A stack is an Abstract Data Type (ADT), commonly used in most
programming languages. It is named stack as it behaves like a real-
world stack, for example – a deck of cards or a pile of plates, etc.
3 Mrs.K.Arunasakthi, AP/CSE
21CS202 - STACK ADT
Stack Representation
• Can be implemented by means of Array, Structure, Pointers and
Linked List.
• Stack can either be a fixed size or dynamic.
4 Mrs.K.Arunasakthi, AP/CSE
21CS202 - STACK ADT
5
STACK
push
create
pop
isfull
isempty
Mrs.K.Arunasakthi, AP/CSE
21CS202 - STACK ADT
STACK: Last-In-First-Out (LIFO)
• void push (stack *s, int element);
/* Insert an element in the stack */
• int pop (stack *s);
/* Remove and return the top element */
• void create (stack *s);
/* Create a new stack */
• int isempty (stack *s);
/* Check if stack is empty */
• int isfull (stack *s);
/* Check if stack is full */
6
Assumption: stack contains integer elements!
Mrs.K.Arunasakthi, AP/CSE
21CS202 - STACK ADT
Stack using Array
7 Mrs.K.Arunasakthi, AP/CSE
21CS202 - STACK ADT
8
top
top
PUSH
Push using Stack
Mrs.K.Arunasakthi, AP/CSE
21CS202 - STACK ADT
9
top
top
POP
Pop using Stack
Mrs.K.Arunasakthi, AP/CSE
21CS202 - STACK ADT
• In the array implementation, we would:
• Declare an array of fixed size (which determines the maximum
size of the stack).
• Keep a variable which always points to the “top” of the stack.
• Contains the array index of the “top” element.
• In the linked list implementation, we would:
• Maintain the stack as a linked list.
• A pointer variable top points to the start of the list.
• The first element of the linked list is considered as the stack top.
10
Basic Idea
Mrs.K.Arunasakthi, AP/CSE
21CS202 - STACK ADT
Stack Using Array
11
Declaration & Creation of Stack
#define MAX 100
int stack[MAX];
// int stack[100];
int top =-1;
Mrs.K.Arunasakthi, AP/CSE
21CS202 - STACK ADT
Pushing an element into stack
12
void push(int st[], int val)
{
if(top == MAX-1)
{
printf("n STACK OVERFLOW");
}
else
{
top++;
st[top] = val;
}
}
 Process of inserting an
element into the stack.
 For inserting an
element Top will be
incremented by 1
 Element should be
pushed on to the top of
the stack.i,e.,
st[top]=val
 Top=Top+1 // Top++
Mrs.K.Arunasakthi, AP/CSE
21CS202 - STACK ADT
Popping an element from stack
13
int pop(int st[])
{
int val;
if(top == -1)
{
printf("n STACK UNDERFLOW");
return -1;
}
else
{
val = st[top];
top--;
return val;
}
}
 Pop is the process of
removing the top element
from the stack.
 After removing top should
be decremented by 1
 top=top-1 or top--;
Mrs.K.Arunasakthi, AP/CSE
21CS202 - STACK ADT
Checking stack- Full or Empty
14
int isempty(int st[])
{
if(top == -1)
{
printf("n STACK IS EMPTY")
return -1;
}
}
EMPTY
Checking that whether the
given stack is empty
FULL
Checking that whether the given
stack is empty
int isfull(int st[])
{
if (top == MAX-1)
printf(“STACK IS FULL”);
else
return (0);
}
Mrs.K.Arunasakthi, AP/CSE
21CS202 - STACK ADT
Display the Stack and PEEK
15
void display(int st[])
{
int i;
if(top == -1)
printf("n STACK IS EMPTY");
else
{
for(i=top;i>=0;i--)
printf("n %d",st[i]);
printf("n"); // Added for
formatting purposes
}
}
DISPLAY
Process of displaying all the
elements in the stack.
FULL
Displaying the Top element of
the Stack.
int peek(int st[])
{
if(top == -1)
{
printf("n STACK IS EMPTY");
return -1;
}
else
return (st[top]);
}
Mrs.K.Arunasakthi, AP/CSE
21CS202 - STACK ADT
Stack using Linked List
16 Mrs.K.Arunasakthi, AP/CSE
21CS202 - STACK ADT
17
top
PUSH OPERATION
Push using Linked List
Mrs.K.Arunasakthi, AP/CSE
21CS202 - STACK ADT
18
top
POP OPERATION
Pop using Linked List
Mrs.K.Arunasakthi, AP/CSE
21CS202 - STACK ADT
Declaration – Stack using Linked List
19
struct lifo
{
int value;
struct lifo *next;
};
typedef struct lifo
stack;
stack *top;
Mrs.K.Arunasakthi, AP/CSE
21CS202 - STACK ADT
Stack Creation
20
void create (stack **top)
{
*top = NULL;
/* top points to NULL,
indicating empty
stack */
}
LINKED LIST
Mrs.K.Arunasakthi, AP/CSE
21CS202 - STACK ADT
Pushing an element into stack
21
void push (stack **top, int element)
{
stack *new;
new = (stack *)malloc (sizeof(stack));
if (new == NULL)
{
printf (“n Stack is full”);
exit(-1);
}
new->value = element;
new->next = *top;
*top = new;
}
LINKED LIST
Mrs.K.Arunasakthi, AP/CSE
21CS202 - STACK ADT
Popping an element from stack
22
int pop (stack **top)
{
int t;
stack *p;
if (*top == NULL)
{
printf (“n Stack is empty”);
exit(-1);
}
else
{
t = (*top)->value;
p = *top;
*top = (*top)->next;
free (p);
return t;
}
}
LINKED LIST
Mrs.K.Arunasakthi, AP/CSE
21CS202 - STACK ADT
Checking for stack empty
23
LINKED LIST
int isempty (stack *top)
{
if (top == NULL)
return (1);
else
return (0);
}
Mrs.K.Arunasakthi, AP/CSE
21CS202 - STACK ADT
Example: A Stack using an Array
24
#include <stdio.h>
#define MAXSIZE 100
struct lifo
{
int st[MAXSIZE];
int top;
};
typedef struct lifo stack;
main() {
stack A, B;
create(&A);
create(&B);
push(&A,10);
push(&A,20);
push(&A,30);
push(&B,100);
push(&B,5);
printf (“%d %d”, pop(&A), pop(&B));
push (&A, pop(&B));
if (isempty(&B))
printf (“n B is empty”);
return;
}
Mrs.K.Arunasakthi, AP/CSE
21CS202 - STACK ADT
Example: A Stack using Linked List
25
#include <stdio.h>
struct lifo
{
int value;
struct lifo *next;
};
typedef struct lifo stack;
main() {
stack *A, *B;
create(&A);
create(&B);
push(&A,10);
push(&A,20);
push(&A,30);
push(&B,100);
push(&B,5);
printf (“%d %d”, pop(&A), pop(&B));
push (&A, pop(&B));
if (isempty(B))
printf (“n B is empty”);
return;
}
Mrs.K.Arunasakthi, AP/CSE
21CS202 - STACK ADT
Applications of Stacks
• Direct applications:
• Page-visited history in a Web browser
• Undo sequence in a text editor
• Chain of method calls in the Java Virtual Machine
• Validate XML
• Reversing a List, Paranthesis checker, Tower of hanoi
• Indirect applications:
• Auxiliary data structure for algorithms
• Component of other data structures
26 Mrs.K.Arunasakthi, AP/CSE
21CS202 - STACK ADT
Infix and Postfix Notations
• Infix: operators placed between operands:
A+B*C
• Postfix: operands appear before their operators:-
ABC*+
• There are no precedence rules to learn in postfix notation, and
parentheses are never needed
27 Mrs.K.Arunasakthi, AP/CSE
21CS202 - STACK ADT
Infix Postfix
A + B A B +
A + B * C A B C * +
(A + B) * C A B + C *
A + B * C + D A B C * + D +
(A + B) * (C + D) A B + C D + *
A * B + C * D A B * C D * +
A + B * C  (A + (B * C))  (A + (B C *) )  A B C * +
A + B * C + D  ((A + (B * C)) + D )  ((A + (B C*) )+ D) 
((A B C *+) + D)  A B C * + D +
28
Infix to Postfix
Mrs.K.Arunasakthi, AP/CSE
21CS202 - STACK ADT
Infix to postfix conversion
• Use a stack for processing operators (push and pop operations).
• Scan the sequence of operators and operands from left to right and
perform one of the following:
• output the operand,
• push an operator of higher precedence,
• pop an operator and output, till the stack top contains operator of a lower
precedence and push the present operator.
29 Mrs.K.Arunasakthi, AP/CSE
21CS202 - STACK ADT
The algorithm steps
1. Print operands as they arrive.
2. If the stack is empty or contains a left parenthesis on top, push the incoming operator
onto the stack.
3. If the incoming symbol is a left parenthesis, push it on the stack.
4. If the incoming symbol is a right parenthesis, pop the stack and print the operators
until you see a left parenthesis. Discard the pair of parentheses.
5. If the incoming symbol has higher precedence than the top of the stack, push it on the
stack.
6. If the incoming symbol has equal precedence with the top of the stack, use association.
If the association is left to right, pop and print the top of the stack and then push the
incoming operator. If the association is right to left, push the incoming operator.
7. If the incoming symbol has lower precedence than the symbol on the top of the stack,
pop the stack and print the top operator. Then test the incoming operator against the
new top of stack.
8. At the end of the expression, pop and print all operators on the stack. (No parentheses
should remain.)
30 Mrs.K.Arunasakthi, AP/CSE
21CS202 - STACK ADT
Infix to Postfix Conversion
Requires operator precedence information
Operands:
Add to postfix expression.
Close parenthesis:
pop stack symbols until an open parenthesis appears.
Operators:
Pop all stack symbols until a symbol of lower precedence appears. Then push
the operator.
End of input:
Pop all remaining stack symbols and add to the expression.
31 Mrs.K.Arunasakthi, AP/CSE
21CS202 - STACK ADT
Current
symbol
Operator
Stack
Postfix string
1 A A
2 * * A
3 ( * ( A
4 B * ( A B
5 + * ( + A B
6 C * ( + A B C
7 * * ( + * A B C
8 D * ( + * A B C D
9 ) * A B C D * +
10 + + A B C D * + *
11 E + A B C D * + * E
12 A B C D * + * E +
Expression:
A * (B + C * D) + E
becomes
A B C D * + * E +
Postfix notation
is also called as
Reverse Polish
Notation (RPN)
32
Infix to Postfix Rules
Mrs.K.Arunasakthi, AP/CSE
21CS202 - STACK ADT
Mrs.K.Arunasakthi, AP/CSE
21CS202 - STACK ADT 33

More Related Content

PPT
Abstract data types
PPT
Divide and conquer
PPTX
Pointer in c
PPTX
Control statements in c
PPTX
Asymptotic notations
PPTX
Linear search-and-binary-search
PPT
Error Detection And Correction
PDF
Automata theory
Abstract data types
Divide and conquer
Pointer in c
Control statements in c
Asymptotic notations
Linear search-and-binary-search
Error Detection And Correction
Automata theory

What's hot (20)

PPT
Stacks
PPSX
Data Structure (Queue)
PPT
Expression evaluation
PPTX
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
PPTX
queue & its applications
PPT
PPTX
Infix to postfix conversion
PPTX
Stacks IN DATA STRUCTURES
PPTX
Evaluation of postfix expression
PPT
PDF
Python programming : Arrays
PDF
Array data structure
PPT
Multidimensional array in C
PPTX
Queue ppt
PPTX
stack & queue
PPTX
Doubly Linked List
PPTX
Circular link list.ppt
PPTX
Data Structures (CS8391)
PPTX
single linked list
PPTX
Stacks
Data Structure (Queue)
Expression evaluation
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
queue & its applications
Infix to postfix conversion
Stacks IN DATA STRUCTURES
Evaluation of postfix expression
Python programming : Arrays
Array data structure
Multidimensional array in C
Queue ppt
stack & queue
Doubly Linked List
Circular link list.ppt
Data Structures (CS8391)
single linked list
Ad

Similar to Stack ADT (20)

PPTX
13 Stacks and Queues.pptx
PPTX
Stack & Queue in Data Structure and Algorithms
PDF
Data structure lab manual
PDF
Data structures stacks
PPT
Stack linked list
PPTX
STACK1.pptx
PDF
stacks and queues
PPTX
introduction of the Stacks and Queues.pptx
PPTX
Stack - Data Structure - Notes
PDF
Stacks
PPTX
DSA_Unit3_ Stacks and Queues using array (1).pptx
PPTX
Applicationofstack by Ali F.RAshid
PPTX
Application of Stack - Yadraj Meena
PPTX
Implementation of stacks and queues in C
PPTX
Stacks and Queue - Data Structures
PPTX
Data structures1
PPT
Unit 3 stack
PPTX
Lec5-Stack-bukc-28022024-112316am (1) .pptx
PPT
Stack ppt file of Stack DSA For lab in the lab of DSA lecture and Lab.ppt
13 Stacks and Queues.pptx
Stack & Queue in Data Structure and Algorithms
Data structure lab manual
Data structures stacks
Stack linked list
STACK1.pptx
stacks and queues
introduction of the Stacks and Queues.pptx
Stack - Data Structure - Notes
Stacks
DSA_Unit3_ Stacks and Queues using array (1).pptx
Applicationofstack by Ali F.RAshid
Application of Stack - Yadraj Meena
Implementation of stacks and queues in C
Stacks and Queue - Data Structures
Data structures1
Unit 3 stack
Lec5-Stack-bukc-28022024-112316am (1) .pptx
Stack ppt file of Stack DSA For lab in the lab of DSA lecture and Lab.ppt
Ad

Recently uploaded (20)

PPTX
Information Storage and Retrieval Techniques Unit III
PDF
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
PPT
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
PPTX
Management Information system : MIS-e-Business Systems.pptx
PPTX
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
PDF
BIO-INSPIRED ARCHITECTURE FOR PARSIMONIOUS CONVERSATIONAL INTELLIGENCE : THE ...
PDF
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
PPTX
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PDF
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
PPTX
Fundamentals of Mechanical Engineering.pptx
PPTX
Module 8- Technological and Communication Skills.pptx
PDF
distributed database system" (DDBS) is often used to refer to both the distri...
PDF
Abrasive, erosive and cavitation wear.pdf
PDF
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
PPTX
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
PDF
Soil Improvement Techniques Note - Rabbi
PDF
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
PDF
22EC502-MICROCONTROLLER AND INTERFACING-8051 MICROCONTROLLER.pdf
Information Storage and Retrieval Techniques Unit III
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
Management Information system : MIS-e-Business Systems.pptx
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
BIO-INSPIRED ARCHITECTURE FOR PARSIMONIOUS CONVERSATIONAL INTELLIGENCE : THE ...
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
Fundamentals of safety and accident prevention -final (1).pptx
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
Fundamentals of Mechanical Engineering.pptx
Module 8- Technological and Communication Skills.pptx
distributed database system" (DDBS) is often used to refer to both the distri...
Abrasive, erosive and cavitation wear.pdf
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
Soil Improvement Techniques Note - Rabbi
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
22EC502-MICROCONTROLLER AND INTERFACING-8051 MICROCONTROLLER.pdf

Stack ADT

  • 1. Stack ADT 1 Mrs.K.Arunasakthi, AP/CSE 21CS202 - STACK ADT Mrs. K.Arunasakthi Assistant Professor Computer Science & Engineering, Velammal college of Engineering & Technology, Madurai
  • 2. Stack 2  Basic principles  Operation of stack  Stack using Array  Stack using Linked List  Applications of stack Mrs.K.Arunasakthi, AP/CSE 21CS202 - STACK ADT
  • 3. Basic Idea • A stack is an Abstract Data Type (ADT), commonly used in most programming languages. It is named stack as it behaves like a real- world stack, for example – a deck of cards or a pile of plates, etc. 3 Mrs.K.Arunasakthi, AP/CSE 21CS202 - STACK ADT
  • 4. Stack Representation • Can be implemented by means of Array, Structure, Pointers and Linked List. • Stack can either be a fixed size or dynamic. 4 Mrs.K.Arunasakthi, AP/CSE 21CS202 - STACK ADT
  • 6. STACK: Last-In-First-Out (LIFO) • void push (stack *s, int element); /* Insert an element in the stack */ • int pop (stack *s); /* Remove and return the top element */ • void create (stack *s); /* Create a new stack */ • int isempty (stack *s); /* Check if stack is empty */ • int isfull (stack *s); /* Check if stack is full */ 6 Assumption: stack contains integer elements! Mrs.K.Arunasakthi, AP/CSE 21CS202 - STACK ADT
  • 7. Stack using Array 7 Mrs.K.Arunasakthi, AP/CSE 21CS202 - STACK ADT
  • 10. • In the array implementation, we would: • Declare an array of fixed size (which determines the maximum size of the stack). • Keep a variable which always points to the “top” of the stack. • Contains the array index of the “top” element. • In the linked list implementation, we would: • Maintain the stack as a linked list. • A pointer variable top points to the start of the list. • The first element of the linked list is considered as the stack top. 10 Basic Idea Mrs.K.Arunasakthi, AP/CSE 21CS202 - STACK ADT
  • 11. Stack Using Array 11 Declaration & Creation of Stack #define MAX 100 int stack[MAX]; // int stack[100]; int top =-1; Mrs.K.Arunasakthi, AP/CSE 21CS202 - STACK ADT
  • 12. Pushing an element into stack 12 void push(int st[], int val) { if(top == MAX-1) { printf("n STACK OVERFLOW"); } else { top++; st[top] = val; } }  Process of inserting an element into the stack.  For inserting an element Top will be incremented by 1  Element should be pushed on to the top of the stack.i,e., st[top]=val  Top=Top+1 // Top++ Mrs.K.Arunasakthi, AP/CSE 21CS202 - STACK ADT
  • 13. Popping an element from stack 13 int pop(int st[]) { int val; if(top == -1) { printf("n STACK UNDERFLOW"); return -1; } else { val = st[top]; top--; return val; } }  Pop is the process of removing the top element from the stack.  After removing top should be decremented by 1  top=top-1 or top--; Mrs.K.Arunasakthi, AP/CSE 21CS202 - STACK ADT
  • 14. Checking stack- Full or Empty 14 int isempty(int st[]) { if(top == -1) { printf("n STACK IS EMPTY") return -1; } } EMPTY Checking that whether the given stack is empty FULL Checking that whether the given stack is empty int isfull(int st[]) { if (top == MAX-1) printf(“STACK IS FULL”); else return (0); } Mrs.K.Arunasakthi, AP/CSE 21CS202 - STACK ADT
  • 15. Display the Stack and PEEK 15 void display(int st[]) { int i; if(top == -1) printf("n STACK IS EMPTY"); else { for(i=top;i>=0;i--) printf("n %d",st[i]); printf("n"); // Added for formatting purposes } } DISPLAY Process of displaying all the elements in the stack. FULL Displaying the Top element of the Stack. int peek(int st[]) { if(top == -1) { printf("n STACK IS EMPTY"); return -1; } else return (st[top]); } Mrs.K.Arunasakthi, AP/CSE 21CS202 - STACK ADT
  • 16. Stack using Linked List 16 Mrs.K.Arunasakthi, AP/CSE 21CS202 - STACK ADT
  • 17. 17 top PUSH OPERATION Push using Linked List Mrs.K.Arunasakthi, AP/CSE 21CS202 - STACK ADT
  • 18. 18 top POP OPERATION Pop using Linked List Mrs.K.Arunasakthi, AP/CSE 21CS202 - STACK ADT
  • 19. Declaration – Stack using Linked List 19 struct lifo { int value; struct lifo *next; }; typedef struct lifo stack; stack *top; Mrs.K.Arunasakthi, AP/CSE 21CS202 - STACK ADT
  • 20. Stack Creation 20 void create (stack **top) { *top = NULL; /* top points to NULL, indicating empty stack */ } LINKED LIST Mrs.K.Arunasakthi, AP/CSE 21CS202 - STACK ADT
  • 21. Pushing an element into stack 21 void push (stack **top, int element) { stack *new; new = (stack *)malloc (sizeof(stack)); if (new == NULL) { printf (“n Stack is full”); exit(-1); } new->value = element; new->next = *top; *top = new; } LINKED LIST Mrs.K.Arunasakthi, AP/CSE 21CS202 - STACK ADT
  • 22. Popping an element from stack 22 int pop (stack **top) { int t; stack *p; if (*top == NULL) { printf (“n Stack is empty”); exit(-1); } else { t = (*top)->value; p = *top; *top = (*top)->next; free (p); return t; } } LINKED LIST Mrs.K.Arunasakthi, AP/CSE 21CS202 - STACK ADT
  • 23. Checking for stack empty 23 LINKED LIST int isempty (stack *top) { if (top == NULL) return (1); else return (0); } Mrs.K.Arunasakthi, AP/CSE 21CS202 - STACK ADT
  • 24. Example: A Stack using an Array 24 #include <stdio.h> #define MAXSIZE 100 struct lifo { int st[MAXSIZE]; int top; }; typedef struct lifo stack; main() { stack A, B; create(&A); create(&B); push(&A,10); push(&A,20); push(&A,30); push(&B,100); push(&B,5); printf (“%d %d”, pop(&A), pop(&B)); push (&A, pop(&B)); if (isempty(&B)) printf (“n B is empty”); return; } Mrs.K.Arunasakthi, AP/CSE 21CS202 - STACK ADT
  • 25. Example: A Stack using Linked List 25 #include <stdio.h> struct lifo { int value; struct lifo *next; }; typedef struct lifo stack; main() { stack *A, *B; create(&A); create(&B); push(&A,10); push(&A,20); push(&A,30); push(&B,100); push(&B,5); printf (“%d %d”, pop(&A), pop(&B)); push (&A, pop(&B)); if (isempty(B)) printf (“n B is empty”); return; } Mrs.K.Arunasakthi, AP/CSE 21CS202 - STACK ADT
  • 26. Applications of Stacks • Direct applications: • Page-visited history in a Web browser • Undo sequence in a text editor • Chain of method calls in the Java Virtual Machine • Validate XML • Reversing a List, Paranthesis checker, Tower of hanoi • Indirect applications: • Auxiliary data structure for algorithms • Component of other data structures 26 Mrs.K.Arunasakthi, AP/CSE 21CS202 - STACK ADT
  • 27. Infix and Postfix Notations • Infix: operators placed between operands: A+B*C • Postfix: operands appear before their operators:- ABC*+ • There are no precedence rules to learn in postfix notation, and parentheses are never needed 27 Mrs.K.Arunasakthi, AP/CSE 21CS202 - STACK ADT
  • 28. Infix Postfix A + B A B + A + B * C A B C * + (A + B) * C A B + C * A + B * C + D A B C * + D + (A + B) * (C + D) A B + C D + * A * B + C * D A B * C D * + A + B * C  (A + (B * C))  (A + (B C *) )  A B C * + A + B * C + D  ((A + (B * C)) + D )  ((A + (B C*) )+ D)  ((A B C *+) + D)  A B C * + D + 28 Infix to Postfix Mrs.K.Arunasakthi, AP/CSE 21CS202 - STACK ADT
  • 29. Infix to postfix conversion • Use a stack for processing operators (push and pop operations). • Scan the sequence of operators and operands from left to right and perform one of the following: • output the operand, • push an operator of higher precedence, • pop an operator and output, till the stack top contains operator of a lower precedence and push the present operator. 29 Mrs.K.Arunasakthi, AP/CSE 21CS202 - STACK ADT
  • 30. The algorithm steps 1. Print operands as they arrive. 2. If the stack is empty or contains a left parenthesis on top, push the incoming operator onto the stack. 3. If the incoming symbol is a left parenthesis, push it on the stack. 4. If the incoming symbol is a right parenthesis, pop the stack and print the operators until you see a left parenthesis. Discard the pair of parentheses. 5. If the incoming symbol has higher precedence than the top of the stack, push it on the stack. 6. If the incoming symbol has equal precedence with the top of the stack, use association. If the association is left to right, pop and print the top of the stack and then push the incoming operator. If the association is right to left, push the incoming operator. 7. If the incoming symbol has lower precedence than the symbol on the top of the stack, pop the stack and print the top operator. Then test the incoming operator against the new top of stack. 8. At the end of the expression, pop and print all operators on the stack. (No parentheses should remain.) 30 Mrs.K.Arunasakthi, AP/CSE 21CS202 - STACK ADT
  • 31. Infix to Postfix Conversion Requires operator precedence information Operands: Add to postfix expression. Close parenthesis: pop stack symbols until an open parenthesis appears. Operators: Pop all stack symbols until a symbol of lower precedence appears. Then push the operator. End of input: Pop all remaining stack symbols and add to the expression. 31 Mrs.K.Arunasakthi, AP/CSE 21CS202 - STACK ADT
  • 32. Current symbol Operator Stack Postfix string 1 A A 2 * * A 3 ( * ( A 4 B * ( A B 5 + * ( + A B 6 C * ( + A B C 7 * * ( + * A B C 8 D * ( + * A B C D 9 ) * A B C D * + 10 + + A B C D * + * 11 E + A B C D * + * E 12 A B C D * + * E + Expression: A * (B + C * D) + E becomes A B C D * + * E + Postfix notation is also called as Reverse Polish Notation (RPN) 32 Infix to Postfix Rules Mrs.K.Arunasakthi, AP/CSE 21CS202 - STACK ADT