05 Stacks and Its Implementation
05 Stacks and Its Implementation
1
Recap
Introduction to data
structures & algorithms
Pointers, structures & arrays
List ADT
Array lists
Linked Lists (single,
double & circular)
Queues
2
Source: https://pic1.zhimg.com/v2-5dd21bea8b50af177e31351ed0a2ea8d_r.jpg?
source=172ae18b
Stacks
3
Today‘s lecture
What is a Stack?
Operations on a Stack
Initialize a stack
Push (insert) an element
Pop (delete) an element
Read the top element
status: empty, full
Applications of Stack
Reverse a sequence of number using stack
Keep track of function calls in a program
Testing the correctness of a mathematical expression
Converting a mathematical expression from infix to postfix and prefix notations
Solving a mathematical expression in postfix form
4
What is a Stack?
Definition:
Conceptually, a stack is a data structure that allows
adding and removing elements in a particular
order
More specifically, in an order such that items can be
inserted or deleted to the collection of already inserted
items from one end only, called the top of the stack
5
Stack
6
Stack
Stack is said to have “First In, Last Out" (FILO) or
“Last In, First Out” (LIFO) behaviour meaning
that the first item added to a stack will be the
last item removed from a stack
Example:
Which is the first coin to pick up from
the stack of gold coins?
7
Basics of stack
Size: The number of elements on the stack
8
Stack Operations
Primarily two operations performed on one end
only
9
Stack Operations
Push Concerns
What if when the stack is full?
If the push operation is called when a stack is already full, the
condition is called “stack overflow”
Pop Concerns
What if the stack is empty
If the pop operation is called when a stack is already empty, the
condition is called “stack underflow”
Solution:
Before any push, check if the stack is already filled or not
If it is filled then generate error message e.g., Stack Overflow
10
Stack Operations
push(item) // Push an item onto the stack
14
Implementation of Stack in C++
15
Take a sequence: -1.5 2.3 6.7
16
-1.5 2.3 6.7
Top
17
2.3 6.7
Top -1.5
18
6.7
Top 2.3
-1.5
19
Push onto a stack one
Top 6.7 number at a time.
2.3
-1.5
20
Top 6.7
2.3
-1.5
Then pop each number off the stack
21
Top 2.3
-1.5
Then pop each number off the stack
6.7 22
Top -1.5
Then pop each number off the stack
6.7 2.3 23
Top
loop {
if (Stack is empty) then {exit loop}
pop nextNumber off the Stack
output nextNumber
}
}
} 25
Choice of implementation
Array based: Stack Maximum stack size is known
ahead of time
26
Array based stack
When using an array to implement a stack
27
Array based stack
Allocate an array of some size (pre-defined)
Maximum N elements in stack
28
Array based stack operations
int stack[size]; int Pop() {
int top = -1; int element = -1;
If (!isEmpty()) {
bool isEmpty() {
bool isFull() {
return top<0;
return top==size-1;
} }
29
Linked List based implementation
How can we implement Stack
using linked lists?
LIFO
Push = Insert at head\
30
Linked List based implementation
31
Linked List based implementation
void main ()
{
...
stack();
stack.push(5);
...
}
32
Linked List based implementation
33
Practice:
Write one of the unique application of Stack in 2 min
34
Lab Project
• Choose an Interesting topic relevant to DSA
Any Questions?
36