SlideShare a Scribd company logo
1 of 33
Download to read offline
Stack
Stack
A stack is a data structure
in which items can be
inserted only from one end
and get items back from the
same end. There , the last
item inserted into stack, is
the the first item to be taken
out from the stack. In short
its also called Last in First
out [LIFO].
Example of Stack (LIFO)
● A Stack of book on table.
● Token stack in Bank.
● Stack of trays and plates.
Stack Operations
● Top: Open end of the stack is called Top, From this end item can
be inserted.
● Push: To insert an item from Top of stack is called push operation.
The push operation change the position of Top in stack.
● POP: To put-off, get or remove some item from top of the stack is
the pop operation, We can POP only only from top of the stack.
● IsEmpty: Stack considered empty when there is no item on Top.
IsEmpty operation return true when no item in stack else false.
● IsFull: Stack considered full if no other element can be inserted on
top of the stack. This condition normally occur when stack
implement ed through array.
Stack Abstract Data Type
ADT/Class Stack {
Data/Attributes/Values:
int size; Top
Type items;
Functions/Operations:
CreateStack (int size); --create stack of size
Void Push(int n); - - if stack is not full
Type Pop(); - - if stack is not empty return top item
Int isEmpty(); - - return true if empty otherwise false
Int isFull(); - - return true if full otherwise false }
Stack Example
Stack Example ...
# include <iostream.h>
#include <conio.h>
template <class Type>
Class intstack { private:
int size, top;
Type* Items;
// Functions & Operations
public:
Intstack ( ) { //default constructor
top=0; size=3;
Items = new Type[size]; }
Stack Implementation using Array
~intstack( ) {
delete[] Items; }
Void Push (Type a) {
if(!IsFul( ))
Items[top++ ] = a;
else
Cout <<”n stack is full.... push failed”; }
Type POP(void) {
if(!isEmpty( ))
return Items [ --top ];
else
Count <<n stack is empty .. POP failed”; }
Stack Implementation using Array
int IsEmpty ( ) { return (top == 0); }
int IsFull( ) { return (top == size); }
Void Display ( ) {
If( isEmpty( ))
Cout<<”Empty”;
Else for (int i=0; i<top; i++)
cout<<Item[i] <<” “;
}
} ; // end stack class
Stack Implementation using Array
Stack Implementation using Array
Void main( )
{ intstack s;
s.Display();
s.POP();
s.push(6); s.push(5); s.push(2);
s.push(3);
s.Display( );
s.push(8);
s.pop( ); s.push(8);
s.Display( );
s.pop( ); s.pop ( ); s.push(40);
s.Display( );
s.pop( ); s.pop( ); s.push(45);
s.Display ()
getch();
}
Stack Implementation using Array
1. s.Display();
2. s.POP();
3. s.push(6); s.push(5); s.push(2);
s.push(3);
4. s.Display( );
5. s.push(8);
6. s.pop( ); s.push(8);
7. s.Display( );
8. s.pop( ); s.pop ( ); s.push(40);
9. s.Display( );
10.s.pop( ); s.pop( ); s.push(45);
11.s.Display( );
1. Empty
2. Stack is empty
3.
4. 6 5 2 3
5. Stack is full
6.
7. 6 5 2 8
8.
9. 6 5 40
10.s.pop( ); s.pop( ); s.push(45);
11. 6 45
Stack Implementation using Array ...
Stack Application
Reversing a string: To reverse a string we can use
following algorithm.
1. Given the sting and a stack
2. While there is not end of string, do the following.
3. Read a character form the string
4. Push it on the stack
5. End while
6. Re-initialize string position
7. While stack is not Empty, do the following.
8. Pop a character from the stack
9. Insert the character popped into next position in string.
10.End While
Reverse String
Void reverse (char* str) {
Inst i, len;
len = strlen(str);
//Create stack of characters of size =
len
Stack<char> s(len);
for(i=o; i< len; i++ )
s.Push(str[i]);
s.Display;
For (j=0; j<len;j++)
str[j] = s.POP[j];
}
void main( )
{
char str[ ] =”abcdef”;
cout<<” Original String is “<<str;
reverse(str);
cout<<” Reversed String is “<<str;
getch();
}
Reverse String...
String is a b c d e f
PUSH to SACK
Reverse String...
Reversed String: f e d c b a
POP from SACK
Infix Expression: An expression in which the
operator is in between its two operands.
A+B
Prefix Expression: An expression in which operator
precedes its two operands is called an prefix
expression.
+AB
Postfix Expression: An expression in which
operator follows its two operands is called a postfix
expression.
AB+
Infix, Postfix and Prefix Expression
Infix to Postfix Conversion
Infix expression can be directly evaluated but
the standard practice in CS is that the infix
expression converted to postfix form and then
the expression is evaluated. During both
processes stack is proved to be a useful data
structure.
1. Given a expression in the infix form.
2. Find the highest precedence operator
3. If there are more then one operators with the same
precedence check associativity, i.e. pick the left most first.
4. Convert the operator and its operands from infix to postfix
A + B --> A B+
5. Repeat steps 2 to 4, until all the operators in the given
expression are in the postfix form.
Infix to Postfix By-Hand Algorithm
Infix to Prefix By-Hand Algorithm
1. Given a expression in the infix form.
2. Find the highest precedence operator
3. If there are more then one operators with the same
precedence check associativity, i.e. pick the left most first.
4. Convert the operator and its operands from infix to prefix
A + B --> +A B
5. Repeat steps 2 to 4, until all the operators in the given
expression are in the postfix form.
C language operators Precedence and
Associativity
● A+B*C-D (Infix)
● A+BC*-D
● ABC*+-D
● ABC*+D- (Postfix)
● (A+B)*C-D (Infix)
● (AB+)*C-D
● AB+C*-D
● AB+C*D- (Postfix)
Exercise:
1. Infix ( (A * B) + (C / D) ) to Postfix
2. Infix ((A * (B + C) ) / D) to Postfix
3. Infix (A * (B + (C / D) ) ) to Postfix
Infix to Postfix Step by Step Conversion
● A*B+C/D (Infix)
● *AB+C/D
● *AB+/CD
● +*AB/CD (Prefix)
● A*(B+C/D)
● A*(B+/CD)
● A*(+B/CD)
● *A+B/CD
Exercise:
1. Infix ( (A * B) + (C / D) ) to Prefix
2.Infix ((A * (B + C) ) / D) to Prefix
3.Infix (A * (B + (C / D) ) ) to Prefix
Infix to Prefix Step by Step Conversion
Infix to Postfix using stack
● Given an expression in the Infix form.
● Create and initialize a stack to hold operators.
● While expression has more token (operator and operands) Do.
● If the next token is an operand, add it to postfix string.
● Else if the next token is end parenthesis ')', then
– Do until the start parenthesis '(' is opened
● Pop operator from the stack
● Add to postfix string
– END Do
– Discard the Popped start parenthesis '('.
● End If
Infix to Postfix using stack ...
● If the next token is start parenthesis '(', push it on the stack.
● Else If the next token is an operator, Then
– While (Stack is not empty) AND (precedence of operator on top
of the stack is more then the current operator) Do
● Pop operator from the stack
● Add to postfix string
– End While
– Push operator to stack
● End if
● End While
Infix to Postfix using stack ...
● While stack is not empty
– Pop operator from the stack
– Add to postfix string
● End While
● Postfix is Created
Infix to Postfix using stack
● Example A*B+C become AB*C+
Infix to Postfix using stack ...
● Example A * (B + C * D) + E becomes A B C D * + * E +
Infix to Postfix using Stack..
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.
Infix to Postfix using Stack..
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.)
Evaluating Arithmetic Postfix
Expression
● Expression is available in the postfix form
● Initialize the stack of operands.
● While expression has more token (operators and operands), do
– If the next token is an operand
● Push it on the stack
– Else the token is an operator
● Pop two operands from the stack
● Apply the operator to the operands
● Push the result back on to the stack
– End If
● End While
● Result of expression is on the operand stack, pop and display.
Evaluating Arithmetic Postfix
Expression
PostFix Expression 2 3 4 + * 5 *
Move Token Stack
1 2 2
2 3 2 3
3 4 2 3 4
4 + 2 7 (3+4=7)
5 * 1 4 (2*7=14)
6 5 14 5
7 * 70 (14*5=70)

