SlideShare a Scribd company logo
STACKS INC++
INTRODUCTION TO
STACK
DEFINITION:
 A stack is a data structure that provides temporary storage of data
in such
a way that theelement stored last will be retrieved first.
 This method is also called LIFO – Last In First Out.
EXAMPLE:
In real life we can think of stack as a stack of copies, stack of plates
etc. The first copy put in the stack is the last one to be removed.
Similarly last copy to put in the stack is the first one to be removed.
OPERATIONS ON
STACK
 A stack is a linear data structure.
 It is controlled by two operations: push and pop.
 Both the operations take place from one end of the stack,
usually called top. Top points to the current top position of
the stack.
 Push operation adds an element to the top of the stack.
 Pop operation removes an element from the top of the
stack.
 These two operations implements the LIFO method .
IMPLEMENTATION OF
STACK
Stack can be implemented in two
ways:
 As an Array
 As a Linked List
STACK AS AN
ARRAY
Array implementation of stack
uses
 an array to store data
 an integer type variable usually called the top,
which points to the top of the stack.
NOTE: The variable top contains the index
number of the top most filled element of the
array.
30
20
10
2
4
3
2
1
0
TOP
PUSH
OPERATION
 Initially when the stack is empty, TOP can have any
integer value other than any valid index number of the
array. Let TOP =
-1;
 The first element in the empty stack goes to the 0th
position of the array and the Top is initialized to value 0.
 After this every push operation increase the TOP by
1 and inserts new data at that particular position.
 As arrays are fixed in length, elements can not be
inserted beyond the maximum size of the array.
Pushing data beyond the maximum size of the stack
results in “data overflow”.
PUSHOPERATION EXPLAINED
NOTE:Pushing one more element into the stack will result in overflow.
POP OPERATION
The pop operation deletes the most recently entered item from
the stack.
Any attempt to delete an element from the empty stack results
in “data underflow” condition.
The variableTOPcontains the index number of the current top
position ofthe stack.
Eachtime the pop operation is performed, theTOPvariable is
decremented by1.
POPOPERATION EXPLAINED
top =2
APPLICATIONS OF STACK
Astack is anappropriate data structure on which information is stored and
then later retrieved in reverse order.The application of stack areasfollows:
When a program executes, stack is used to store the return address at time
of function call. After the execution of the function is over, return address is
popped from stackand control is returned back to the calling function.
Converting an infix expression o postfix operation and to evaluate the
postfix expression.
Reversingan array, converting decimal number into binary number etc.
INFIX AND POSTFIX OPERATIONS
 The infix expression is aconventional algebraic expression, in
which the operator is in between the operands.
For example:a+b
 In the postfix expression the operator is placed after the
operands.
For example:ab+
NEED OF POSTFIX EXPRESSION
The postfixexpressions are special because
 They do notuseparenthesis
 The order of expression is uniquely reconstructed from the
