SlideShare a Scribd company logo
Introduction
To
Stack
Introduction
 A stack is a non-primitive linear data structure.
 It is an ordered list in which addition of new data
item and deletion of already existing data items done
from only one end, known as top tacks (TOP).
 As all the deletion and insertion in a stack is done
from top of the stack, the last added element will be
the first to be removed from the stack.
 That is the reason why stack is also called LAST-IN-
FIRST-OUT (LIFO) type of list.
Introduction
 Example 1: A common model of a stack is plates
in a marriage party. Fresh plates are “pushed” onto
to the top and “popped” off the top.
 Example 2: Some of you may eat biscuits. If you
assume only one side of the cover is open and
biscuits are taken off one by one from one side.
Introduction
 Whenever a stack is created the stack base remains
fixed, as a new element is added to the stack from
the top, the top goes on increasing.
 Conversely as the top most elements of the stack
is removed the stack top is decrementing.
 Show the next slide figure to various stages of
stack top during insertion and during deletion.
Introduction
 Stack top increases during insertion
Stack empty
top=-1
4
3
2
1
0 10
Insert first element
Top=0
4
3
2
1
0
20
10
Top=1
Insert second element
0
1
2
3
4
Introduction
 Stack top decreases during deletion
Element 10 deleted
4
3
2
1
0 10
Element 20 deleted
Top=0
4
3
2
1
0
20
10
Stack Initially
0
1
2
3
4
Top=1
Top=-1
Stack Implementation
 Stack can be implemented in two ways:
 Static implementation
 Dynamic implementation
 Static implementation uses arrays to create stack.
 Static implementation though a very simple
technique but is not a flexible way of creation, as
the size of stack has to be declared during program
design, after that the size cannot be varied.
 Moreover static implementation is not too efficient
with respect to memory utilization.
Stack Implementation
 As the declaration of array is done before the start
of the operation, now if there are too few elements
to be stored in the stack the statically allocated
memory will be wasted.
 On the other hand if there are more number of
elements to be stored in the stack then we can’t be
able to change the size of array to increase its
capacity.
Stack Implementation
 Dynamic implementation is also called linked
list representation and uses pointers to implement
the stack type of data structure.
Operations on Stack
 The basic operation that can be performed on
stack are as follows:
 PUSH :The process of adding a new element to
the top of stack is called PUSH operation. when
new element will be inserted at the top after every
push operation that top is incremented by one. In
case the array is full and no new element can be
accommodated, it is called STACK-FULL
condition. This condition is called STACK
OVERFLOW.
Operations on Stack
 POP: The process of deleting an element from the
top of stack is called POP operation. After every pop
operation the stack is decremented by one. If there is
no element on the stack and the pop is performed
then this will result into STACK UNDERFLOW
condition.
 PEEP: If one is interested only about an information
stored at some location in a stack then peep
operation is required. In short we can say extract any
position information from the stack.
 UPDATE: Update operation is required when the
content of some location in a stack is to be changed.
Stack Terminology
 MAXSIZE: This term is not standard one, we use
this term to refer the maximum size of stack.
 TOP: This term refers to the top stack (TOS). The
stack top is used to check stack overflow or
underflow conditions. Initially TOP stores -1. this
assumption is taken so that whenever an element
is added to the stack the TOP is first incremented
and then the item is inserted into the location
currently indicated by the TOP.
Stack Terminology
 STACK UNDERFLOW: This is the situation
when the stack contains no element. At this point
the top of stack is present at the bottom of the
stack.
 STACK OVERFLOW: This is the situation when
the stack becomes full, and no more elements can
be pushed onto the stack. At this point the stack
top is present at the highest location of the stack.
Applications of Stack
 There are various applications of Stack:
 Recursion: Recursion is an important facility
in many programming language, such as
PASCAL and C ect.
 Some machine are also known which use built-
in stack hardware called ‘stack machine’.
 Representation of Polish Notation
