SlideShare a Scribd company logo
1 of 31
Chapter 4 
Stack
Stack 
“Stack is a linear data structure used for 
temporary storage where elements can be 
inserted or deleted at only one end called 
TOP of the stack” 
LIFO – Last in First Out 
e.g. Stack of Subject Book 
By Prof. Raj Sarode 2
Stack 
WT 
DS DS 
OOAD 
WT 
DS 
DCCN 
OOAD 
WT 
DS 
E.g. 
suppose Four Books like DS, WT, OOAD, DCCN are we entered into Stack 
By Prof. Raj Sarode 3 
TOP 
TOP 
TOP 
TOP 
E 
M 
P 
T 
Y 
TOP
Implementation or Representation of Stack 
1 Static / sequential / array representation 
 In this case of array, stack is also collection of 
homogeneous elements . 
 Therefore stack can be easily implemented 
using array. 
 Two operation : PUSH & POP 
 Element stored from stack[0] to stack[TOP] 
 Stack[0] ->Bottom of STACK 
 Stack [TOP]-> Top of STACK 
 TOP =-1 It indicate Stack is Empty. 
By Prof. Raj Sarode 4
2 Dynamic / pointer/ linked Representation 
 In Dynamic or linked representation stack is 
collection of nodes. Where each node is divided in to 
two parts 
INFO NEXT 
Top of stack is represented by start pointer of linked 
list 
 If START =NULL , it shows that stack is empty & TOP = 
NULL 
 Last node Next part contain NULL Value, it Indicate 
bottom of stack 
By Prof. Raj Sarode 5 
Store Data <- 
Structure of Node 
->Point to next Node
3 NEXT 2 NEXT 1 
E.g. 
3 NEXT 
2 NEXT 
1 
By Prof. Raj Sarode 6
Stack Operation 
1. PUSH Operation 
 Used to insert element into Stack 
e.g. 
2 
1 
0 
Top=-1 
Empty Stack 
A 
2 
1 
0 
Insert A 
Top=0 
B 
A 
2 
1 
0 
Insert B 
Top=1 
C 
B 
A 
2 
1 
0 
Insert C 
Top=2 
Top-> 
Top-> 
Top-> 
 Each time new element is inserted into stack, 
the value of Top is incremented by one 
 Top=2 i.e. MAX-1. It shows stack is Overflow 
By Prof. Raj Sarode 7
Algorithm 1 
Push(stack, MAX, Top, Ele) 
1. Check for Overflow 
a. If Top=MAX-1 then 
b. Print “overflow” & return 
Else 
a. Top = Top+1 //Increment Top by One 
b. Stack[Top]=Ele //insert Element at new Location 
End if 
2. Return. 
By Prof. Raj Sarode 8
Stack Operation 
2. POP Operation 
 Used to Delete element From Stack 
e.g. 
2 
1 
0 
A is Removed 
Top=-1 
Stack is Empty 
A 
2 
1 
0 
B is Removed 
Top=0 
B 
A 
2 
1 
0 
C is Removed 
Top=1 
C 
B 
A 
2 
1 
0 
Top=2 
Top-> 
Top-> 
Top-> 
 In POP Last inserted element is Deleted First [LIFO] 
 After deleting element value of top is decreased by one 
 When all elements from stack are removed (i.e. empty stack) we 
cannot removed anything from stack. This condition is called 
“Underflow” By Prof. Raj Sarode 9
Algorithm 2 
POP(stack, MAX, Top, Ele) 
1. Check for Underflow 
a. If Top=-1 then 
b. Print “underflow” & return 
Else 
a. Ele=Stack[Top] //delete element from stack 
b. Top = Top-1 //decrement Top by One 
End if 
2. Return. 
By Prof. Raj Sarode 10
Application of Stack 
• Reversing of String 
• Matching Parenthesis 
• Conversion of Expression 
• Evaluation of Expression 
By Prof. Raj Sarode 11
1. Reversing of String 
 Push every character of string into stack 
 Pop the stack and display the 
 Get reverse string 