More Related Content

What's hot

Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Balwant Gorad
 
Priority queue in DSA
Priority queue in DSAPriority queue in DSA
Priority queue in DSAjunnubabu
 
Stack data structure
Stack data structureStack data structure
Stack data structureTech_MX
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSGokul Hari
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListManishPrajapati78
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary searchZia Ush Shamszaman
 
Evaluation of postfix expression
Evaluation of postfix expressionEvaluation of postfix expression
Evaluation of postfix expressionAkhil Ahuja
 
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
 
STACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureSTACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureYaksh Jethva
 

What's hot (20)

stack & queue
stack & queuestack & queue
stack & queue
 
Stack a Data Structure
Stack a Data StructureStack a Data Structure
Stack a Data Structure
 
Data structure by Digvijay
Data structure by DigvijayData structure by Digvijay
Data structure by Digvijay
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Stacks
StacksStacks
Stacks
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
 
Priority queue in DSA
Priority queue in DSAPriority queue in DSA
Priority queue in DSA
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Array ppt
Array pptArray ppt
Array ppt
 
Infix to postfix conversion
Infix to postfix conversionInfix to postfix conversion
Infix to postfix conversion
 
Stack
StackStack
Stack
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
 
Queues
QueuesQueues
Queues
 
Linked lists
Linked listsLinked lists
Linked lists
 