Recursion:
 Recursion is the name gives to a technique for
defining a function in terms of itself.
 If is define as “a function call itself, thus chain of
process occurs.”
 There are two important conditions that must be
satisfied by any recursive procedure:
 Each time a procedure calls itself, it must be
“nearer” in some sense solution.
 There must be a decision criterion for stopping
the process or computation.
Recursion:
 There are three popular implementation of
recursive computations:

Calculation of factorial value
 Quick sort

Tower of Hanoi problem
 Very simple example is to find Factorial value for
an integer n:
n!=n*(n-1)*(n-2)*……..*3*2*1
n!=n*(n-1)!
Recursion:
 What is the simple implementation of
factorial calculation:
1. fact=1
2. For(i=1 to N) do
3. Fact=i*fact
4. End for
5. Return(fact)
6. Stop
Recursion:
 Now let us see the recursive definition of the same.
1. if (N==0)
2. fact=1
3. else
4. fact=N*FACTORIAL(N-1)
5. end if
6. return (fact)
7. Stop
 This recursive definition to implementation using
stack show in word file.
Tower of Hanoi Problem
 Another complex recursive problem is the tower of
Hanoi problem.
 This problem has a historical basis in the ritual of
ancient Vietnam.
 The problem can be described as below:
 Suppose, there are three pillars A,B,C. there are N
discs of decreasing size so that no two discs are of
the same size.
 Initially all the discs are stacked on one pillar in
their decreasing order of size.
Tower of Hanoi Problem
 Let this pillar be A. other two pillars are empty.
 The problem is to move all the discs from one
pillar to other using as auxiliary so that
 Only one disc may be moved at a time.
 A disc may be moved from any pillar to
another.
 At no time can a larger disc be placed on a
smaller disc.
Tower of Hanoi Problem
 The solution of this problem is: Move N discs
from Pillar A to C via the pillar B means:
 Move first (n-1) discs from pillar A to B
 Move the disc from pillar A to C
 Move all (n-1) discs from pillar B to C
 This problem implementation to show in
word file.
Polish Notation
 The process of writing the operators of an
expression either before their operands or after
them is called the polish notation.
 The honors of its discoverer, the polish
mathematician JAN LUKSIEWICZ.
 The fundamental property of polish notation is that
the order in which the operations are to be
performed is completely determined by the
positions of the operators and operands in the
expression.
Polish Notation
 The polish notation are classified into three
categories.
 These are:
 Infix notation
 Prefix notation
 Postfix notation
Introduction
 Operator Precedence Table:
Operator Name Operator Highest Precedence Association
All types of
Brackets
(),[],{} 1 Left-Right
Exponential ^ or $ or
↑
2 Right-Left
Mulitplication/
Devision
*,/ 3 Left-Right
Addition/
Subtraction
+,- 4 Left-Right
Examples
 Infix expression to postfix form:
 A*B+C
=(AB*)+C
=T+C :AB*=T
=TC+
=AB*C+ :puts the value of T
Examples
 A*B+C/D
=(AB*)+C/D
=T+C/D :AB*=T
=T+(CD/) :CD/=S
=T+S
=TS+
=AB*CD/+ :put the value of T & S
Examples
 (A+B)/(C-D)
=(AB+)/(C-D)
=(AB+)/(CD-)
=T/S :T=AB+ & S=CD-
=TS/
=AB+CD-/ :put the value of T & S
Examples
 (A+B)*C/D
=(AB+)*C/D
=T*C/D :T=AB+
=(TC*)/D :S=TC*
=S/D
=SD/
=TC*D/ :put the value of S
=AB+C*D/ :put the value of T
Examples
 To solves these examples:
 (A+B)*C/D+E^F/G

Ans: AB+C*D/EF^G/+
 A-B/(C*D^E)

Ans: ABCDE^*/-
 (a + b c d) * (e + f / d)
↑ ↑

Ans:abcd +efd/+*
↑↑
Examples
 Infix expression to prefix form:
 A*B+C
