DS Stacks
DS Stacks
Objectives
Ver. 1.0
Data Structures and Algorithms
Stacks
7 7
7 7
6 6
6 6
6 6
6 6
7 7
7 7
6 6
6 6
6 6
6 6
Ver. 1.0
Data Structures and Algorithms
Defining a Stack
What
A is is
stack a Stack ?
a collection of data items that can be accessed at
only one end, called top.
Items can be inserted and deleted in a stack only at the top.
The last item inserted in a stack is the first one to be
deleted.
Therefore, a stack is called a Last-In-First-Out (LIFO) data
structure.
Ver. 1.0
Data Structures and Algorithms
Identifying the Operations on Stacks
PUSH:are
There It istwo
thebasic
process
operations
of inserting
that aare
newperformed
element on the
stacks:
top of a stack.
PUSH
POP
Push an
Element 1
Empty Stack
Ver. 1.0
Data Structures and Algorithms
Identifying the Operations on Stacks (Contd.)
Push an
Push an
Element 32
Element
3
2
1
Ver. 1.0
Data Structures and Algorithms
Identifying the Operations on Stacks (Contd.)
POP an
Element
3
3 2
2
1
Ver. 1.0
Data Structures and Algorithms
Just a minute
Answer:
LIFO
Ver. 1.0
Data Structures and Algorithms
Just a minute
List down some real life examples that work on the LIFO
principle.
Answer:
Pile of books: Suppose a set of books are placed one over the
other in a pile. When you remove books from the pile, the
topmost book will be removed first. Similarly, when you have to
add a book to the pile, the book will be placed at the top of the
pile.
Pile of plates: The first plate begins the pile. The second plate
is placed on the top of the first plate and the third plate is
placed on the top of the second plate, and so on. In general, if
you want to add a plate to the pile, you can keep it on the top of
the pile. Similarly, if you want to remove a plate, you can
remove the plate from the top of the pile.
Bangles in a hand: When a person wears bangles, the last
bangle worn is the first one to be removed.
Ver. 1.0
Data Structures and Algorithms
Implementing Stacks
Ver. 1.0
Data Structures and Algorithms
Implementing Stacks (Contd.)
{(a + b) × (c + d) + (c × d)]}
Consider an example.
Suppose the expression is:
{(a + b) × (c + d) + (c × d)]}
Scan the expression from
left to right.
The first entry to be
scanned is ‘{’, which is a left
parenthesis.
Push it into the stack.
{
Ver. 1.0
Data Structures and Algorithms
Implementing Stacks (Contd.)
{(a + b) × (c + d) + (c × d)]}
The next entry to be
scanned is ‘(’, which is a left
parenthesis.
Push it into the stack.
The next entry is ‘a’, which
is an operand. Therefore, it
is discarded.
The next entry is ‘+’, which
is an operator. Therefore, it
is discarded. (
{
The next entry is ‘b’, which
is an operand. Therefore, it
is discarded.
Ver. 1.0
Data Structures and Algorithms
Implementing Stacks (Contd.)
{(a + b) × (c + d) + (c × d)]}
The next entry to be
scanned is ‘)’, which is a
right parenthesis
POP the topmost entry from
the stack.
Match the two brackets.
( )
Brackets Matched
(
{
Ver. 1.0
Data Structures and Algorithms
Implementing Stacks (Contd.)
{(a + b) × (c + d) + (c × d)]}
The next entry to be
scanned is ‘)’, which is a
right parenthesis.
POP the topmost element
from the stack.
Match the two brackets.
( )
Brackets Matched
(
{
Ver. 1.0
Data Structures and Algorithms
Implementing Stacks (Contd.)
{(a + b) × (c + d) + (c × d)]}
The next entry to be
scanned is ‘)’, which is a
right parenthesis.
POP the topmost element
from the stack.
Match the two brackets.
( )
Brackets Matched
(
{
Ver. 1.0
Data Structures and Algorithms
Implementing Stacks (Contd.)
{(a + b) × (c + d) + (c × d)]}
The next entry to be
scanned is ‘]’, which is a
right parenthesis.
POP the topmost element
from the stack.
Match the two brackets.
{ ]
Ver. 1.0
Data Structures and Algorithms
Implementing a Stack Using an Array
A
Tostack is similar
implement to a list
a stack usingin which insertion and deletion is
an array:
allowed onlyanatarray:
Declare one end.
Therefore, similar to a //list,
int Stack[5]; stack size
Maximum can needs
be implemented using
to be specified in
both arrays and linked lists. // advance
Declare a variable, top to hold the index of the topmost
element in the stacks:
int top;
Initially, when the stack is empty, set:
top = –1
Ver. 1.0
Data Structures and Algorithms
Implementing a Stack Using an Array (Contd.)
PUSH an element 3
0 1 2 3 4
Stack
Ver. 1.0
Data Structures and Algorithms
Implementing a Stack Using an Array (Contd.)
1. Increment top by 1.
PUSH an element 3
0 1 2 3 4
Stack 10
Ver. 1.0
Data Structures and Algorithms
Implementing a Stack Using an Array (Contd.)
1. Increment top by 1.
PUSH an element 3
0 1 2 3 4
Stack 10
top = 0
Ver. 1.0
Data Structures and Algorithms
Implementing a Stack Using an Array (Contd.)
1. Increment top by 1.
PUSH an element 3
0 1 2 3 4
Stack 3 10 Item pushed
top = 0
Ver. 1.0
Data Structures and Algorithms
Implementing a Stack Using an Array (Contd.)
1. Increment top by 1.
PUSH an element 8
0 1 2 3 4
Stack 3 10
top = 0
Ver. 1.0
Data Structures and Algorithms
Implementing a Stack Using an Array (Contd.)
1. Increment top by 1.
PUSH an element 8
0 1 2 3 4
Stack 3 10
top = 0
top = 1
Ver. 1.0
Data Structures and Algorithms
Implementing a Stack Using an Array (Contd.)
1. Increment top by 1.
PUSH an element 8
0 1 2 3 4
Stack 3 10 8 Item pushed
top = 1
Ver. 1.0
Data Structures and Algorithms
Implementing a Stack Using an Array (Contd.)
1. Increment top by 1.
PUSH an element 5
0 1 2 3 4
Stack 3 10 8
top = 1
Ver. 1.0
Data Structures and Algorithms
Implementing a Stack Using an Array (Contd.)
1. Increment top by 1.
PUSH an element 5
0 1 2 3 4
Stack 3 10 8
top = top
1 =2
Ver. 1.0
Data Structures and Algorithms
Implementing a Stack Using an Array (Contd.)
1. Increment top by 1.
PUSH an element 5
0 1 2 3 4
Stack 3 10 8 5 Item pushed
top = 2
Ver. 1.0
Data Structures and Algorithms
Implementing a Stack Using an Array (Contd.)
1. Increment top by 1.
PUSH an element 1
0 1 2 3 4
Stack 3 10 8 5
top = 2
Ver. 1.0
Data Structures and Algorithms
Implementing a Stack Using an Array (Contd.)
1. Increment top by 1.
PUSH an element 1
0 1 2 3 4
Stack 3 10 8 5
top = 2
top = 3
Ver. 1.0
Data Structures and Algorithms
Implementing a Stack Using an Array (Contd.)
1. Increment top by 1.
PUSH an element 1
0 1 2 3 4
Stack 3 10 8 5 1 Item pushed
top = 3
Ver. 1.0
Data Structures and Algorithms
Implementing a Stack Using an Array (Contd.)
1. Increment top by 1.
PUSH an element 9
0 1 2 3 4
Stack 3 10 8 5 1
top = 3
Ver. 1.0
Data Structures and Algorithms
Implementing a Stack Using an Array (Contd.)
1. Increment top by 1.
PUSH an element 9
0 1 2 3 4
Stack 3 10 8 5 1
top = 3top = 4
Ver. 1.0
Data Structures and Algorithms
Implementing a Stack Using an Array (Contd.)
1. Increment top by 1.
PUSH an element 9
0 1 2 3 4
Stack 3 10 8 5 1 9 Item pushed
top = 4
Ver. 1.0
Data Structures and Algorithms
Implementing a Stack Using an Array (Contd.)
1. Increment top by 1.
PUSH an element 2
0 1 2 3 4
Stack 3 10 8 5 1 9
top = 4
Ver. 1.0
Data Structures and Algorithms
Implementing a Stack Using an Array (Contd.)
1. Increment top by 1.
PUSH an element 2
0 1 2 3 4
Stack 3 10 8 5 1 9
top = 4top = 5
Ver. 1.0
Data Structures and Algorithms
Implementing a Stack Using an Array (Contd.)
1. Increment top by 1.
PUSH an element 2
0 1 2 3 4
Stack 3 10 8 5 1 9 Stack overflow
top = 5
Ver. 1.0
Data Structures and Algorithms
Implementing a Stack Using an Array (Contd.)
Theavoid
To stackthe
has beenoverflow,
stack implemented
you 1. Increment
If top = MAX top–by1: 1.
a. Display “Stack
in an to
need array of size
check 5. stack full
for the 2. Store the
Full”value to be
condition
Therefore,before pushing
you cannot an element
store more pushed
b. Exitat index top in
the array. Top now
into
thanthe stack. in the stack.
5 elements 2. contains
Increment the top
index
by 1of the
Let us modify the algorithm to check topmost element.
3. Store the value to be
for this condition. pushed at index top in
the array
0 1 2 3 4
Stack 3 10 8 5 1 9
Ver. 1.0
Data Structures and Algorithms
Implementing a Stack Using an Array (Contd.)
Ver. 1.0
Data Structures and Algorithms
Just a minute
Answer:
top
Ver. 1.0
Data Structures and Algorithms
Implementing a Stack Using a Linked List
Writealgorithm
The an algorithm to implement
for POP
PUSH operation
operationthe PUSH
isisas and POP
asfollows:
follows:
operations
1. Allocate using
If top = NULL: a Linked
memory List.node.
for the new
2. Assign
a. Display
value“Stack
to the data
Underflow:
field ofCannot
the newdelete
node.from an empty
3. Makestack”
the next field of the new node point to top.
4. b. Exit
Make top point to the new node.
2. Make a variable/pointer tmp point to the topmost node.
3. Retrieve the value contained in the topmost node.
4. Make top point to the next node in sequence.
5. Release memory allocated to the node marked by tmp.
Ver. 1.0
Data Structures and Algorithms
Activity: Implementing a Stack Using an Array
Problem Statement:
Write a program to implement a stack by using an array that
can store five elements.
Ver. 1.0
Data Structures and Algorithms
Activity: Implementing a Stack Using a Linked List
Problem Statement:
Write a program to implement a stack by using a linked list.
Ver. 1.0
Data Structures and Algorithms
Just a minute
Answer:
When a stack is implemented as a linked list, there is no upper
bound limit on the size of the stack. Therefore, there will be no
stack full condition in this case.
Ver. 1.0
Data Structures and Algorithms
Just a minute
Answer:
2. At the beginning of the list
Ver. 1.0
Data Structures and Algorithms
Applications of Stacks
Ver. 1.0
Data Structures and Algorithms
Implementing Function Calls
Ver. 1.0
Data Structures and Algorithms
Implementing Function Calls (Contd.)
void F1()
Assuming these instructions at the
{
given locations in the memory.
1100 int x;
1101 x = 5;
1102 F2(x);
1103 print(x);
}
void F2(int x)
{
1120 x = x + 5;
1121 F3(x);
1122 print(x);
}
void F3(int x)
{
1140 x = x × 2;
1141 print x;
}
Ver. 1.0
Data Structures and Algorithms
Implementing Function Calls (Contd.)
void F1()
{ The execution starts from
1100 int x; function F1
1101 x = 5;
1102 F2(x);
1103 print(x);
}
void F2(int x)
{
1120 x = x + 5;
1121 F3(x);
1122 print(x);
}
void F3(int x)
{
1140 x = x × 2;
1141 print x;
}
Ver. 1.0
Data Structures and Algorithms
Implementing Function Calls (Contd.)
void F1()
{
1100 int x;
1101 x = 5;
1102 F2(x);
1103 print(x);
}
void F2(int x)
{
1120 x = x + 5;
1121 F3(x);
1122 print(x);
}
void F3(int x)
{
1140 x = x × 2;
1141 print x;
}
Ver. 1.0
Data Structures and Algorithms
Implementing Function Calls (Contd.)
void F1() x=5
{
1100 int x;
1101 x = 5;
1102 F2(x);
1103 print(x);
}
void F2(int x)
{
1120 x = x + 5;
1121 F3(x);
1122 print(x);
}
void F3(int x)
{
1140 x = x × 2;
1141 print x;
}
Ver. 1.0
Data Structures and Algorithms
Implementing Function Calls (Contd.)
void F1() x=5
{
1100 int x;
1101 x = 5;
1102 F2(x);
1103 print(x);
}
void F2(int x)
{
1120 x = x + 5;
1121 F3(x); 1103, x = 5
1122 print(x);
} Address and the local variable of F1
void F3(int x)
{
1140 x = x × 2;
1141 print x;
}
Ver. 1.0
Data Structures and Algorithms
Implementing Function Calls (Contd.)
void F1() x=5
10
{
1100 int x;
1101 x = 5;
1102 F2(x);
1103 print(x);
}
void F2(int x)
{
1120 x = x + 5;
1121 F3(x); 1103, x = 5
1122 print(x);
}
void F3(int x)
{
1140 x = x × 2;
1141 print x;
}
Ver. 1.0
Data Structures and Algorithms
Implementing Function Calls (Contd.)
void F1() x = 10
{
1100 int x;
1101 x = 5;
1102 F2(x);
1103 print(x);
}
void F2(int x)
{
1120 x = x + 5; 1122, x = 10
1121 F3(x); 1103, x = 5
1122 print(x);
} Address and the local variable of F2
void F3(int x)
{
1140 x = x × 2;
1141 print x;
}
Ver. 1.0
Data Structures and Algorithms
Implementing Function Calls (Contd.)
void F1() 20
x = 10
{
1100 int x;
1101 x = 5;
1102 F2(x);
1103 print(x);
}
void F2(int x)
{
1120 x = x + 5; 1122, x = 10
1121 F3(x); 1103, x = 5
1122 print(x);
} Address and the local variable of F2
void F3(int x)
{
1140 x = x × 2;
1141 print x;
}
Ver. 1.0
Data Structures and Algorithms
Implementing Function Calls (Contd.)
void F1() 20
x = 10
{
1100 int x;
1101 x = 5;
1102 F2(x);
1103 print(x);
}
void F2(int x) 1122, x = 10
{
1120 x = x + 5; 1122, x = 10
1121 F3(x); 1103, x = 5
1122 print(x);
}
void F3(int x)
{
1140 x = x × 2;
1141 20
print x;
}
Ver. 1.0
Data Structures and Algorithms
Implementing Function Calls (Contd.)
void F1() 5
x = 10
{
1100 int x;
1101 x = 5;
1102 F2(x);
1103 print(x);
}
void F2(int x) 1103, x = 5
{
1120 x = x + 5;
1121 F3(x); 1103, x = 5
1122 print(x);
}
void F3(int x)
{
1140 x = x × 2;
1141 20
print 10
x;
}
Ver. 1.0
Data Structures and Algorithms
Implementing Function Calls (Contd.)
void F1() x=5
{
1100 int x;
1101 x = 5;
1102 F2(x);
1103 print(x);
}
void F2(int x)
{
1120 x = x + 5;
1121 F3(x);
1122 print(x);
}
void F3(int x)
{
1140 x = x × 2;
1141 20
print 10
x; 5
}
Ver. 1.0
Data Structures and Algorithms
Maintaining the UNDO list for an Application
Ver. 1.0
Data Structures and Algorithms
Checking the Nesting of Parentheses in an Expression
Ver. 1.0
Data Structures and Algorithms
Evaluating Expressions
Ver. 1.0
Data Structures and Algorithms
Activity: Implementing a Stack using a Linked List
Problem Statement:
Write a program that accepts an infix expression, and then
converts it into a postfix expression. You can assume that the
entered expression is a valid infix expression.
Ver. 1.0
Data Structures and Algorithms
Summary
Ver. 1.0
Data Structures and Algorithms
Summary (Contd.)
Ver. 1.0