Evaluation of postfix expression
Evaluation of postfix expressionEvaluation of postfix expression
Evaluation of postfix expression
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application
 
STACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureSTACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data Structure
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
 

Viewers also liked

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
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data StructureZidny Nafan
 
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
 
Ppt presentation of queues
Ppt presentation of queuesPpt presentation of queues
Ppt presentation of queuesBuxoo Abdullah
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examplesgreatqadirgee4u
 
Queue data structure
Queue data structureQueue data structure
Queue data structureanooppjoseph
 
Notation
NotationNotation
Notationruhatch
 
Stack application
Stack applicationStack application
Stack applicationStudent
 
Data structures and algorithms short note (version 14).pd
Data structures and algorithms short note (version 14).pdData structures and algorithms short note (version 14).pd
Data structures and algorithms short note (version 14).pdNimmi Weeraddana
 
Expression evaluation
Expression evaluationExpression evaluation
Expression evaluationJeeSa Sultana
 
computer notes - Evaluating postfix expressions
computer notes - Evaluating postfix expressionscomputer notes - Evaluating postfix expressions
computer notes - Evaluating postfix expressionsecomputernotes
 

Viewers also liked (20)

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
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Data Structure (Stack)
Data Structure (Stack)Data Structure (Stack)
Data Structure (Stack)
 
Application of Stacks
Application of StacksApplication of Stacks
Application of Stacks
 
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
 
Ppt presentation of queues
Ppt presentation of queuesPpt presentation of queues
Ppt presentation of queues
 
Stack Applications
Stack ApplicationsStack Applications
Stack Applications
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examples
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Notation
NotationNotation
Notation
 
Lifo
LifoLifo
Lifo
 
Stack application
Stack applicationStack application
Stack application
 
Data structures and algorithms short note (version 14).pd
Data structures and algorithms short note (version 14).pdData structures and algorithms short note (version 14).pd
Data structures and algorithms short note (version 14).pd
 
Lec21
Lec21Lec21
Lec21
 
Team 4
Team 4Team 4
Team 4
 
Expression evaluation
Expression evaluationExpression evaluation
Expression evaluation
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
 
Data Structures - Lecture 5 [Stack]
Data Structures - Lecture 5 [Stack]Data Structures - Lecture 5 [Stack]
Data Structures - Lecture 5 [Stack]
 
computer notes - Evaluating postfix expressions
computer notes - Evaluating postfix expressionscomputer notes - Evaluating postfix expressions
computer notes - Evaluating postfix expressions
 

Similar to Stack

Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manualnikshaikh786
 
Stack in Data Structure
Stack in Data StructureStack in Data Structure
Stack in Data StructureUshaP15
 
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptxSTACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptxKALPANAC20
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxprakashvs7
 