expression itself.
Theseexpressions are useful in computer applications.
CONVERTING INFIX TO POSTFIX EXPRESSION
CONVERT_I_P(I,P
,STACK) Where I =infix expression, P=postfix expression,STACK=stack asan array
Step1.Push‘(‘ into STACKandadd‘)’to the endof I.
Step2.ScanexpressionIfrom left to right andrepeatsteps3-6for eachelement of I untilthe stack is
empty.
Step3.If scannedelement =‘(‘, pushit into the STACK. Step4.If
scannedelement =operand,addit to P
.
Step5.If scannedelement =operator,then:
a)Repeatedly pop from STACKand add to Peachoperator from the top of the stackuntil
the operatorat the top of the stackhaslower precedencethan the operatorextracted from I.
b) Addscannedoperatorto the top of theSTACK. Step6.If
scannedelement =‘)’then:
c)RepeatedlypopfromSTACKandaddto Peachoperatoruntil the left parenthesis is
encountered.
d) Popthe left parenthesisbut do not addto P
.
CONVERTING INFIXTO POSTFIX EXPRESSION
QConvert the following infix operation to
postfix operation.
((TRUE&& FALSE)|| ! (FALSE|| TRUE))
Ans.The Postfix expression is:
TRUE FALSE&& FALSETRUE|| ! ||
EVALUATION OF POSTFIX EXPRESSION
Evaluate(P, result, STACK)
where P=Postfix expression,STACK=stack,result=to hold the result of evaluation
Step 1.Scanthe postfix expression Pfrom left to right and repeat the steps 2, 3
until theSTACKis empty.
Step 2. If the scanned element =operand,pushit into the STACK.
Step 3. If the scanned element =operator,then
a) Popthe two operands viz operandAand operand Bfrom the STACK.
b) Evaluate ((operand B)operator (operandA))
c) Pushthe result of evaluation into the STACK.
Step 4.Set result=top most element of the STACK.
EVALUATING POSTFIXEXPRESSION
Q Evaluate the following Postfixexpression:
TRUE FALSE || FALSE TRUE&& ! ||
Ans. TRUE Ste
p
No.
SymbolScanned Operation Performed Stack Status
1 TRUE TRUE
2 FALSE TRUEFALSE
3 || TRUE || FALSE =TRUE TRUE
4 FALSE TRUEFALSE
5 TRUE TRUEFALSE TRUE
6 && FALSE&& TRUE=FALSE TRUEFALSE
7 ! ! FALSE= TRUE TRUE TRUE
8 || TRUE || TRUE TRUE
STACK AS A LINKED LIST
Linked list implementation of stack uses
 Alinked list to store data
 ApointerTOPpointing to the top most position of the stack.
NOTE:Eachnode of astack asalinked list hastwo parts : data part and link part and
is created with the help of self referential structure.
Thedata part stores the data and link part stores the addressof the next node of the
linked list.
NODE
TOP
DATA LINK
PROGRAM TO ILLUSTRATE OPERATIONS ON STACK
ASA LINKED LIST
#include<iostream.h>
#include<conio.h>
struct node
{
int roll;
charname[20];
float total;
node *next;
};
classstack
{
node *top;
public :
stack()
{ top=NULL;}
void push();
void pop();
void display();
};
void stack::push()
{
node *temp;
temp=new node;
cout<<"Enter roll :";
cin>>temp->roll;
cout<<"Enter name:";
cin>>temp->name;
cout<<"Enter total :";
cin>>temp->total;
temp->next=top;
top=temp;
}
void stack::pop()
{
if(top!=NULL)
{
node*temp=top;
top=top->next;
cout<<temp->roll<<temp-
>name<<temp->total
<<"deleted";
delete temp;
}
else
cout<<"Stack empty";
}
void stack::display()
{
node *temp=top;
while(temp!=NULL)
{
cout<<temp->roll<<temp-
>name<<temp->total<<" ";
temp=temp->next;
} }
void main()
{ stack st;
char ch;
do
{ cout<<"stack
optionsnPpush
nO PopnD
Display nQquit";
cin>>ch;
switch(ch)
{ case'P':
st.push();break;
case'O':
st.pop();break;
case'D':
st.display();break;
}
}while(ch!='Q');
}
PROGRAM TO ILLUSTRATE OPERATIONS ON STACK
AS A LINKED LIST :Explanation
struct node
{
int roll;
charname[20];
float total;
node *next;
};
Self Referential Structure:Theseare specialstructures which
contains pointers tothemselves.
Here, next is apointer of type node itself.
Self Referential structures are needed to create Linked Lists.
classstack
{
node*top;
public :
stack()
{ top=NULL;}
void push();
void pop();
void display();
};
classstack: It contains pointerTOPwhich will point to the top
of thestack.
Theconstructor function initializesTOPto NULL.
PROGRAM TO ILLUSTRATE OPERATIONS ON STACK
AS A LINKED LIST :Explanation
void stack::push()
{
node *temp; // pointer of type node
temp=new node; // new operator will create a new
//node and address of new node
// is stored in temp
cout<<"Enter roll:";
Theselines will input
values into roll , name
and total of newly
created node.
cin>>temp->roll;
cout<<"Enter name:";
cin>>temp->name;
cout<<"Enter total:";
cin>>temp->total;
temp->next=top; // address stored in TOPgets
//stored in next of newly
//created node.
top=temp; // Topwill contain address of
// newly created node
}
void stack::pop()
{
if(top!=NULL) // if stack is not empty
{
node *temp=top; // temp is a pointer
//containing address of first node
top=top->next; // top will now containaddress of
//second node
cout<<temp->roll<<
temp->name<<temp->total
<<"deleted";
//This line will display
//data stored in
// deleted node
delete temp; // delete operator will delete the node
// stored attemp
}
else
cout<<"Stack empty";}

