CS-206 Data
Structures and
Algorithms
Topic: Stacks
Dr. Mariam Nosheen
1
Stacks
Examples of stacks in "real life": Examples of stacks in computing:
• The stack of trays in a cafeteria; • Back/Forward stacks on browsers;
• A stack of plates in a cupboard; • Undo/Redo stacks in Excel or Word;
• A driveway that is only one car wide. • Activation records of method calls;
2
Stacks
Implementation of Stacks 2
Top
• Stack can be implemented by using arrays or link
18
lists.
• An array provides a simple way to implement any 36
stack. However it only allows a fixed data items to be 17
added into the stacks. In static array the size cannot
be changed during execution. Bottom 26
• The other way to implement stacks is link lists. In link Stacks
list, memory is allocated dynamically to store data
items into stacks. Which provide more flexibility.
3
Stacks
Basic Features of Stacks
Following are the basic features of stacks
• Stack is ordered list of items with similar data types.
• Stack is a LIFO data structure(items are accessed in LAST IN FIRST OUT
manner)
• The PUSH operation is used to insert new element into the stack. And
POP operation is used to delete an item from stack. Both PUSH and
POP operations are done only one end of the stack TOP.
• The stack is OVERFLOW state when it is completely full. Where as it is
said to be UNDERFLOW state if it is completely empty.
4
Stacks
5
The PUSH operation involves the following steps
1. Check if the stack is full
Stacks 2. If the stack is full, produce an error message and
exit operation
3. If the stack is not full, make an increment to the
top to print next empty space.
Push and Pop Operations in Stacks
4. Add the new data element into the top position of
the stack.
5. Exit the operation
6
The PUSH operation involves the following steps
1. Check if the stack is full
2. If the stack is full, produce an error message and
Stacks exit operation
3. If the stack is not full, make an increment to the
top to print next empty space.
4. Add the new data element into the top position of
Push and Pop Operations in Stacks the stack.
5. Exit the operation
Top
2
Top 8
Top 8
Top 3 3
3
Empty Stack PUSH “3” PUSH “8” PUSH “2” 7
Stacks
Push and Pop Operations in Stacks
8
The POP operation involves the following steps
1. Check if the stack is empty
Stacks 2. If the stack is empty, produce an error message and
exit operation
3. If the stack is not empty, access the data element
to which the TOP is pointing..
Push and Pop Operations in Stacks
4. Decrease the value of the TOP by 1.
5. Exit the operation
9
The POP operation involves the following steps
1. Check if the stack is empty
2. If the stack is empty, produce an error message and
Stacks exit operation
3. If the stack is not empty, access the data element
to which the TOP is pointing..
4. Decrease the value of the TOP by 1.
Push and Pop Operations in Stacks
5. Exit the operation
Top 2
Top 8 8
Top Top
3 3 3
POP “3” POP “8” POP “2”
10
Stacks
11
Stacks
12
Stacks
Stacks in airline reservation system
Exercise
13