Stacks Data structure.pptx
Stacks Data structure.pptxStacks Data structure.pptx
Stacks Data structure.pptxline24arts
 
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
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxchandankumar364348
 
Data structures stacks
Data structures   stacksData structures   stacks
Data structures stacksmaamir farooq
 

Similar to Stack (20)

Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
Unit 3 stack
Unit   3 stackUnit   3 stack
Unit 3 stack
 
DS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptxDS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptx
 
Stack
StackStack
Stack
 
Stack in Data Structure
Stack in Data StructureStack in Data Structure
Stack in Data Structure
 
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptxSTACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.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
 
stack-Intro.pptx
stack-Intro.pptxstack-Intro.pptx
stack-Intro.pptx
 
Stacks
StacksStacks
Stacks
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
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
 
STACK1.pptx
STACK1.pptxSTACK1.pptx
STACK1.pptx
 
Stacks Data structure.pptx
Stacks Data structure.pptxStacks Data structure.pptx
Stacks Data structure.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
 
CH4.pptx
CH4.pptxCH4.pptx
CH4.pptx
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
 
Data structure Stack
Data structure StackData structure Stack
Data structure Stack
 
DATA STRUCTURE - STACK
DATA STRUCTURE - STACKDATA STRUCTURE - STACK
DATA STRUCTURE - STACK
 
Data structures stacks
Data structures   stacksData structures   stacks
Data structures stacks
 

More from Zaid Shabbir

Modern SDLC and QA.pptx
Modern SDLC and QA.pptxModern SDLC and QA.pptx
Modern SDLC and QA.pptxZaid Shabbir
 
Software Agility.pptx
Software Agility.pptxSoftware Agility.pptx
Software Agility.pptxZaid Shabbir
 
Software Development Guide To Accelerate Performance
Software Development Guide To Accelerate PerformanceSoftware Development Guide To Accelerate Performance
Software Development Guide To Accelerate PerformanceZaid Shabbir
 
Software Testing and Agility
Software Testing and Agility Software Testing and Agility
Software Testing and Agility Zaid Shabbir
 
Data security and Integrity
Data security and IntegrityData security and Integrity
Data security and IntegrityZaid Shabbir
 
Cloud computing &amp; dbms
Cloud computing &amp; dbmsCloud computing &amp; dbms
Cloud computing &amp; dbmsZaid Shabbir
 
No sql bigdata and postgresql
No sql bigdata and postgresqlNo sql bigdata and postgresql
No sql bigdata and postgresqlZaid Shabbir
 
Files and data storage
Files and data storageFiles and data storage
Files and data storageZaid Shabbir
 
Tree and binary tree
Tree and binary treeTree and binary tree
Tree and binary treeZaid Shabbir
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structureZaid Shabbir
 

More from Zaid Shabbir (14)

Modern SDLC and QA.pptx
Modern SDLC and QA.pptxModern SDLC and QA.pptx
Modern SDLC and QA.pptx
 
Software Agility.pptx
Software Agility.pptxSoftware Agility.pptx
Software Agility.pptx
 
Software Development Guide To Accelerate Performance
Software Development Guide To Accelerate PerformanceSoftware Development Guide To Accelerate Performance
Software Development Guide To Accelerate Performance
 
Software Testing and Agility
Software Testing and Agility Software Testing and Agility
Software Testing and Agility
 
Data security and Integrity
Data security and IntegrityData security and Integrity
Data security and Integrity
 
Cloud computing &amp; dbms
Cloud computing &amp; dbmsCloud computing &amp; dbms
Cloud computing &amp; dbms
 
No sql bigdata and postgresql
No sql bigdata and postgresqlNo sql bigdata and postgresql
No sql bigdata and postgresql
 
Files and data storage
Files and data storageFiles and data storage
Files and data storage
 
Queue
QueueQueue
Queue
 
Queue
QueueQueue
Queue
 
Sorting
SortingSorting
Sorting
 
Tree and binary tree
Tree and binary treeTree and binary tree
Tree and binary tree
 
Sorting
SortingSorting
Sorting
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
 

Recently uploaded