More Related Content

What's hot

Stacks overview with its applications
Stacks overview with its applicationsStacks overview with its applications
Stacks overview with its applicationsSaqib Saeed
 
stacks and queues
stacks and queuesstacks and queues
stacks and queues
EktaVaswani2
 
data structure, stack, stack data structure
data structure, stack, stack data structuredata structure, stack, stack data structure
data structure, stack, stack data structurepcnmtutorials
 
Data structure Stack
Data structure StackData structure Stack
Data structure Stack
Praveen Vishwakarma
 
stack and queue array implementation in java.
stack and queue array implementation in java.stack and queue array implementation in java.
stack and queue array implementation in java.
CIIT Atd.
 
Stack
StackStack
STACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureSTACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data Structure
Yaksh Jethva
 
Stack of Data structure
Stack of Data structureStack of Data structure
Stack of Data structure
Sheikh Monirul Hasan
 
Stack linked list
Stack linked listStack linked list
Stack linked listbhargav0077
 
04 stacks
04 stacks04 stacks
04 stacks
Rajan Gautam
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
Vineeta Garg
 
Stack project
Stack projectStack project
Stack project
Amr Aboelgood
 
stack presentation
stack presentationstack presentation
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
Archie Jamwal
 
Stack Data Structure
Stack Data StructureStack Data Structure
Stack Data Structure
Afaq Mansoor Khan
 
Introduction To Stack
Introduction To StackIntroduction To Stack
Introduction To Stack
Education Front
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj Meena
Dipayan Sarkar
 
Stacks and queue
Stacks and queueStacks and queue
Stacks and queueAmit Vats
 
Stack organization
Stack organizationStack organization
Stack organization
chauhankapil
 
Stack Data Structure
Stack Data StructureStack Data Structure
Stack Data Structure
Rabin BK
 

What's hot (20)

Stacks overview with its applications
Stacks overview with its applicationsStacks overview with its applications
Stacks overview with its applications
 
stacks and queues
stacks and queuesstacks and queues
stacks and queues
 
data structure, stack, stack data structure
data structure, stack, stack data structuredata structure, stack, stack data structure
data structure, stack, stack data structure
 
Data structure Stack
Data structure StackData structure Stack
Data structure Stack
 
stack and queue array implementation in java.
stack and queue array implementation in java.stack and queue array implementation in java.
stack and queue array implementation in java.
 
Stack
StackStack
Stack
 
STACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureSTACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data Structure
 
Stack of Data structure
Stack of Data structureStack of Data structure
Stack of Data structure
 
Stack linked list
Stack linked listStack linked list
Stack linked list
 
04 stacks
04 stacks04 stacks
04 stacks
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Stack project
Stack projectStack project
Stack project
 
stack presentation
stack presentationstack presentation
stack presentation
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
Stack Data Structure
Stack Data StructureStack Data Structure
Stack Data Structure
 
Introduction To Stack
Introduction To StackIntroduction To Stack
Introduction To Stack
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj Meena
 
