SlideShare a Scribd company logo
1 of 27
A
Data Structure
 WHAT IS A STACK
 STACK OPERATIONS
 LIFOSTACK
 ANIMATION DEPICTING PUSH OPERATION
 ANIMATION DEPICTING POP OPERATION
 ARRAYIMPLEMENTATIONOF STACK
 APPLICATIONS OF STACK
CHECKING FOR BALANCEDBRACES
CONVERTING INFIX EXPRESSION TO POSTFIX
POSTFIX CALCULATOR
W
HAT IS A STACK?
A stack is a Last In, First Out (LIFO) data
structure,objects inserted last are the first to
come out of the stack
Anything added to the stack goes on the “top”
of the stack
Anything removed from the stack is taken from
the “top” of the stack
Things are removed in the reverse order from
that in which they were inserted
W
HAT IS A STACK?
A Stack is a linear data structure that follows
the LIFO (Last-In-First-Out) principle. Stack has
one end, whereas the Queue has two ends (front
and rear). It contains only one pointer top
pointer pointing to the topmost element of the
stack. Whenever an element is added in the stack, it
is added on the top of the stack, and the element
can be deleted only from the stack. In other words,
a stack can be defined as a container in which
insertion and deletion can be done from the one
end known as the top of the stack.
STACKOPERATIONS
•push(): When we insert an element in a stack then the
operation is known as a push. If the stack is full then the
overflow condition occurs.
•pop(): When we delete an element from the stack, the
operation is known as a pop. If the stack is empty means that no
element exists in the stack, this state is known as an underflow
state.
•isEmpty(): It determines whether the stack is empty or not.
•isFull(): It determines whether the stack is full or not.'
•peek(): It returns the element at the given position.
•count(): It returns the total number of elements available in a
A LIFOSTACK
Top
Bottom
Stack Pointer
Push Pop
APPLICATIONS
•Balancing of symbols: Stack is used for balancing a symbol. For example, we have
the following program:
1.int main()
2.{
3. cout<<"Hello";
4. cout<<"javaTpoint";
5.}
As we know, each program has an opening and closing braces; when the opening
braces come, we push the braces in a stack, and when the closing braces appear, we
pop the opening braces from the stack. Therefore, the net value comes out to be zero.
If any symbol is left in the stack, it means that some syntax occurs in a program.
APPLICATIONS
•String reversal: Stack is also used for reversing a
string. For example, we want to reverse a "javaTpoint"
string, so we can achieve this with the help of a stack.
First, we push all the characters of the string in a stack
until we reach the null character.
After pushing all the characters, we start taking out the
character one by one until we reach the bottom of the
stack.
APPLICATIONS
•UNDO/REDO: It can also be used for performing
UNDO/REDO operations.
•For example, we have an editor in which we write 'a', then
'b', and then 'c'; therefore, the text written in an editor is abc.
•So, there are three states, a, ab, and abc, which are stored in
a stack. There would be two stacks in which one stack shows
UNDO state, and the other shows REDO state.
•If we want to perform UNDO operation, and want to achieve
'ab' state, then we implement pop operation.
APPLICATIONS
•Recursion: The recursion means that the function is calling
itself again. To maintain the previous states, the compiler
creates a system stack in which all the previous records of the
function are maintained.
•DFS(Depth First Search): This search is implemented on a
Graph, and Graph uses the stack data structure.
•Backtracking: Suppose we have to create a path to solve a
maze problem. If we are moving in a particular path, and we
realize that we come on the wrong way. In order to come at
the beginning of the path to create a new path, we have to
use the stack data structure.
APPLICATIONS
•Expression conversion: Stack can also be used for
expression conversion. This is one of the most important
applications of stack.
•Memory management: The stack manages the memory.
The memory is assigned in the contiguous memory blocks.
The memory is known as stack memory as all the variables
are assigned in a function call stack memory. The memory
size assigned to the program is known to the compiler. When
the function is created, all its variables are assigned in the
stack memory. When the function completed its execution, all
the variables assigned in the stack are released.
ARRAY IM
PLEM
ENTATIONOF STACK
A[1] A[2] …. …. …. A[n]
w y
Stack pointer
J=n
Stack Full
Stack pointer
J=2
Before Push
Stack pointer
J=3
After Push
After many
Push Ops
Push X w y x
Stack Pointer
J=0
Stack Empty
Pop
After many Pop ops
Stack Pointer
J=3
Before Pop
Stack Pointer
J=2
After Pop
CHECKINGFOR BALANCED BRACES
 ([]({()}[()])) is balanced; ([]({()}[())]) is not
 Simple counting is not enough to check