(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendArshad QA
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 

Recently uploaded (20)

(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 

Stack

  • 2. Stack A stack is a data structure in which items can be inserted only from one end and get items back from the same end. There , the last item inserted into stack, is the the first item to be taken out from the stack. In short its also called Last in First out [LIFO].
  • 3. Example of Stack (LIFO) ● A Stack of book on table. ● Token stack in Bank. ● Stack of trays and plates.
  • 4. Stack Operations ● Top: Open end of the stack is called Top, From this end item can be inserted. ● Push: To insert an item from Top of stack is called push operation. The push operation change the position of Top in stack. ● POP: To put-off, get or remove some item from top of the stack is the pop operation, We can POP only only from top of the stack. ● IsEmpty: Stack considered empty when there is no item on Top. IsEmpty operation return true when no item in stack else false. ● IsFull: Stack considered full if no other element can be inserted on top of the stack. This condition normally occur when stack implement ed through array.
  • 5. Stack Abstract Data Type ADT/Class Stack { Data/Attributes/Values: int size; Top Type items; Functions/Operations: CreateStack (int size); --create stack of size Void Push(int n); - - if stack is not full Type Pop(); - - if stack is not empty return top item Int isEmpty(); - - return true if empty otherwise false Int isFull(); - - return true if full otherwise false }
  • 8. # include <iostream.h> #include <conio.h> template <class Type> Class intstack { private: int size, top; Type* Items; // Functions & Operations public: Intstack ( ) { //default constructor top=0; size=3; Items = new Type[size]; } Stack Implementation using Array
  • 9. ~intstack( ) { delete[] Items; } Void Push (Type a) { if(!IsFul( )) Items[top++ ] = a; else Cout <<”n stack is full.... push failed”; } Type POP(void) { if(!isEmpty( )) return Items [ --top ]; else Count <<n stack is empty .. POP failed”; } Stack Implementation using Array
  • 10. int IsEmpty ( ) { return (top == 0); } int IsFull( ) { return (top == size); } Void Display ( ) { If( isEmpty( )) Cout<<”Empty”; Else for (int i=0; i<top; i++) cout<<Item[i] <<” “; } } ; // end stack class Stack Implementation using Array
  • 11. Stack Implementation using Array Void main( ) { intstack s; s.Display(); s.POP(); s.push(6); s.push(5); s.push(2); s.push(3); s.Display( ); s.push(8); s.pop( ); s.push(8); s.Display( ); s.pop( ); s.pop ( ); s.push(40); s.Display( ); s.pop( ); s.pop( ); s.push(45); s.Display () getch(); }
  • 12. Stack Implementation using Array 1. s.Display(); 2. s.POP(); 3. s.push(6); s.push(5); s.push(2); s.push(3); 4. s.Display( ); 5. s.push(8); 6. s.pop( ); s.push(8); 7. s.Display( ); 8. s.pop( ); s.pop ( ); s.push(40); 9. s.Display( ); 10.s.pop( ); s.pop( ); s.push(45); 11.s.Display( ); 1. Empty 2. Stack is empty 3. 4. 6 5 2 3 5. Stack is full 6. 7. 6 5 2 8 8. 9. 6 5 40 10.s.pop( ); s.pop( ); s.push(45); 11. 6 45
  • 14. Stack Application Reversing a string: To reverse a string we can use following algorithm. 1. Given the sting and a stack 2. While there is not end of string, do the following. 3. Read a character form the string 4. Push it on the stack 5. End while 6. Re-initialize string position 7. While stack is not Empty, do the following. 8. Pop a character from the stack 9. Insert the character popped into next position in string. 10.End While
  • 15. Reverse String Void reverse (char* str) { Inst i, len; len = strlen(str); //Create stack of characters of size = len Stack<char> s(len); for(i=o; i< len; i++ ) s.Push(str[i]); s.Display; For (j=0; j<len;j++) str[j] = s.POP[j]; } void main( ) { char str[ ] =”abcdef”; cout<<” Original String is “<<str; reverse(str); cout<<” Reversed String is “<<str; getch(); }
  • 16. Reverse String... String is a b c d e f PUSH to SACK
  • 17. Reverse String... Reversed String: f e d c b a POP from SACK
  • 18. Infix Expression: An expression in which the operator is in between its two operands. A+B Prefix Expression: An expression in which operator precedes its two operands is called an prefix expression. +AB Postfix Expression: An expression in which operator follows its two operands is called a postfix expression. AB+ Infix, Postfix and Prefix Expression
  • 19. Infix to Postfix Conversion Infix expression can be directly evaluated but the standard practice in CS is that the infix expression converted to postfix form and then the expression is evaluated. During both processes stack is proved to be a useful data structure.
  • 20. 1. Given a expression in the infix form. 2. Find the highest precedence operator 3. If there are more then one operators with the same precedence check associativity, i.e. pick the left most first. 4. Convert the operator and its operands from infix to postfix A + B --> A B+ 5. Repeat steps 2 to 4, until all the operators in the given expression are in the postfix form. Infix to Postfix By-Hand Algorithm
  • 21. Infix to Prefix By-Hand Algorithm 1. Given a expression in the infix form. 2. Find the highest precedence operator 3. If there are more then one operators with the same precedence check associativity, i.e. pick the left most first. 4. Convert the operator and its operands from infix to prefix A + B --> +A B 5. Repeat steps 2 to 4, until all the operators in the given expression are in the postfix form.
  • 22. C language operators Precedence and Associativity
  • 23. ● A+B*C-D (Infix) ● A+BC*-D ● ABC*+-D ● ABC*+D- (Postfix) ● (A+B)*C-D (Infix) ● (AB+)*C-D ● AB+C*-D ● AB+C*D- (Postfix) Exercise: 1. Infix ( (A * B) + (C / D) ) to Postfix 2. Infix ((A * (B + C) ) / D) to Postfix 3. Infix (A * (B + (C / D) ) ) to Postfix Infix to Postfix Step by Step Conversion
  • 24. ● A*B+C/D (Infix) ● *AB+C/D ● *AB+/CD ● +*AB/CD (Prefix) ● A*(B+C/D) ● A*(B+/CD) ● A*(+B/CD) ● *A+B/CD Exercise: 1. Infix ( (A * B) + (C / D) ) to Prefix 2.Infix ((A * (B + C) ) / D) to Prefix 3.Infix (A * (B + (C / D) ) ) to Prefix Infix to Prefix Step by Step Conversion
  • 25. Infix to Postfix using stack ● Given an expression in the Infix form. ● Create and initialize a stack to hold operators. ● While expression has more token (operator and operands) Do. ● If the next token is an operand, add it to postfix string. ● Else if the next token is end parenthesis ')', then – Do until the start parenthesis '(' is opened ● Pop operator from the stack ● Add to postfix string – END Do – Discard the Popped start parenthesis '('. ● End If
  • 26. Infix to Postfix using stack ... ● If the next token is start parenthesis '(', push it on the stack. ● Else If the next token is an operator, Then – While (Stack is not empty) AND (precedence of operator on top of the stack is more then the current operator) Do ● Pop operator from the stack ● Add to postfix string – End While – Push operator to stack ● End if ● End While
  • 27. Infix to Postfix using stack ... ● While stack is not empty – Pop operator from the stack – Add to postfix string ● End While ● Postfix is Created
  • 28. Infix to Postfix using stack ● Example A*B+C become AB*C+
  • 29. Infix to Postfix using stack ... ● Example A * (B + C * D) + E becomes A B C D * + * E +
  • 30. Infix to Postfix using Stack.. 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.
  • 31. Infix to Postfix using Stack.. 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.)
  • 32. Evaluating Arithmetic Postfix Expression ● Expression is available in the postfix form ● Initialize the stack of operands. ● While expression has more token (operators and operands), do – If the next token is an operand ● Push it on the stack – Else the token is an operator ● Pop two operands from the stack ● Apply the operator to the operands ● Push the result back on to the stack – End If ● End While ● Result of expression is on the operand stack, pop and display.
  • 33. Evaluating Arithmetic Postfix Expression PostFix Expression 2 3 4 + * 5 * Move Token Stack 1 2 2 2 3 2 3 3 4 2 3 4 4 + 2 7 (3+4=7) 5 * 1 4 (2*7=14) 6 5 14 5 7 * 70 (14*5=70)