Stack ADT
• A stack is a data structure that stores a linear collection of items
with access limited to a last-in first-out (LIFO) order.
• Adding and removing items is restricted to one end known as
the top of the stack.
Stack
• Putting an item on top of the stack is called push.
• Removing an item is called pop.
LIFO Principle of Stack
• Int stk[10], top=-1
Void Push( stk[10], int n)
Int pop(stk[10])
Int peep(stk[10])
Bool isFull() // overflow
If(top>=n-1) //overflow
Bool isEmpty() //underflow
If(top<=-1) //underflow
• Push (item): Add an given item to the top of a stack.
• Pop(): Removes and returns the top item of the stack, if the
stack is not empty. The next item on the stack becomes the
new top item.
• Stack(): Creates a new empty stack.
• IsEmpty(): Check if the stack is empty or not. Return a
boolean value.
• IsFull(): Check if the stack is full. Return a boolean value.
• Peek(): Get the value of the top element without removing
it.
• Length(): Returns the number of items in the stack.
Stack: Basic Operations
1. A pointer called TOP is used to keep track of the top element in the stack.
2. When initializing the stack, we set its value to -1 so that we can check if the stack is
empty by comparing TOP == -1.
3. On pushing an element, we increase the value of TOP and place the new element in
the position pointed to by TOP.
4. On popping an element, we return the element pointed to by TOP and reduce its
value.
5. Before pushing, we check if the stack is already full
6. Before popping, we check if the stack is already empty
Working of Stack Data Structure
Example: Stack Operations
Implementing the Stack: Using Array in C
Implementing the Stack: Using Single Linked List
• String reversal.
• Expression evaluation/conversion.
• Recursion.
• DFS(Depth First Search).
• UNDO/REDO.
• Backtracking.
Applications of Stack
• The process in which a function calls itself directly or indirectly
is called recursion.
• A recursive function is a function that calls itself during its
execution.
• Best example of a recursive function – factorial.
• n! = n * (n-1)!
Working of Recursive Function
Recursion and Recursive Function
Sum of Natural Numbers Using Recursion
Example of a Recursive Function
Output
Working of a Recursive Sum Function

Lecture#5 - Stack ADT.pptx

  • 1.
  • 2.
    • A stackis a data structure that stores a linear collection of items with access limited to a last-in first-out (LIFO) order. • Adding and removing items is restricted to one end known as the top of the stack. Stack
  • 3.
    • Putting anitem on top of the stack is called push. • Removing an item is called pop. LIFO Principle of Stack
  • 4.
    • Int stk[10],top=-1 Void Push( stk[10], int n) Int pop(stk[10]) Int peep(stk[10]) Bool isFull() // overflow If(top>=n-1) //overflow Bool isEmpty() //underflow If(top<=-1) //underflow
  • 5.
    • Push (item):Add an given item to the top of a stack. • Pop(): Removes and returns the top item of the stack, if the stack is not empty. The next item on the stack becomes the new top item. • Stack(): Creates a new empty stack. • IsEmpty(): Check if the stack is empty or not. Return a boolean value. • IsFull(): Check if the stack is full. Return a boolean value. • Peek(): Get the value of the top element without removing it. • Length(): Returns the number of items in the stack. Stack: Basic Operations
  • 6.
    1. A pointercalled TOP is used to keep track of the top element in the stack. 2. When initializing the stack, we set its value to -1 so that we can check if the stack is empty by comparing TOP == -1. 3. On pushing an element, we increase the value of TOP and place the new element in the position pointed to by TOP. 4. On popping an element, we return the element pointed to by TOP and reduce its value. 5. Before pushing, we check if the stack is already full 6. Before popping, we check if the stack is already empty Working of Stack Data Structure
  • 7.
  • 8.
    Implementing the Stack:Using Array in C
  • 9.
    Implementing the Stack:Using Single Linked List
  • 10.
    • String reversal. •Expression evaluation/conversion. • Recursion. • DFS(Depth First Search). • UNDO/REDO. • Backtracking. Applications of Stack
  • 11.
    • The processin which a function calls itself directly or indirectly is called recursion. • A recursive function is a function that calls itself during its execution. • Best example of a recursive function – factorial. • n! = n * (n-1)! Working of Recursive Function Recursion and Recursive Function
  • 12.
    Sum of NaturalNumbers Using Recursion Example of a Recursive Function Output
  • 13.
    Working of aRecursive Sum Function