Stacks and queue
Stacks and queueStacks and queue
Stacks and queue
 
Stack organization
Stack organizationStack organization
Stack organization
 
Stack Data Structure
Stack Data StructureStack Data Structure
Stack Data Structure
 

Similar to Stack data structure

Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
nikshaikh786
 
Stack
StackStack
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
Apurbo Datta
 
Unit 3 stack
Unit   3 stackUnit   3 stack
Unit 3 stack
Dabbal Singh Mahara
 
Stack and its operations, Queue and its operations
Stack and its operations, Queue and its operationsStack and its operations, Queue and its operations
Stack and its operations, Queue and its operations
poongothai11
 
Data Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptxData Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptx
RashidFaridChishti
 
Stacks & Queues
Stacks & QueuesStacks & Queues
Stacks & Queuestech4us
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
kulachihansraj
 
Stack
StackStack
In C++ a function used to put data into a stack is typically called a.docx
In C++ a function used to put data into a stack is typically called a.docxIn C++ a function used to put data into a stack is typically called a.docx
In C++ a function used to put data into a stack is typically called a.docx
tristans3
 
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
KALPANAC20
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
pinakspatel
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
AliRaza899305
 
Stack and its operations
Stack and its operationsStack and its operations
Stack and its operations
V.V.Vanniaperumal College for Women
 
DSA- Unit III- STACK AND QUEUE
DSA- Unit III- STACK AND QUEUEDSA- Unit III- STACK AND QUEUE
DSA- Unit III- STACK AND QUEUE
swathirajstar
 
Stacks Data structure.pptx
Stacks Data structure.pptxStacks Data structure.pptx
Stacks Data structure.pptx
line24arts
 
Stacks
StacksStacks
Stacks
Sadaf Ismail
 
Data structures stacks
Data structures   stacksData structures   stacks
Data structures stacks
maamir farooq
 
Stack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparation
RAtna29
 

Similar to Stack data structure (20)

Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
Stack
StackStack
Stack
 
Stack
StackStack
Stack
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Unit 3 stack
Unit   3 stackUnit   3 stack
Unit 3 stack
 
Stack and its operations, Queue and its operations
Stack and its operations, Queue and its operationsStack and its operations, Queue and its operations
Stack and its operations, Queue and its operations
 
Data Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptxData Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptx
 
Stacks & Queues
Stacks & QueuesStacks & Queues
Stacks & Queues
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
 
Stack
StackStack
Stack
 
In C++ a function used to put data into a stack is typically called a.docx
In C++ a function used to put data into a stack is typically called a.docxIn C++ a function used to put data into a stack is typically called a.docx
In C++ a function used to put data into a stack is typically called a.docx
 
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
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
Stack and its operations
Stack and its operationsStack and its operations
Stack and its operations
 
DSA- Unit III- STACK AND QUEUE
DSA- Unit III- STACK AND QUEUEDSA- Unit III- STACK AND QUEUE
DSA- Unit III- STACK AND QUEUE
 
Stacks Data structure.pptx
Stacks Data structure.pptxStacks Data structure.pptx
Stacks Data structure.pptx
 
Stacks
StacksStacks
Stacks
 
Data structures stacks
Data structures   stacksData structures   stacks
Data structures stacks
 
Stack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparation
 

Recently uploaded

GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
CatarinaPereira64715
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 

Recently uploaded (20)

GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 