=(*AB)+C
=T+C :T=*AB
=+TC
=+*ABC :put the value of T
Examples
 A/B^C+D
=A/(^BC)+D
=A/T+D:T=^BC
=(/AT)+D
=S+D :S=/AT
=+SD :put the value of S
=+/ATD :put the value of T
=+/A^BCD
Examples
 (A*B+(C/D))-F
=(A*B+(/CD))-F
=(A*B+T)-F :T=/CD
=((*AB)+T)-F
=(S+T)-F :S=*AB
=(+ST)-F :R=+ST
=R-F
=-RF
=-+STF :put the value of R
=-+*ABTF :put the value of S
=-+*AB/CDF :put the value of T
Examples
 Postfix expression to Infix form: to evaluate
expression from left to right
 AB*C+
=(A*B)C+
=TC+ :A*B=T
=T+C
=A*B+C :put the value of T
Examples
 ABC/+D-
=A(B/C)+D-
=AT+D-:T=B/C
=(A+T)D-
=SD- :S=A+T
=S-D
=A+T-D:put the value of S
=A+B/C-D :put the value of T
Examples
 AB+C*D/
=(A+B)C*D/
=TC*D/ :T=A+B
=(T*C)D/
=SD/ :S=T*C
=S/D
=T*C/D :put the value of S
=(A+B)*C/D :put the value of T
Examples
 Prefix expression to Infix form: to evaluate
expression from right to left
 +*ABC
=+(A*B)C
=+TC :T=A*B
=T+C
=A*B+C :put the value of T
Examples
 -/A^BCD
=-/A(B^C)D
=-/ATD :T=B^C
=-(A/T)D
=-SD :S=A/T
=S-D
=A/T-D :put the value of S
=A/B^C-D :put the value of T
Examples
 Postfix Expression Evaluation: to evaluate
expression from left to right
 345*+
=3(4*5)+
=3 20 +
=3+20
=23
Examples
 Prefix Expression Evaluation: to evaluate
expression from right to left
 +*213
=+(2*1)3
=+ 2 3
=2+3
=5

More Related Content

PPT
Data structure lecture7
PPTX
Unit I.pptx
PPT
The Stack in data structures .ppt
PPTX
Chapter 5-stack.pptx
PPTX
Introduction To Stack
PPTX
U3.stack queue
PPTX
Stacks in DATA STRUCTURE
PPTX
Stacks and queues using aaray line .pptx
Data structure lecture7
Unit I.pptx
The Stack in data structures .ppt
Chapter 5-stack.pptx
Introduction To Stack
U3.stack queue
Stacks in DATA STRUCTURE
Stacks and queues using aaray line .pptx

Similar to stack data structure , applications, advantages (20)

