STACK ADT
Jun Y. Ercia
The Stack ADT
Objectives
• examine stack processing
• define a stack abstract data type
• demonstrate how a stack can be used to solve problems
• examine various stack implementations
• compare stack implementations
The Stack ADT
Data
 a collection whose elements are added and removed
from one end, called the top of the stack.
 a stack is a Last In, First Out (LIFO) data structure
Example
• Stack of plates in a cafeteria
Note ▪ items are removed from a stack in the
reverse order from the way they were
inserted
▪ It is not legal to access any item other
than the one that is at the top of the
stack!
Operations on a Stack
Stack Errors
1. Underflow – occurs when trying to pop (or peek at) an empty stack
2. Overflow – occurs when trying to push onto an already full stack
Uses of Stacks in Computing
Stack is useful for any kind of problem involving LIFO data
1. Calculators:
− Convert infix expressions to postfix to make evaluation easier
− Evaluate postfix expressions
2. Compilers:
− to convert infix expressions to postfix, to make translation to lower level language easier
3. Word processors, editors, etc:
− to check expressions or strings of text for matching parentheses
− to implement undo operations
▪ keeps track of the most recent operations
▪ why are they kept in reverse order?
Implementation of Stack ADT
1. Array implementation of Stack
▪ The entire stack is represented by an array together with a pointer top pointing to the next available cell in the array.
▪ An empty stack is represented with top pointing to the first cell in the array.
▪ Suitable for applications in which the maximum stack size is known ahead of time.
Code for adding an item to the top of the stack
Code for removing the item most recently
added
Code for determining whether the stack is
empty
Code for determining whether the stack is full
Code for making the stack empty
Code for retrieving the item most recently
added
Implementation of Stack ADT
1. Linked List implementation of Stack
▪ The entire stack is represented by a linked list of nodes together with
a pointer top pointing to the first node of the linked list.
▪ An empty stack is represented with top pointing NULL.
▪ Suitable for applications in which the maximum stack size is not
known beforehand.
Note ▪ With a linked-list representation,
overflow will not happen (unless you
exhaust memory, which is another
kind of problem)
▪ Underflow can happen, and should be
handled.
Code for adding an item to the top of the stack
Code for removing the item most recently
added
Code for determining whether the stack is
empty
Code for making the stack empty
Code for retrieving the item most recently
added

Lesson 3 - Stack ADT.pdf

  • 1.
  • 2.
    The Stack ADT Objectives •examine stack processing • define a stack abstract data type • demonstrate how a stack can be used to solve problems • examine various stack implementations • compare stack implementations
  • 3.
    The Stack ADT Data a collection whose elements are added and removed from one end, called the top of the stack.  a stack is a Last In, First Out (LIFO) data structure Example • Stack of plates in a cafeteria Note ▪ items are removed from a stack in the reverse order from the way they were inserted ▪ It is not legal to access any item other than the one that is at the top of the stack!
  • 4.
  • 5.
    Stack Errors 1. Underflow– occurs when trying to pop (or peek at) an empty stack 2. Overflow – occurs when trying to push onto an already full stack
  • 6.
    Uses of Stacksin Computing Stack is useful for any kind of problem involving LIFO data 1. Calculators: − Convert infix expressions to postfix to make evaluation easier − Evaluate postfix expressions 2. Compilers: − to convert infix expressions to postfix, to make translation to lower level language easier 3. Word processors, editors, etc: − to check expressions or strings of text for matching parentheses − to implement undo operations ▪ keeps track of the most recent operations ▪ why are they kept in reverse order?
  • 7.
    Implementation of StackADT 1. Array implementation of Stack ▪ The entire stack is represented by an array together with a pointer top pointing to the next available cell in the array. ▪ An empty stack is represented with top pointing to the first cell in the array. ▪ Suitable for applications in which the maximum stack size is known ahead of time.
  • 8.
    Code for addingan item to the top of the stack
  • 9.
    Code for removingthe item most recently added
  • 10.
    Code for determiningwhether the stack is empty
  • 11.
    Code for determiningwhether the stack is full
  • 12.
    Code for makingthe stack empty
  • 13.
    Code for retrievingthe item most recently added
  • 14.
    Implementation of StackADT 1. Linked List implementation of Stack ▪ The entire stack is represented by a linked list of nodes together with a pointer top pointing to the first node of the linked list. ▪ An empty stack is represented with top pointing NULL. ▪ Suitable for applications in which the maximum stack size is not known beforehand. Note ▪ With a linked-list representation, overflow will not happen (unless you exhaust memory, which is another kind of problem) ▪ Underflow can happen, and should be handled.
  • 15.
    Code for addingan item to the top of the stack
  • 16.
    Code for removingthe item most recently added
  • 17.
    Code for determiningwhether the stack is empty
  • 18.
    Code for makingthe stack empty
  • 19.
    Code for retrievingthe item most recently added