SlideShare a Scribd company logo
1 of 33
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

Similar to Stack ADT

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
haaamin01
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
Kumar
 
@author Derek Harter @cwid 123 45 678 @class .docx
@author Derek Harter  @cwid   123 45 678  @class  .docx@author Derek Harter  @cwid   123 45 678  @class  .docx
@author Derek Harter @cwid 123 45 678 @class .docx
adkinspaige22
 

Similar to Stack ADT (20)

Stacks.pptx
Stacks.pptxStacks.pptx
Stacks.pptx
 
Applicationofstack by Ali F.RAshid
Applicationofstack  by Ali F.RAshid Applicationofstack  by Ali F.RAshid
Applicationofstack by Ali F.RAshid
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj Meena
 
Stacks and Queue - Data Structures
Stacks and Queue - Data StructuresStacks and Queue - Data Structures
Stacks and Queue - Data Structures
 
Data structures1
Data structures1Data structures1
Data structures1
 
Unit 3 stack
Unit 3   stackUnit 3   stack
Unit 3 stack
 
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
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
MO 2020 DS Stacks 1 AB.ppt
MO 2020 DS Stacks 1 AB.pptMO 2020 DS Stacks 1 AB.ppt
MO 2020 DS Stacks 1 AB.ppt
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
 
@author Derek Harter @cwid 123 45 678 @class .docx
@author Derek Harter  @cwid   123 45 678  @class  .docx@author Derek Harter  @cwid   123 45 678  @class  .docx
@author Derek Harter @cwid 123 45 678 @class .docx
 
Data Structure and Algorithms Stacks
Data Structure and Algorithms StacksData Structure and Algorithms Stacks
Data Structure and Algorithms Stacks
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Stack
StackStack
Stack
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
04 stacks
04 stacks04 stacks
04 stacks
 

Recently uploaded

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
Digital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxDigital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptx
pritamlangde
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
MayuraD1
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
Kamal Acharya
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
AldoGarca30
 

Recently uploaded (20)

Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Digital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxDigital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptx
 
Signal Processing and Linear System Analysis
Signal Processing and Linear System AnalysisSignal Processing and Linear System Analysis
Signal Processing and Linear System Analysis
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx
457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx
457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
Ground Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementGround Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth Reinforcement
 
Electromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxElectromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptx
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 

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