balance
 You can do it with a stack: going left to
right,
 If you see a (, [, or {, push it on the
stack
 If you see a ), ], or }, pop the stack
and check whether you got the
corresponding (, [, or {
 When you reach the end, check that the
stack is empty
CONVERTING INFIX EXPRESSIONS TOEQUIV
ALENT
POSTFIX EXPRESSIONS
 Infix Expression a+b
 Postfix Expression ab+
 An infix expression can be evaluated by first
being converted into an equivalent postfix
expression
 Facts about converting from infix to postfix
 Operands always stay in the same order with
respect to one another
 All parentheses are removed
ALGORITHM: CONVERTINGINFIX EXPRESSION TO
POSTFIX USINGSTACK
Suppose X is an arithmetic expression written in infix notation.
This algorithm finds the equivalent postfix expression Y.
1. Push "(" onto STACK, and add ")" to the end of X.
2. Scan X from left to right and REPEAT Steps 3 to 6 for each element of X UNTIL the
STACK is empty :
3. If an operand is encountered, add it to Y.
4. If a left parenthesis is encountered, push it onto STACK.
5. If an operator is encountered, then :
(a) Repeatedly pop from STACK and add to Y each operator (on the top of STACK) which has
the same precedence as or higher precedence than operator.
(b) Add operator to STACK.
/* End of If structure * /
6. If a right parenthesis is encountered, then :
(a)Repeatedly pop from STACK and add to Y each operator (on the top of STACK)
until a left parenthesis is encountered.
(b) Remove the left parenthesis. [Do not add the left parenthesis to Y].
/* End of If structure * /
/* End of Step 2 loop * /
7. END.
A trace of the algorithm that converts the infix expression
a - (b + c * d)/e to postfix form
A POSTFIX CALCULATOR
 When an operand is entered, the calculator
 Pushes it onto a stack
 When an operator is entered, the calculator
 Applies it to the top (one or two, depending
on unary or binary operator) operand(s) of
the stack
 Pops the operands from the stack
 Pushes the result of the operation on the
stack
The action of a postfix calculator when evaluating the expression 2 * (3 + 4)
Postfix Notation--- 2 3 4 + *
ACKNOWLEDGEMENT
 DATA STRUCTURES BYTANENBAUM
 INTERNET
 COMPUTER SCIENCE C++ A TEXTBOOK FOR CLASS XII -BY
SUMITAARORA
PREPARED BY-
SEEMA KAUSHIK,
COMPUTER SCIENCE DEPARTMENT,
DAV PUBLIC SCHOOL, SECTOR-14, GURGAON

More Related Content

Similar to STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx

Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manualnikshaikh786
 
Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxYogesh Pawar
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTUREArchie Jamwal
 
Stacks Data structure.pptx
Stacks Data structure.pptxStacks Data structure.pptx
Stacks Data structure.pptxline24arts
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-listpinakspatel
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7Kumar
 
STACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureSTACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureYaksh Jethva
 
5 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart25 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart2SSE_AndyLi
 
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
 
Stack linked list
Stack linked listStack linked list
Stack linked listbhargav0077
 
Infix-Postfix expression conversion
Infix-Postfix expression conversionInfix-Postfix expression conversion
Infix-Postfix expression conversionRashmiranja625
 
Module 2 ppt.pptx
Module 2 ppt.pptxModule 2 ppt.pptx
Module 2 ppt.pptxSonaPathak4
 

Similar to STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx (20)

Unit 3 stack
Unit   3 stackUnit   3 stack
Unit 3 stack
 
Stack
StackStack
Stack
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptx
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
Stacks
StacksStacks
Stacks
 
Stacks Data structure.pptx
Stacks Data structure.pptxStacks Data structure.pptx
Stacks Data structure.pptx
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
 
Stacks
StacksStacks
Stacks
 
STACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureSTACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data Structure
 
CH4.pptx
CH4.pptxCH4.pptx
CH4.pptx
 
5 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart25 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart2
 
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
 
Stack linked list
Stack linked listStack linked list
Stack linked list
 
STACK1.pptx
STACK1.pptxSTACK1.pptx
STACK1.pptx
 
Infix-Postfix expression conversion
Infix-Postfix expression conversionInfix-Postfix expression conversion
Infix-Postfix expression conversion
 
5.-Stacks.pptx
5.-Stacks.pptx5.-Stacks.pptx
5.-Stacks.pptx
 
Module 2 ppt.pptx
Module 2 ppt.pptxModule 2 ppt.pptx
Module 2 ppt.pptx
 

More from KALPANAC20

Animation History from ancient days to recent.ppt
Animation History from ancient days to recent.pptAnimation History from ancient days to recent.ppt
Animation History from ancient days to recent.pptKALPANAC20
 
HISTORY OF ANIMATION, ANCIENT MACHINES.ppt
HISTORY OF ANIMATION, ANCIENT MACHINES.pptHISTORY OF ANIMATION, ANCIENT MACHINES.ppt
HISTORY OF ANIMATION, ANCIENT MACHINES.pptKALPANAC20
 
1. Design and Analysis of Algorithms LAB 21.02.24.pptx
1. Design and Analysis of Algorithms LAB 21.02.24.pptx1. Design and Analysis of Algorithms LAB 21.02.24.pptx
1. Design and Analysis of Algorithms LAB 21.02.24.pptxKALPANAC20
 
COMPUTER NETWORKS DATAS AND SIGNALS.pptx
COMPUTER NETWORKS DATAS AND SIGNALS.pptxCOMPUTER NETWORKS DATAS AND SIGNALS.pptx
COMPUTER NETWORKS DATAS AND SIGNALS.pptxKALPANAC20
 
DIFF BET VFX AND ANIMATION.pptx
DIFF BET VFX AND ANIMATION.pptxDIFF BET VFX AND ANIMATION.pptx
DIFF BET VFX AND ANIMATION.pptxKALPANAC20
 
TERMINOLOGIES OF TREE, TYPES OF TREE.pptx
TERMINOLOGIES OF TREE, TYPES OF TREE.pptxTERMINOLOGIES OF TREE, TYPES OF TREE.pptx
TERMINOLOGIES OF TREE, TYPES OF TREE.pptxKALPANAC20
 
1-PE-I-OOAD.pptx
1-PE-I-OOAD.pptx1-PE-I-OOAD.pptx
1-PE-I-OOAD.pptxKALPANAC20
 
2-CIVILIZATION AND CULTURE.ppt
2-CIVILIZATION AND CULTURE.ppt2-CIVILIZATION AND CULTURE.ppt
2-CIVILIZATION AND CULTURE.pptKALPANAC20
 

More from KALPANAC20 (10)

Animation History from ancient days to recent.ppt
Animation History from ancient days to recent.pptAnimation History from ancient days to recent.ppt
Animation History from ancient days to recent.ppt
 
HISTORY OF ANIMATION, ANCIENT MACHINES.ppt
HISTORY OF ANIMATION, ANCIENT MACHINES.pptHISTORY OF ANIMATION, ANCIENT MACHINES.ppt
HISTORY OF ANIMATION, ANCIENT MACHINES.ppt
 
1. Design and Analysis of Algorithms LAB 21.02.24.pptx
1. Design and Analysis of Algorithms LAB 21.02.24.pptx1. Design and Analysis of Algorithms LAB 21.02.24.pptx
1. Design and Analysis of Algorithms LAB 21.02.24.pptx
 
COMPUTER NETWORKS DATAS AND SIGNALS.pptx
COMPUTER NETWORKS DATAS AND SIGNALS.pptxCOMPUTER NETWORKS DATAS AND SIGNALS.pptx
COMPUTER NETWORKS DATAS AND SIGNALS.pptx
 
DIFF BET VFX AND ANIMATION.pptx
DIFF BET VFX AND ANIMATION.pptxDIFF BET VFX AND ANIMATION.pptx
DIFF BET VFX AND ANIMATION.pptx
 
TERMINOLOGIES OF TREE, TYPES OF TREE.pptx
TERMINOLOGIES OF TREE, TYPES OF TREE.pptxTERMINOLOGIES OF TREE, TYPES OF TREE.pptx
TERMINOLOGIES OF TREE, TYPES OF TREE.pptx
 
STLAB.pptx
STLAB.pptxSTLAB.pptx
STLAB.pptx
 
ST LAB-1.pptx
ST LAB-1.pptxST LAB-1.pptx
ST LAB-1.pptx
 
1-PE-I-OOAD.pptx
1-PE-I-OOAD.pptx1-PE-I-OOAD.pptx
1-PE-I-OOAD.pptx
 
2-CIVILIZATION AND CULTURE.ppt
2-CIVILIZATION AND CULTURE.ppt2-CIVILIZATION AND CULTURE.ppt
2-CIVILIZATION AND CULTURE.ppt
 

Recently uploaded

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
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
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 

Recently uploaded (20)

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
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🔝
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
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
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
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
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 

STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx

  • 2.  WHAT IS A STACK  STACK OPERATIONS  LIFOSTACK  ANIMATION DEPICTING PUSH OPERATION  ANIMATION DEPICTING POP OPERATION  ARRAYIMPLEMENTATIONOF STACK  APPLICATIONS OF STACK CHECKING FOR BALANCEDBRACES CONVERTING INFIX EXPRESSION TO POSTFIX POSTFIX CALCULATOR
  • 3. W HAT IS A STACK? A stack is a Last In, First Out (LIFO) data structure,objects inserted last are the first to come out of the stack Anything added to the stack goes on the “top” of the stack Anything removed from the stack is taken from the “top” of the stack Things are removed in the reverse order from that in which they were inserted
  • 4. W HAT IS A STACK? A Stack is a linear data structure that follows the LIFO (Last-In-First-Out) principle. Stack has one end, whereas the Queue has two ends (front and rear). It contains only one pointer top pointer pointing to the topmost element of the stack. Whenever an element is added in the stack, it is added on the top of the stack, and the element can be deleted only from the stack. In other words, a stack can be defined as a container in which insertion and deletion can be done from the one end known as the top of the stack.
  • 5. STACKOPERATIONS •push(): When we insert an element in a stack then the operation is known as a push. If the stack is full then the overflow condition occurs. •pop(): When we delete an element from the stack, the operation is known as a pop. If the stack is empty means that no element exists in the stack, this state is known as an underflow state. •isEmpty(): It determines whether the stack is empty or not. •isFull(): It determines whether the stack is full or not.' •peek(): It returns the element at the given position. •count(): It returns the total number of elements available in a
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13. APPLICATIONS •Balancing of symbols: Stack is used for balancing a symbol. For example, we have the following program: 1.int main() 2.{ 3. cout<<"Hello"; 4. cout<<"javaTpoint"; 5.} As we know, each program has an opening and closing braces; when the opening braces come, we push the braces in a stack, and when the closing braces appear, we pop the opening braces from the stack. Therefore, the net value comes out to be zero. If any symbol is left in the stack, it means that some syntax occurs in a program.
  • 14. APPLICATIONS •String reversal: Stack is also used for reversing a string. For example, we want to reverse a "javaTpoint" string, so we can achieve this with the help of a stack. First, we push all the characters of the string in a stack until we reach the null character. After pushing all the characters, we start taking out the character one by one until we reach the bottom of the stack.
  • 15. APPLICATIONS •UNDO/REDO: It can also be used for performing UNDO/REDO operations. •For example, we have an editor in which we write 'a', then 'b', and then 'c'; therefore, the text written in an editor is abc. •So, there are three states, a, ab, and abc, which are stored in a stack. There would be two stacks in which one stack shows UNDO state, and the other shows REDO state. •If we want to perform UNDO operation, and want to achieve 'ab' state, then we implement pop operation.
  • 16. APPLICATIONS •Recursion: The recursion means that the function is calling itself again. To maintain the previous states, the compiler creates a system stack in which all the previous records of the function are maintained. •DFS(Depth First Search): This search is implemented on a Graph, and Graph uses the stack data structure. •Backtracking: Suppose we have to create a path to solve a maze problem. If we are moving in a particular path, and we realize that we come on the wrong way. In order to come at the beginning of the path to create a new path, we have to use the stack data structure.
  • 17. APPLICATIONS •Expression conversion: Stack can also be used for expression conversion. This is one of the most important applications of stack. •Memory management: The stack manages the memory. The memory is assigned in the contiguous memory blocks. The memory is known as stack memory as all the variables are assigned in a function call stack memory. The memory size assigned to the program is known to the compiler. When the function is created, all its variables are assigned in the stack memory. When the function completed its execution, all the variables assigned in the stack are released.
  • 18.
  • 19. ARRAY IM PLEM ENTATIONOF STACK A[1] A[2] …. …. …. A[n] w y Stack pointer J=n Stack Full Stack pointer J=2 Before Push Stack pointer J=3 After Push After many Push Ops Push X w y x Stack Pointer J=0 Stack Empty Pop After many Pop ops Stack Pointer J=3 Before Pop Stack Pointer J=2 After Pop
  • 20. CHECKINGFOR BALANCED BRACES  ([]({()}[()])) is balanced; ([]({()}[())]) is not  Simple counting is not enough to check balance  You can do it with a stack: going left to right,  If you see a (, [, or {, push it on the stack  If you see a ), ], or }, pop the stack and check whether you got the corresponding (, [, or {  When you reach the end, check that the stack is empty
  • 21.
  • 22. CONVERTING INFIX EXPRESSIONS TOEQUIV ALENT POSTFIX EXPRESSIONS  Infix Expression a+b  Postfix Expression ab+  An infix expression can be evaluated by first being converted into an equivalent postfix expression  Facts about converting from infix to postfix  Operands always stay in the same order with respect to one another  All parentheses are removed
  • 23. ALGORITHM: CONVERTINGINFIX EXPRESSION TO POSTFIX USINGSTACK Suppose X is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix expression Y. 1. Push "(" onto STACK, and add ")" to the end of X. 2. Scan X from left to right and REPEAT Steps 3 to 6 for each element of X UNTIL the STACK is empty : 3. If an operand is encountered, add it to Y. 4. If a left parenthesis is encountered, push it onto STACK. 5. If an operator is encountered, then : (a) Repeatedly pop from STACK and add to Y each operator (on the top of STACK) which has the same precedence as or higher precedence than operator. (b) Add operator to STACK. /* End of If structure * / 6. If a right parenthesis is encountered, then : (a)Repeatedly pop from STACK and add to Y each operator (on the top of STACK) until a left parenthesis is encountered. (b) Remove the left parenthesis. [Do not add the left parenthesis to Y]. /* End of If structure * / /* End of Step 2 loop * / 7. END.
  • 24. A trace of the algorithm that converts the infix expression a - (b + c * d)/e to postfix form
  • 25. A POSTFIX CALCULATOR  When an operand is entered, the calculator  Pushes it onto a stack  When an operator is entered, the calculator  Applies it to the top (one or two, depending on unary or binary operator) operand(s) of the stack  Pops the operands from the stack  Pushes the result of the operation on the stack
  • 26. The action of a postfix calculator when evaluating the expression 2 * (3 + 4) Postfix Notation--- 2 3 4 + *
  • 27. ACKNOWLEDGEMENT  DATA STRUCTURES BYTANENBAUM  INTERNET  COMPUTER SCIENCE C++ A TEXTBOOK FOR CLASS XII -BY SUMITAARORA PREPARED BY- SEEMA KAUSHIK, COMPUTER SCIENCE DEPARTMENT, DAV PUBLIC SCHOOL, SECTOR-14, GURGAON