Stack data structure

  • 2. INTRODUCTION TO STACK DEFINITION:  A stack is a data structure that provides temporary storage of data in such a way that theelement stored last will be retrieved first.  This method is also called LIFO – Last In First Out. EXAMPLE: In real life we can think of stack as a stack of copies, stack of plates etc. The first copy put in the stack is the last one to be removed. Similarly last copy to put in the stack is the first one to be removed.
  • 3. OPERATIONS ON STACK  A stack is a linear data structure.  It is controlled by two operations: push and pop.  Both the operations take place from one end of the stack, usually called top. Top points to the current top position of the stack.  Push operation adds an element to the top of the stack.  Pop operation removes an element from the top of the stack.  These two operations implements the LIFO method .
  • 4. IMPLEMENTATION OF STACK Stack can be implemented in two ways:  As an Array  As a Linked List
  • 5. STACK AS AN ARRAY Array implementation of stack uses  an array to store data  an integer type variable usually called the top, which points to the top of the stack. NOTE: The variable top contains the index number of the top most filled element of the array. 30 20 10 2 4 3 2 1 0 TOP
  • 6. PUSH OPERATION  Initially when the stack is empty, TOP can have any integer value other than any valid index number of the array. Let TOP = -1;  The first element in the empty stack goes to the 0th position of the array and the Top is initialized to value 0.  After this every push operation increase the TOP by 1 and inserts new data at that particular position.  As arrays are fixed in length, elements can not be inserted beyond the maximum size of the array. Pushing data beyond the maximum size of the stack results in “data overflow”.
  • 7. PUSHOPERATION EXPLAINED NOTE:Pushing one more element into the stack will result in overflow.
  • 8. POP OPERATION The pop operation deletes the most recently entered item from the stack. Any attempt to delete an element from the empty stack results in “data underflow” condition. The variableTOPcontains the index number of the current top position ofthe stack. Eachtime the pop operation is performed, theTOPvariable is decremented by1.
  • 10. APPLICATIONS OF STACK Astack is anappropriate data structure on which information is stored and then later retrieved in reverse order.The application of stack areasfollows: When a program executes, stack is used to store the return address at time of function call. After the execution of the function is over, return address is popped from stackand control is returned back to the calling function. Converting an infix expression o postfix operation and to evaluate the postfix expression. Reversingan array, converting decimal number into binary number etc.
  • 11. INFIX AND POSTFIX OPERATIONS  The infix expression is aconventional algebraic expression, in which the operator is in between the operands. For example:a+b  In the postfix expression the operator is placed after the operands. For example:ab+
  • 12. NEED OF POSTFIX EXPRESSION The postfixexpressions are special because  They do notuseparenthesis  The order of expression is uniquely reconstructed from the expression itself. Theseexpressions are useful in computer applications.
  • 13. CONVERTING INFIX TO POSTFIX EXPRESSION CONVERT_I_P(I,P ,STACK) Where I =infix expression, P=postfix expression,STACK=stack asan array Step1.Push‘(‘ into STACKandadd‘)’to the endof I. Step2.ScanexpressionIfrom left to right andrepeatsteps3-6for eachelement of I untilthe stack is empty. Step3.If scannedelement =‘(‘, pushit into the STACK. Step4.If scannedelement =operand,addit to P . Step5.If scannedelement =operator,then: a)Repeatedly pop from STACKand add to Peachoperator from the top of the stackuntil the operatorat the top of the stackhaslower precedencethan the operatorextracted from I. b) Addscannedoperatorto the top of theSTACK. Step6.If scannedelement =‘)’then: c)RepeatedlypopfromSTACKandaddto Peachoperatoruntil the left parenthesis is encountered. d) Popthe left parenthesisbut do not addto P .
  • 14. CONVERTING INFIXTO POSTFIX EXPRESSION QConvert the following infix operation to postfix operation. ((TRUE&& FALSE)|| ! (FALSE|| TRUE)) Ans.The Postfix expression is: TRUE FALSE&& FALSETRUE|| ! ||
  • 15. EVALUATION OF POSTFIX EXPRESSION Evaluate(P, result, STACK) where P=Postfix expression,STACK=stack,result=to hold the result of evaluation Step 1.Scanthe postfix expression Pfrom left to right and repeat the steps 2, 3 until theSTACKis empty. Step 2. If the scanned element =operand,pushit into the STACK. Step 3. If the scanned element =operator,then a) Popthe two operands viz operandAand operand Bfrom the STACK. b) Evaluate ((operand B)operator (operandA)) c) Pushthe result of evaluation into the STACK. Step 4.Set result=top most element of the STACK.
  • 16. EVALUATING POSTFIXEXPRESSION Q Evaluate the following Postfixexpression: TRUE FALSE || FALSE TRUE&& ! || Ans. TRUE Ste p No. SymbolScanned Operation Performed Stack Status 1 TRUE TRUE 2 FALSE TRUEFALSE 3 || TRUE || FALSE =TRUE TRUE 4 FALSE TRUEFALSE 5 TRUE TRUEFALSE TRUE 6 && FALSE&& TRUE=FALSE TRUEFALSE 7 ! ! FALSE= TRUE TRUE TRUE 8 || TRUE || TRUE TRUE
  • 17. STACK AS A LINKED LIST Linked list implementation of stack uses  Alinked list to store data  ApointerTOPpointing to the top most position of the stack. NOTE:Eachnode of astack asalinked list hastwo parts : data part and link part and is created with the help of self referential structure. Thedata part stores the data and link part stores the addressof the next node of the linked list. NODE TOP DATA LINK
  • 18. PROGRAM TO ILLUSTRATE OPERATIONS ON STACK ASA LINKED LIST #include<iostream.h> #include<conio.h> struct node { int roll; charname[20]; float total; node *next; }; classstack { node *top; public : stack() { top=NULL;} void push(); void pop(); void display(); }; void stack::push() { node *temp; temp=new node; cout<<"Enter roll :"; cin>>temp->roll; cout<<"Enter name:"; cin>>temp->name; cout<<"Enter total :"; cin>>temp->total; temp->next=top; top=temp; } void stack::pop() { if(top!=NULL) { node*temp=top; top=top->next; cout<<temp->roll<<temp- >name<<temp->total <<"deleted"; delete temp; } else cout<<"Stack empty"; } void stack::display() { node *temp=top; while(temp!=NULL) { cout<<temp->roll<<temp- >name<<temp->total<<" "; temp=temp->next; } } void main() { stack st; char ch; do { cout<<"stack optionsnPpush nO PopnD Display nQquit"; cin>>ch; switch(ch) { case'P': st.push();break; case'O': st.pop();break; case'D': st.display();break; } }while(ch!='Q'); }
  • 19. PROGRAM TO ILLUSTRATE OPERATIONS ON STACK AS A LINKED LIST :Explanation struct node { int roll; charname[20]; float total; node *next; }; Self Referential Structure:Theseare specialstructures which contains pointers tothemselves. Here, next is apointer of type node itself. Self Referential structures are needed to create Linked Lists. classstack { node*top; public : stack() { top=NULL;} void push(); void pop(); void display(); }; classstack: It contains pointerTOPwhich will point to the top of thestack. Theconstructor function initializesTOPto NULL.
  • 20. PROGRAM TO ILLUSTRATE OPERATIONS ON STACK AS A LINKED LIST :Explanation void stack::push() { node *temp; // pointer of type node temp=new node; // new operator will create a new //node and address of new node // is stored in temp cout<<"Enter roll:"; Theselines will input values into roll , name and total of newly created node. cin>>temp->roll; cout<<"Enter name:"; cin>>temp->name; cout<<"Enter total:"; cin>>temp->total; temp->next=top; // address stored in TOPgets //stored in next of newly //created node. top=temp; // Topwill contain address of // newly created node } void stack::pop() { if(top!=NULL) // if stack is not empty { node *temp=top; // temp is a pointer //containing address of first node top=top->next; // top will now containaddress of //second node cout<<temp->roll<< temp->name<<temp->total <<"deleted"; //This line will display //data stored in // deleted node delete temp; // delete operator will delete the node // stored attemp } else cout<<"Stack empty";}