PPTX
Introduction to information about Data Structure.pptx
PPTX
Stack and its Applications : Data Structures ADT
PPTX
Stacks IN DATA STRUCTURES
PPT
Stacks
PPT
Stack a Data Structure
PDF
Chapter 5 Stack and Queue.pdf
PPTX
Stack - Data Structure - Notes
PPTX
DS-UNIT 3 FINAL.pptx
PPTX
5.-Stacks.pptx
PDF
LEC3-DS ALGO(updated).pdf
PPTX
PPT Lecture 3.2.1 stack newxxxxxxxxxx.pptx
PDF
The Stack (Data Structccccccccccccccccccc
PPT
week 7,8,10,11 alll files included from .ppt
PPTX
Data Structures Stack and Queue Data Structures
PPTX
PPTX
Stacks Data structure.pptx
PPT
Stack data structures with definition and code
PPTX
Data structures
PPTX
Stack and its operations, Queue and its operations
PPTX
STACK.pptx
Introduction to information about Data Structure.pptx
Stack and its Applications : Data Structures ADT
Stacks IN DATA STRUCTURES
Stacks
Stack a Data Structure
Chapter 5 Stack and Queue.pdf
Stack - Data Structure - Notes
DS-UNIT 3 FINAL.pptx
5.-Stacks.pptx
LEC3-DS ALGO(updated).pdf
PPT Lecture 3.2.1 stack newxxxxxxxxxx.pptx
The Stack (Data Structccccccccccccccccccc
week 7,8,10,11 alll files included from .ppt
Data Structures Stack and Queue Data Structures
Stacks Data structure.pptx
Stack data structures with definition and code
Data structures
Stack and its operations, Queue and its operations
STACK.pptx
Ad

More from rajinooka (10)

PPT
Security topic in OS from Galvin book chp 14
PPT
Deadlocks PPT Presentation from galvin book
PPTX
Basic Tree Data Structure BST Traversals .pptx
PPT
Linear Datsructure_stacks_Data Structure_PPT.ppt
PPT
stack data structure and its applications , advantages, disadvantages etc
PDF
l4a.pdf
PDF
80x86_2.pdf
PPT
UNIT-V.ppt
PPT
Introduction to LaTeX - Dumitrescu.ppt
PPTX
8-Queens Problem.pptx
Security topic in OS from Galvin book chp 14
Deadlocks PPT Presentation from galvin book
Basic Tree Data Structure BST Traversals .pptx
Linear Datsructure_stacks_Data Structure_PPT.ppt
stack data structure and its applications , advantages, disadvantages etc
l4a.pdf
80x86_2.pdf
UNIT-V.ppt
Introduction to LaTeX - Dumitrescu.ppt
8-Queens Problem.pptx
Ad

Recently uploaded (20)

PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PPTX
Current and future trends in Computer Vision.pptx
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPT
Project quality management in manufacturing
PPT
Mechanical Engineering MATERIALS Selection
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
Geodesy 1.pptx...............................................
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
additive manufacturing of ss316l using mig welding
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Current and future trends in Computer Vision.pptx
bas. eng. economics group 4 presentation 1.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Project quality management in manufacturing
Mechanical Engineering MATERIALS Selection
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Lecture Notes Electrical Wiring System Components
Geodesy 1.pptx...............................................
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
CYBER-CRIMES AND SECURITY A guide to understanding
additive manufacturing of ss316l using mig welding
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Foundation to blockchain - A guide to Blockchain Tech
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...

stack data structure , applications, advantages

  • 2. Introduction  A stack is a non-primitive linear data structure.  It is an ordered list in which addition of new data item and deletion of already existing data items done from only one end, known as top tacks (TOP).  As all the deletion and insertion in a stack is done from top of the stack, the last added element will be the first to be removed from the stack.  That is the reason why stack is also called LAST-IN- FIRST-OUT (LIFO) type of list.
  • 3. Introduction  Example 1: A common model of a stack is plates in a marriage party. Fresh plates are “pushed” onto to the top and “popped” off the top.  Example 2: Some of you may eat biscuits. If you assume only one side of the cover is open and biscuits are taken off one by one from one side.
  • 4. Introduction  Whenever a stack is created the stack base remains fixed, as a new element is added to the stack from the top, the top goes on increasing.  Conversely as the top most elements of the stack is removed the stack top is decrementing.  Show the next slide figure to various stages of stack top during insertion and during deletion.
  • 5. Introduction  Stack top increases during insertion Stack empty top=-1 4 3 2 1 0 10 Insert first element Top=0 4 3 2 1 0 20 10 Top=1 Insert second element 0 1 2 3 4
  • 6. Introduction  Stack top decreases during deletion Element 10 deleted 4 3 2 1 0 10 Element 20 deleted Top=0 4 3 2 1 0 20 10 Stack Initially 0 1 2 3 4 Top=1 Top=-1
  • 7. Stack Implementation  Stack can be implemented in two ways:  Static implementation  Dynamic implementation  Static implementation uses arrays to create stack.  Static implementation though a very simple technique but is not a flexible way of creation, as the size of stack has to be declared during program design, after that the size cannot be varied.  Moreover static implementation is not too efficient with respect to memory utilization.
  • 8. Stack Implementation  As the declaration of array is done before the start of the operation, now if there are too few elements to be stored in the stack the statically allocated memory will be wasted.  On the other hand if there are more number of elements to be stored in the stack then we can’t be able to change the size of array to increase its capacity.
  • 9. Stack Implementation  Dynamic implementation is also called linked list representation and uses pointers to implement the stack type of data structure.
  • 10. Operations on Stack  The basic operation that can be performed on stack are as follows:  PUSH :The process of adding a new element to the top of stack is called PUSH operation. when new element will be inserted at the top after every push operation that top is incremented by one. In case the array is full and no new element can be accommodated, it is called STACK-FULL condition. This condition is called STACK OVERFLOW.
  • 11. Operations on Stack  POP: The process of deleting an element from the top of stack is called POP operation. After every pop operation the stack is decremented by one. If there is no element on the stack and the pop is performed then this will result into STACK UNDERFLOW condition.  PEEP: If one is interested only about an information stored at some location in a stack then peep operation is required. In short we can say extract any position information from the stack.  UPDATE: Update operation is required when the content of some location in a stack is to be changed.
  • 12. Stack Terminology  MAXSIZE: This term is not standard one, we use this term to refer the maximum size of stack.  TOP: This term refers to the top stack (TOS). The stack top is used to check stack overflow or underflow conditions. Initially TOP stores -1. this assumption is taken so that whenever an element is added to the stack the TOP is first incremented and then the item is inserted into the location currently indicated by the TOP.
  • 13. Stack Terminology  STACK UNDERFLOW: This is the situation when the stack contains no element. At this point the top of stack is present at the bottom of the stack.  STACK OVERFLOW: This is the situation when the stack becomes full, and no more elements can be pushed onto the stack. At this point the stack top is present at the highest location of the stack.
  • 14. Applications of Stack  There are various applications of Stack:  Recursion: Recursion is an important facility in many programming language, such as PASCAL and C ect.  Some machine are also known which use built- in stack hardware called ‘stack machine’.  Representation of Polish Notation
  • 15. Recursion:  Recursion is the name gives to a technique for defining a function in terms of itself.  If is define as “a function call itself, thus chain of process occurs.”  There are two important conditions that must be satisfied by any recursive procedure:  Each time a procedure calls itself, it must be “nearer” in some sense solution.  There must be a decision criterion for stopping the process or computation.
  • 16. Recursion:  There are three popular implementation of recursive computations:  Calculation of factorial value  Quick sort  Tower of Hanoi problem  Very simple example is to find Factorial value for an integer n: n!=n*(n-1)*(n-2)*……..*3*2*1 n!=n*(n-1)!
  • 17. Recursion:  What is the simple implementation of factorial calculation: 1. fact=1 2. For(i=1 to N) do 3. Fact=i*fact 4. End for 5. Return(fact) 6. Stop
  • 18. Recursion:  Now let us see the recursive definition of the same. 1. if (N==0) 2. fact=1 3. else 4. fact=N*FACTORIAL(N-1) 5. end if 6. return (fact) 7. Stop  This recursive definition to implementation using stack show in word file.
  • 19. Tower of Hanoi Problem  Another complex recursive problem is the tower of Hanoi problem.  This problem has a historical basis in the ritual of ancient Vietnam.  The problem can be described as below:  Suppose, there are three pillars A,B,C. there are N discs of decreasing size so that no two discs are of the same size.  Initially all the discs are stacked on one pillar in their decreasing order of size.
  • 20. Tower of Hanoi Problem  Let this pillar be A. other two pillars are empty.  The problem is to move all the discs from one pillar to other using as auxiliary so that  Only one disc may be moved at a time.  A disc may be moved from any pillar to another.  At no time can a larger disc be placed on a smaller disc.
  • 21. Tower of Hanoi Problem  The solution of this problem is: Move N discs from Pillar A to C via the pillar B means:  Move first (n-1) discs from pillar A to B  Move the disc from pillar A to C  Move all (n-1) discs from pillar B to C  This problem implementation to show in word file.
  • 22. Polish Notation  The process of writing the operators of an expression either before their operands or after them is called the polish notation.  The honors of its discoverer, the polish mathematician JAN LUKSIEWICZ.  The fundamental property of polish notation is that the order in which the operations are to be performed is completely determined by the positions of the operators and operands in the expression.
  • 23. Polish Notation  The polish notation are classified into three categories.  These are:  Infix notation  Prefix notation  Postfix notation
  • 24. Introduction  Operator Precedence Table: Operator Name Operator Highest Precedence Association All types of Brackets (),[],{} 1 Left-Right Exponential ^ or $ or ↑ 2 Right-Left Mulitplication/ Devision *,/ 3 Left-Right Addition/ Subtraction +,- 4 Left-Right
  • 25. Examples  Infix expression to postfix form:  A*B+C =(AB*)+C =T+C :AB*=T =TC+ =AB*C+ :puts the value of T
  • 26. Examples  A*B+C/D =(AB*)+C/D =T+C/D :AB*=T =T+(CD/) :CD/=S =T+S =TS+ =AB*CD/+ :put the value of T & S
  • 27. Examples  (A+B)/(C-D) =(AB+)/(C-D) =(AB+)/(CD-) =T/S :T=AB+ & S=CD- =TS/ =AB+CD-/ :put the value of T & S
  • 28. Examples  (A+B)*C/D =(AB+)*C/D =T*C/D :T=AB+ =(TC*)/D :S=TC* =S/D =SD/ =TC*D/ :put the value of S =AB+C*D/ :put the value of T
  • 29. Examples  To solves these examples:  (A+B)*C/D+E^F/G  Ans: AB+C*D/EF^G/+  A-B/(C*D^E)  Ans: ABCDE^*/-  (a + b c d) * (e + f / d) ↑ ↑  Ans:abcd +efd/+* ↑↑
  • 30. Examples  Infix expression to prefix form:  A*B+C =(*AB)+C =T+C :T=*AB =+TC =+*ABC :put the value of T
  • 31. Examples  A/B^C+D =A/(^BC)+D =A/T+D:T=^BC =(/AT)+D =S+D :S=/AT =+SD :put the value of S =+/ATD :put the value of T =+/A^BCD
  • 32. Examples  (A*B+(C/D))-F =(A*B+(/CD))-F =(A*B+T)-F :T=/CD =((*AB)+T)-F =(S+T)-F :S=*AB =(+ST)-F :R=+ST =R-F =-RF =-+STF :put the value of R =-+*ABTF :put the value of S =-+*AB/CDF :put the value of T
  • 33. Examples  Postfix expression to Infix form: to evaluate expression from left to right  AB*C+ =(A*B)C+ =TC+ :A*B=T =T+C =A*B+C :put the value of T
  • 35. Examples  AB+C*D/ =(A+B)C*D/ =TC*D/ :T=A+B =(T*C)D/ =SD/ :S=T*C =S/D =T*C/D :put the value of S =(A+B)*C/D :put the value of T
  • 36. Examples  Prefix expression to Infix form: to evaluate expression from right to left  +*ABC =+(A*B)C =+TC :T=A*B =T+C =A*B+C :put the value of T
  • 37. Examples  -/A^BCD =-/A(B^C)D =-/ATD :T=B^C =-(A/T)D =-SD :S=A/T =S-D =A/T-D :put the value of S =A/B^C-D :put the value of T
  • 38. Examples  Postfix Expression Evaluation: to evaluate expression from left to right  345*+ =3(4*5)+ =3 20 + =3+20 =23
  • 39. Examples  Prefix Expression Evaluation: to evaluate expression from right to left  +*213 =+(2*1)3 =+ 2 3 =2+3 =5