E.g. 
R 
M 
B 
M 
B 
B 
By Prof. Raj Sarode 12 
B 
I I 
M 
B 
I 
R 
M 
B 
I 
D 
R 
M 
B 
I 
PUSH Operation 
I 
D 
I 
R 
I 
M 
I 
B I 
POP Operation 
O/P String is D R M B I 
I/P String is I B M R D
2. Matching parenthesis 
1. Initially take an empty stack 
2. Scan the symbol of expression from left to right 
3. If the symbol is left parenthesis the push into stack. 
4. If the symbol is right parenthesis 
a. if stack is empty 
b. print “right parenthesis are more than left parenthesis.” 
else 
a. pop element from stack 
b. if pop parenthesis !=Element 
c. Print “Mismatched parenthesis.” 
5. After scanning all the symbol 
a. If the stack is empty 
Print “parenthesis are equal: valid” 
else 
Print “left parenthesis are more than right parenthesis.” 
By Prof. Raj Sarode 13
By Prof. Raj Sarode 14 
E.g. (A + B ) * ( C – D ) 
Sr. No. Next I/P Symbol Stack O/P 
1 ( ( 
2 A ( 
3 + ( 
4 ) EMPTY [pop 1 left Parenthesis] 
5 * EMPTY 
6 ( ( 
7 C ( 
8 - ( 
9 D ( [pop 1 left Parenthesis] 
10 ) EMPTY Valid Expression
3. Conversion of Expression 
1. Infix to Postfix e.g. A+B -> AB+ 
2. Infix to Prefix e.g. A+B -> +AB 
3. Postfix to Infix e.g. AB+ -> A+B 
4. Postfix to Prefix e.g. AB+ -> +AB 
5. Prefix to Infix e.g. +AB -> A+B 
6. Prefix to Postfix e.g. +AB -> AB+ 
By Prof. Raj Sarode 15
Expressions 
Expression is of Three types 
1. Infix Expression 
A + B 
2. Postfix Expression 
A B + 
3. Prefix Expression 
+ A B 
Note: Priority of operators (, ), ^, *, /, +, -. 
# indicate end of expression. 
By Prof. Raj Sarode 16
Infix to Postfix Conversion 
Algorithm 
1. Read the infix expression from left to right one character at a time. 
2. Repeat step 3 
3. Read symbol 
a. If symbol is operand put into postfix string 
b. If symbol is left parenthesis push into stack 
c. If right parenthesis, pop stack of TOP until left parenthesis occur and 
make postfix expression 
d. If operator then 
I. If operator has same of less precedence then operators available at 
top of stack, then pop all such operators and form postfix string 
II. Push scanned /incoming operator to the stack 
4. Until last of string encountered 
5. Pop all the element from stack to make stack empty 
By Prof. Raj Sarode 17
E.g. A + B / C * ( D + E ) - F 
Sr. No. Next I/P Symbol Operator Stack O/P POSTFIX STRING 
1 A EMPTY A 
2 + + A 
3 B + AB 
4 / + , / AB 
5 C + , / ABC 
6 * + , * ABC/ 
7 ( + , * , ( ABC/ 
8 D + , * , ( ABC/D 
9 + + , * , ( , + ABC/D 
10 E + , * , ( , + ABC/DE 
11 ) + , * ABC/DE+ 
12 - - ABC/DE+*+ 
13 F - ABC/DE+*+F 
14 Pop all Stack EMPTY ABC/DE+*+F- 
By Prof. Raj Sarode 18
Infix to Prefix Conversion 
Note: for converting infix exp. To prefix exp. It requires two stack 1. operator stack 2. operand stack. 
Algorithm 
1. Read the infix expression from left to right one character at a time. 
2. Repeat step 3 
3. Read symbol 
a. If symbol is operand put into operand stack 
b. If symbol is left parenthesis push into operator stack 
c. If right parenthesis, pop two operand and one operator and push in operand 
stack until left parenthesis occur. 
d. If scan symbol is operator then 
I. If operator has same of less precedence then operator available on top of 
operator stack, then pop one operator and two operand form prefix 
expression & push into operand stack 
4. Repeat Until end of expression. 
5. Pop all the remaining operator and corresponding operand and form prefix 
expression 
6. Exit. 
By Prof. Raj Sarode 19
By Prof. Raj Sarode 20 
E.g. A + B / C – D * E + F 
Sr. No. Next I/P Symbol Operator Stack Operand Stack 
1 A EMPTY A 
2 + + A 
3 B + A , B 
4 / + , / A , B 
5 C + , / A , B , C 
6 - + A , / B C 
7 - - + A / B C 
8 D - + A / B C , D 
9 * - , * + A / B C , D 
10 E - , * + A / B C , D , E 
11 + - + A / B C , * D E 
12 + + - + A / B C * D E 
13 F + - + A / B C * D E , F 
14 END OF I/P POP ALL STACK + - + A / B C * D E F FINAL O/P
Postfix to Infix Conversion 
Algorithm 
Note: for converting postfix expression to infix it require operand stack to store the operands 
1. Read the Postfix expression from left to right one character at a time. 
2. If it is operand push into operand stack. 
3. If it is operator 
a. Pop two operand from stack 
b. Form infix expression and push into operand stack. 
4. If expression is not end go to step One 
5. Pop operand stack and display. 
6. Exit 
By Prof. Raj Sarode 21
By Prof. Raj Sarode 22 
E.g. A B + C D - / 
Sr. No. Next I/P Symbol Operand Stack Infix Expression 
1 A A 
2 B A, B 
3 + A + B A + B 
4 C A + B, C 
5 D A + B, C , D 
6 - A + B, C – D C - D 
7 / A + B / C – D A + B / C – D FINAL O/P
Postfix to Prefix Conversion 
Algorithm 
Note: for converting postfix expression to prefix it require operand stack to store the operands 
1. Read the Postfix expression from left to right one character at a time. 
2. If it is operand push into operand stack. 
3. If it is operator 
a. Pop two operand from stack 
b. Form prefix expression and push into operand stack. 
4. If expression is not end go to step One 
5. Pop operand stack and display. 
6. Exit 
By Prof. Raj Sarode 23
By Prof. Raj Sarode 24 
E.g. A B + C * D / 
Sr. No. Next I/P Symbol Operand Stack Prefix Expression 
1 A A 
2 B A , B 
3 + + A B + A B 
4 C + A B, C 
5 * * + A B C * + A B C 
6 D * + A B C , D 
7 / / * + A B C D / * + A B C D FINAL O/P
Prefix to Infix Conversion 
Algorithm 
Note: for converting prefix expression to infix it require one stack to store the operator as well as 
operands 
1. Read the prefix expression from left to right one character at a time. 
2. If it is operator push into stack. 
3. If it is operand 
while (operand (stack[TOP]) 
{ operand1 = pop(); 
operator = pop(); 
expression=(operand1, operator, symbol) 
symbol=expression; } 
4. Push symbol into stack 
5. If prefix string is not end go to step one 
6. POP stack & Display it 
7. Exit 
By Prof. Raj Sarode 25
By Prof. Raj Sarode 26 
E.g. * + A B – C D 
Sr. No. Next I/P Symbol Stack Infix Expression 
1 * * 
2 + * , + 
3 A * , + , A 
4 B * , A + B A + B 
5 - * , A + B , - 
6 C * , A + B , - , D 
7 D * , A + B , C – D C – D 
8 C – D A + B * C – D A + B * C – D FINAL O/P
Prefix to Postfix Conversion 
Algorithm 
Note: for converting prefix expression to Postfix it require one stack to store the operator as well 
as operands 
1. Read the prefix expression from left to right one character at a time. 
2. If it is operator push into stack. 
3. If it is operand 
while (operand (stack[TOP]) 
{ operand1 = pop(); 
operator = pop(); 
expression=(operand1, symbol, operator) 
symbol=expression; } 
4. Push symbol into stack 
5. If prefix string is not end go to step one 
6. POP stack & Display it 
7. Exit 
By Prof. Raj Sarode 27
By Prof. Raj Sarode 28 
E.g. / * + A B C D 
Sr. No. Next I/P Symbol Stack Postfix Expression 
1 / / 
2 * / , * 
3 + / , * , + 
4 A / , * , + , A 
5 B / , * , A B + A B + 
6 C / , A B + C * A B + C * 
7 D A B + C * D / A B + C * D / FIN`AL O/P
/*program For stack operation */ 
By Prof. Raj Sarode 29 
#include<iostream.h> 
#include<conio.h> 
#include<stdlib.h> 
class stack 
{ 
int stk[5]; 
int top; 
public: 
stack() 
{ top=-1; } 
void push(int x) 
{ if(top > 4) 
{ cout <<"stack over flow"; 
return; 
} 
stk[++top]=x; 
cout <<"inserted" <<x; 
} 
void pop() 
{ if(top <0) 
{ cout <<"stack under flow"; 
return; 
} 
cout <<"deleted" <<stk[top--]; } 
void display() 
{ if(top<0) 
{ cout <<" stack empty"; 
return; 
} for(int i=top;i>=0;i--) 
cout <<stk[i] <<" "; 
} }; 
main() 
{ int ch; 
stack st; 
while(1) 
{ cout <<"n1.push 2.pop 3.display 
4.exitnEnter ur choice"; 
cin >> ch; 
switch(ch) 
{ case 1: cout <<"enter the element"; 
cin >> ch; 
st.push(ch); 
break; 
case 2: st.pop(); break; 
case 3: st.display();break; 
case 4: exit(0); 
} } 
return (0); }
/*program For Reverse String */ 
By Prof. Raj Sarode 30 
#include<iostream.h> 
#include<conio.h> 
#include<stdlib.h> 
# define MAX 10 
class stack 
{ char stk[MAX]; 
int top; 
public: 
stack() 
{ top=-1; } 
void push() 
{ int n,i; 
cout<<"Enter the size of string"; 
cin>>n; 
if(n>MAX) 
{ cout<<"out of size"; 
} else 
{ for(i=0;i<n;i++) 
cin>>stk[++top]; 
} 
} 
void reverse() 
{ if(top<0) 
{ cout <<" stack empty"; 
return; 
} for(int i=top;i>=0;i--) 
cout <<stk[i] <<" "; 
} }; 
main() 
{ int ch; 
clrscr(); 
stack st; 
while(1) 
{ cout <<"n1.push 2.reverse 3.exitn Enter ur 
choice"; 
cin >> ch; 
switch(ch) 
{ 
case 1: st.push();break; 
case 2: st.reverse();break; 
case 3: exit(0); 
} } 
return (0); }
Thank You 
By Prof. Raj Sarode 31

More Related Content

What's hot

What's hot (20)

STACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureSTACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data Structure
 
Stack - Data Structure
Stack - Data StructureStack - Data Structure
Stack - Data Structure
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj Meena
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
 
Stack of Data structure
Stack of Data structureStack of Data structure
Stack of Data structure
 
Stack Data Structure
Stack Data StructureStack Data Structure
Stack Data Structure
 
Stack application
Stack applicationStack application
Stack application
 
Stacks
StacksStacks
Stacks
 
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
 
Stack
StackStack
Stack
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
Stack using Array
Stack using ArrayStack using Array
Stack using Array
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Stack a Data Structure
Stack a Data StructureStack a Data Structure
Stack a Data Structure
 
Unit 3 stack
Unit   3 stackUnit   3 stack
Unit 3 stack
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 

Viewers also liked

Viewers also liked (20)

Queue
QueueQueue
Queue
 
Queue
QueueQueue
Queue
 
Chap 6 cloud security
Chap 6 cloud securityChap 6 cloud security
Chap 6 cloud security
 
Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)
 
Tree
TreeTree
Tree
 
Ppt presentation of queues
Ppt presentation of queuesPpt presentation of queues
Ppt presentation of queues
 
Queue
QueueQueue
Queue
 
Queue as data_structure
Queue as data_structureQueue as data_structure
Queue as data_structure
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Notes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queueNotes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queue
 
23 stacks-queues-deques
23 stacks-queues-deques23 stacks-queues-deques
23 stacks-queues-deques
 
Lists, queues and stacks
Lists, queues and stacksLists, queues and stacks
Lists, queues and stacks
 
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada ReddyDatastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
 
Queue
QueueQueue
Queue
 
Basic structure of computers
Basic structure of computersBasic structure of computers
Basic structure of computers
 
Chap 8 graph
Chap 8 graphChap 8 graph
Chap 8 graph
 
Team 4
Team 4Team 4
Team 4
 
Chap 7 binary threaded tree
Chap 7 binary threaded treeChap 7 binary threaded tree
Chap 7 binary threaded tree
 
Queue
QueueQueue
Queue
 
Chap 1 introduction to cloud computing
Chap 1 introduction to cloud computingChap 1 introduction to cloud computing
Chap 1 introduction to cloud computing
 

Similar to stack

Stack - Data Structure - Notes
Stack - Data Structure - NotesStack - Data Structure - Notes
Stack - Data Structure - NotesOmprakash Chauhan
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manualnikshaikh786
 
Infix-Postfix expression conversion
Infix-Postfix expression conversionInfix-Postfix expression conversion
Infix-Postfix expression conversionRashmiranja625
 
Infix to postfix expression in ds
Infix to postfix expression in dsInfix to postfix expression in ds
Infix to postfix expression in dsRohini Mahajan
 
Applicationsofstack 110805072322-phpapp01
Applicationsofstack 110805072322-phpapp01Applicationsofstack 110805072322-phpapp01
Applicationsofstack 110805072322-phpapp01Jay Patel
 
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
 
Applicationofstack by Ali F.RAshid
Applicationofstack  by Ali F.RAshid Applicationofstack  by Ali F.RAshid
Applicationofstack by Ali F.RAshid ali rashid
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsAfaq Mansoor Khan
 
Topic 2_revised.pptx
Topic 2_revised.pptxTopic 2_revised.pptx
Topic 2_revised.pptxJAYAPRIYAR7
 
358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9sumitbardhan
 
Stack in Data Structure
Stack in Data StructureStack in Data Structure
Stack in Data StructureUshaP15
 
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
 
Concept of stack ,stack of aaray stack by linked list , application of stac...
Concept of stack ,stack of aaray   stack by linked list , application of stac...Concept of stack ,stack of aaray   stack by linked list , application of stac...
Concept of stack ,stack of aaray stack by linked list , application of stac...muskankumari7360
 

Similar to stack (20)

Stack - Data Structure - Notes
Stack - Data Structure - NotesStack - Data Structure - Notes
Stack - Data Structure - Notes
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
Stack
StackStack
Stack
 
Infix-Postfix expression conversion
Infix-Postfix expression conversionInfix-Postfix expression conversion
Infix-Postfix expression conversion
 
Stack
StackStack
Stack
 
Infix to postfix expression in ds
Infix to postfix expression in dsInfix to postfix expression in ds
Infix to postfix expression in ds
 
Applicationsofstack 110805072322-phpapp01
Applicationsofstack 110805072322-phpapp01Applicationsofstack 110805072322-phpapp01
Applicationsofstack 110805072322-phpapp01
 
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
 
MO 2020 DS Stacks 3 AB.ppt
MO 2020 DS Stacks 3 AB.pptMO 2020 DS Stacks 3 AB.ppt
MO 2020 DS Stacks 3 AB.ppt
 
Applicationofstack by Ali F.RAshid
Applicationofstack  by Ali F.RAshid Applicationofstack  by Ali F.RAshid
Applicationofstack by Ali F.RAshid
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix Notations
 
Topic 2_revised.pptx
Topic 2_revised.pptxTopic 2_revised.pptx
Topic 2_revised.pptx
 
358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9
 
Stack in Data Structure
Stack in Data StructureStack in Data Structure
Stack in Data Structure
 
Chapter 6 ds
Chapter 6 dsChapter 6 ds
Chapter 6 ds
 
Stack
StackStack
Stack
 
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
 
stacks and queues
stacks and queuesstacks and queues
stacks and queues
 
Concept of stack ,stack of aaray stack by linked list , application of stac...
Concept of stack ,stack of aaray   stack by linked list , application of stac...Concept of stack ,stack of aaray   stack by linked list , application of stac...
Concept of stack ,stack of aaray stack by linked list , application of stac...
 
Unit 3 stack
Unit 3   stackUnit 3   stack
Unit 3 stack
 

Recently uploaded

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 

Recently uploaded (20)

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 

stack

  • 2. Stack “Stack is a linear data structure used for temporary storage where elements can be inserted or deleted at only one end called TOP of the stack” LIFO – Last in First Out e.g. Stack of Subject Book By Prof. Raj Sarode 2
  • 3. Stack WT DS DS OOAD WT DS DCCN OOAD WT DS E.g. suppose Four Books like DS, WT, OOAD, DCCN are we entered into Stack By Prof. Raj Sarode 3 TOP TOP TOP TOP E M P T Y TOP
  • 4. Implementation or Representation of Stack 1 Static / sequential / array representation  In this case of array, stack is also collection of homogeneous elements .  Therefore stack can be easily implemented using array.  Two operation : PUSH & POP  Element stored from stack[0] to stack[TOP]  Stack[0] ->Bottom of STACK  Stack [TOP]-> Top of STACK  TOP =-1 It indicate Stack is Empty. By Prof. Raj Sarode 4
  • 5. 2 Dynamic / pointer/ linked Representation  In Dynamic or linked representation stack is collection of nodes. Where each node is divided in to two parts INFO NEXT Top of stack is represented by start pointer of linked list  If START =NULL , it shows that stack is empty & TOP = NULL  Last node Next part contain NULL Value, it Indicate bottom of stack By Prof. Raj Sarode 5 Store Data <- Structure of Node ->Point to next Node
  • 6. 3 NEXT 2 NEXT 1 E.g. 3 NEXT 2 NEXT 1 By Prof. Raj Sarode 6
  • 7. Stack Operation 1. PUSH Operation  Used to insert element into Stack e.g. 2 1 0 Top=-1 Empty Stack A 2 1 0 Insert A Top=0 B A 2 1 0 Insert B Top=1 C B A 2 1 0 Insert C Top=2 Top-> Top-> Top->  Each time new element is inserted into stack, the value of Top is incremented by one  Top=2 i.e. MAX-1. It shows stack is Overflow By Prof. Raj Sarode 7
  • 8. Algorithm 1 Push(stack, MAX, Top, Ele) 1. Check for Overflow a. If Top=MAX-1 then b. Print “overflow” & return Else a. Top = Top+1 //Increment Top by One b. Stack[Top]=Ele //insert Element at new Location End if 2. Return. By Prof. Raj Sarode 8
  • 9. Stack Operation 2. POP Operation  Used to Delete element From Stack e.g. 2 1 0 A is Removed Top=-1 Stack is Empty A 2 1 0 B is Removed Top=0 B A 2 1 0 C is Removed Top=1 C B A 2 1 0 Top=2 Top-> Top-> Top->  In POP Last inserted element is Deleted First [LIFO]  After deleting element value of top is decreased by one  When all elements from stack are removed (i.e. empty stack) we cannot removed anything from stack. This condition is called “Underflow” By Prof. Raj Sarode 9
  • 10. Algorithm 2 POP(stack, MAX, Top, Ele) 1. Check for Underflow a. If Top=-1 then b. Print “underflow” & return Else a. Ele=Stack[Top] //delete element from stack b. Top = Top-1 //decrement Top by One End if 2. Return. By Prof. Raj Sarode 10
  • 11. Application of Stack • Reversing of String • Matching Parenthesis • Conversion of Expression • Evaluation of Expression By Prof. Raj Sarode 11
  • 12. 1. Reversing of String  Push every character of string into stack  Pop the stack and display the  Get reverse string E.g. R M B M B B By Prof. Raj Sarode 12 B I I M B I R M B I D R M B I PUSH Operation I D I R I M I B I POP Operation O/P String is D R M B I I/P String is I B M R D
  • 13. 2. Matching parenthesis 1. Initially take an empty stack 2. Scan the symbol of expression from left to right 3. If the symbol is left parenthesis the push into stack. 4. If the symbol is right parenthesis a. if stack is empty b. print “right parenthesis are more than left parenthesis.” else a. pop element from stack b. if pop parenthesis !=Element c. Print “Mismatched parenthesis.” 5. After scanning all the symbol a. If the stack is empty Print “parenthesis are equal: valid” else Print “left parenthesis are more than right parenthesis.” By Prof. Raj Sarode 13
  • 14. By Prof. Raj Sarode 14 E.g. (A + B ) * ( C – D ) Sr. No. Next I/P Symbol Stack O/P 1 ( ( 2 A ( 3 + ( 4 ) EMPTY [pop 1 left Parenthesis] 5 * EMPTY 6 ( ( 7 C ( 8 - ( 9 D ( [pop 1 left Parenthesis] 10 ) EMPTY Valid Expression
  • 15. 3. Conversion of Expression 1. Infix to Postfix e.g. A+B -> AB+ 2. Infix to Prefix e.g. A+B -> +AB 3. Postfix to Infix e.g. AB+ -> A+B 4. Postfix to Prefix e.g. AB+ -> +AB 5. Prefix to Infix e.g. +AB -> A+B 6. Prefix to Postfix e.g. +AB -> AB+ By Prof. Raj Sarode 15
  • 16. Expressions Expression is of Three types 1. Infix Expression A + B 2. Postfix Expression A B + 3. Prefix Expression + A B Note: Priority of operators (, ), ^, *, /, +, -. # indicate end of expression. By Prof. Raj Sarode 16
  • 17. Infix to Postfix Conversion Algorithm 1. Read the infix expression from left to right one character at a time. 2. Repeat step 3 3. Read symbol a. If symbol is operand put into postfix string b. If symbol is left parenthesis push into stack c. If right parenthesis, pop stack of TOP until left parenthesis occur and make postfix expression d. If operator then I. If operator has same of less precedence then operators available at top of stack, then pop all such operators and form postfix string II. Push scanned /incoming operator to the stack 4. Until last of string encountered 5. Pop all the element from stack to make stack empty By Prof. Raj Sarode 17
  • 18. E.g. A + B / C * ( D + E ) - F Sr. No. Next I/P Symbol Operator Stack O/P POSTFIX STRING 1 A EMPTY A 2 + + A 3 B + AB 4 / + , / AB 5 C + , / ABC 6 * + , * ABC/ 7 ( + , * , ( ABC/ 8 D + , * , ( ABC/D 9 + + , * , ( , + ABC/D 10 E + , * , ( , + ABC/DE 11 ) + , * ABC/DE+ 12 - - ABC/DE+*+ 13 F - ABC/DE+*+F 14 Pop all Stack EMPTY ABC/DE+*+F- By Prof. Raj Sarode 18
  • 19. Infix to Prefix Conversion Note: for converting infix exp. To prefix exp. It requires two stack 1. operator stack 2. operand stack. Algorithm 1. Read the infix expression from left to right one character at a time. 2. Repeat step 3 3. Read symbol a. If symbol is operand put into operand stack b. If symbol is left parenthesis push into operator stack c. If right parenthesis, pop two operand and one operator and push in operand stack until left parenthesis occur. d. If scan symbol is operator then I. If operator has same of less precedence then operator available on top of operator stack, then pop one operator and two operand form prefix expression & push into operand stack 4. Repeat Until end of expression. 5. Pop all the remaining operator and corresponding operand and form prefix expression 6. Exit. By Prof. Raj Sarode 19
  • 20. By Prof. Raj Sarode 20 E.g. A + B / C – D * E + F Sr. No. Next I/P Symbol Operator Stack Operand Stack 1 A EMPTY A 2 + + A 3 B + A , B 4 / + , / A , B 5 C + , / A , B , C 6 - + A , / B C 7 - - + A / B C 8 D - + A / B C , D 9 * - , * + A / B C , D 10 E - , * + A / B C , D , E 11 + - + A / B C , * D E 12 + + - + A / B C * D E 13 F + - + A / B C * D E , F 14 END OF I/P POP ALL STACK + - + A / B C * D E F FINAL O/P
  • 21. Postfix to Infix Conversion Algorithm Note: for converting postfix expression to infix it require operand stack to store the operands 1. Read the Postfix expression from left to right one character at a time. 2. If it is operand push into operand stack. 3. If it is operator a. Pop two operand from stack b. Form infix expression and push into operand stack. 4. If expression is not end go to step One 5. Pop operand stack and display. 6. Exit By Prof. Raj Sarode 21
  • 22. By Prof. Raj Sarode 22 E.g. A B + C D - / Sr. No. Next I/P Symbol Operand Stack Infix Expression 1 A A 2 B A, B 3 + A + B A + B 4 C A + B, C 5 D A + B, C , D 6 - A + B, C – D C - D 7 / A + B / C – D A + B / C – D FINAL O/P
  • 23. Postfix to Prefix Conversion Algorithm Note: for converting postfix expression to prefix it require operand stack to store the operands 1. Read the Postfix expression from left to right one character at a time. 2. If it is operand push into operand stack. 3. If it is operator a. Pop two operand from stack b. Form prefix expression and push into operand stack. 4. If expression is not end go to step One 5. Pop operand stack and display. 6. Exit By Prof. Raj Sarode 23
  • 24. By Prof. Raj Sarode 24 E.g. A B + C * D / Sr. No. Next I/P Symbol Operand Stack Prefix Expression 1 A A 2 B A , B 3 + + A B + A B 4 C + A B, C 5 * * + A B C * + A B C 6 D * + A B C , D 7 / / * + A B C D / * + A B C D FINAL O/P
  • 25. Prefix to Infix Conversion Algorithm Note: for converting prefix expression to infix it require one stack to store the operator as well as operands 1. Read the prefix expression from left to right one character at a time. 2. If it is operator push into stack. 3. If it is operand while (operand (stack[TOP]) { operand1 = pop(); operator = pop(); expression=(operand1, operator, symbol) symbol=expression; } 4. Push symbol into stack 5. If prefix string is not end go to step one 6. POP stack & Display it 7. Exit By Prof. Raj Sarode 25
  • 26. By Prof. Raj Sarode 26 E.g. * + A B – C D Sr. No. Next I/P Symbol Stack Infix Expression 1 * * 2 + * , + 3 A * , + , A 4 B * , A + B A + B 5 - * , A + B , - 6 C * , A + B , - , D 7 D * , A + B , C – D C – D 8 C – D A + B * C – D A + B * C – D FINAL O/P
  • 27. Prefix to Postfix Conversion Algorithm Note: for converting prefix expression to Postfix it require one stack to store the operator as well as operands 1. Read the prefix expression from left to right one character at a time. 2. If it is operator push into stack. 3. If it is operand while (operand (stack[TOP]) { operand1 = pop(); operator = pop(); expression=(operand1, symbol, operator) symbol=expression; } 4. Push symbol into stack 5. If prefix string is not end go to step one 6. POP stack & Display it 7. Exit By Prof. Raj Sarode 27
  • 28. By Prof. Raj Sarode 28 E.g. / * + A B C D Sr. No. Next I/P Symbol Stack Postfix Expression 1 / / 2 * / , * 3 + / , * , + 4 A / , * , + , A 5 B / , * , A B + A B + 6 C / , A B + C * A B + C * 7 D A B + C * D / A B + C * D / FIN`AL O/P
  • 29. /*program For stack operation */ By Prof. Raj Sarode 29 #include<iostream.h> #include<conio.h> #include<stdlib.h> class stack { int stk[5]; int top; public: stack() { top=-1; } void push(int x) { if(top > 4) { cout <<"stack over flow"; return; } stk[++top]=x; cout <<"inserted" <<x; } void pop() { if(top <0) { cout <<"stack under flow"; return; } cout <<"deleted" <<stk[top--]; } void display() { if(top<0) { cout <<" stack empty"; return; } for(int i=top;i>=0;i--) cout <<stk[i] <<" "; } }; main() { int ch; stack st; while(1) { cout <<"n1.push 2.pop 3.display 4.exitnEnter ur choice"; cin >> ch; switch(ch) { case 1: cout <<"enter the element"; cin >> ch; st.push(ch); break; case 2: st.pop(); break; case 3: st.display();break; case 4: exit(0); } } return (0); }
  • 30. /*program For Reverse String */ By Prof. Raj Sarode 30 #include<iostream.h> #include<conio.h> #include<stdlib.h> # define MAX 10 class stack { char stk[MAX]; int top; public: stack() { top=-1; } void push() { int n,i; cout<<"Enter the size of string"; cin>>n; if(n>MAX) { cout<<"out of size"; } else { for(i=0;i<n;i++) cin>>stk[++top]; } } void reverse() { if(top<0) { cout <<" stack empty"; return; } for(int i=top;i>=0;i--) cout <<stk[i] <<" "; } }; main() { int ch; clrscr(); stack st; while(1) { cout <<"n1.push 2.reverse 3.exitn Enter ur choice"; cin >> ch; switch(ch) { case 1: st.push();break; case 2: st.reverse();break; case 3: exit(0); } } return (0); }
  • 31. Thank You By Prof. Raj Sarode 31