3. [DStructure - Slides] Stack
3. [DStructure - Slides] Stack
Stacks
Objectives
Upon completion you will be able to
1
What is a stack?
n A stack is an abstract data structure (ADT) that contains a
collection of elements.
n It is an ordered group of homogeneous items of elements.
n Elements are added to and removed from the top of the
stack (the most recently added items are at the top of the
stack).
n The last element to be added is the first to be removed
(LIFO: Last In, First Out).
3
n A stack is a data structure in which data is added and
removed at only one end called the top.
2
top
top 8 8
1
top 1
Push(8)
1
7 7 Push(2) 7
2 2 2
pop()
top 8
top 1
1
top 7 7 pop()
2 pop() 7
2
2
OPERATIONS PERFORMED ON STACK
6
Data Structures: A Pseudocode Approach with C 7
Data Structures: A Pseudocode Approach with C 8
Data Structures: A Pseudocode Approach with C 9
10
STACK IMPLEMENTATION
Stack can be implemented in two ways:
1. Static implementation (using arrays)
2. Dynamic implementation (using pointers)
Ø Static implementation using arrays is a very simple
11
STACK USING ARRAYS
12
13
Algorithm for pop
14
15
16
17
18
19
Stack Methods
In C++, the stack class provides various methods to perform
different operations on a stack.
Operation Description
push() adds an element into the stack
pop() removes an element from the stack
top() returns the element at the top of the stack
size() returns the number of elements in the stack
empty() returns true if the stack is empty
20
Add element into the stack
We use the push() method to add an element into a stack. For
example,
#include <iostream>
#include <stack>
using namespace std;
int main() {
// create a stack of strings
stack<string> colors;
30
31
#include<iostream>
#include<stack>
using namespace std;
main(){
stack<int> stk;
if(stk.empty()){
cout << "Stack is empty" << endl;
} else {
cout << "Stack is not empty" << endl;
}
//insert elements into stack
stk.push(10);
stk.push(20);
stk.push(30);
stk.push(40);
stk.push(50);
cout << "Size of the stack: " << stk.size() << endl;
//pop and dispay elements Output
while(!stk.empty()){ Stack is empty
int item = stk.top(); // same as peek operation
stk.pop();
Size of the stack: 5
cout << item << " "; 50 40 30 20 10
}
} 32
33
APPLICATIONS OF STACKS
Some direct applications:
• Conversion of tail-recursive algorithms to iterative ones.
• Evaluation of arithmetic expressions by compilers [infix to postfix
conversion, infix to prefix conversion, evaluation of postfix
expressions]
f1 f1 f2 … fn
10-4
#include<conio.h>
#include<iostream.h>
void fact(int no, int facto)
{
if (no <= 1)
{
//Final computation and returning and restoring address
cout<<“\nThe Factorial is = ”<<facto;
return;
}
else
{
//Partiial computation of the program
facto=facto*no;
//Function call to itself, that is recursion
fact(--no,facto);
}
}
void main()
{
clrscr();
//Initialization of formal parameters, local variables and etc.
factorial=1;
cout<<“\nEnter the No = ”;
cin>>number;
//Starting point of the function, which calls itself
fact(number,factorial);
getch();
• 1. It consumes more storage space because the recursive calls along with
automatic variables are stored on the stack.
• 2. The computer may run out of memory if the recursive calls are not
checked.
• 3. It is not more efficient in terms of speed and execution time.
• 4. According to some computer professionals, recursion does not offer
any concrete advantage over non-recursive procedures/functions.
• 5. If proper precautions are not taken, recursion may result in non-
terminating iterations.
• 6. Recursion is not advocated when the problem can be through iteration.
Recursion may be treated as a software tool to be applied carefully and
selectively.
TOWER OF HANOI
5 Tower
A B C
ILLEGAL
MOVE
The Tower of
Hanoi
5 Tower
A B C
The Tower of
Hanoi
3 Tower
A B C
D E M O 3 TOW E R
The Tower of
Hanoi
3 Tower
A B C
The Tower of
Hanoi
3 Tower
A B C
The Tower of
Hanoi
3 Tower
A B C
The Tower of
Hanoi
3 Tower
A B C
The Tower of
Hanoi
3 Tower
A B C
The Tower of
Hanoi
3 Tower
A B C
The Tower of
Hanoi
3 Tower
7 Moves
A B C
Tower of Hanoi puzzle with n disks can be solved in minimum
2n−1 steps. This presentation shows that a puzzle with 3 disks has
taken 2n - 1 = 7 steps.
Algorithm
To write an algorithm for Tower of Hanoi, first we need to learn
how to solve this problem with lesser amount of disks, say → 1 or
2. We mark three towers with name, source, destination and aux
(only to help moving the disks). If we have only one disk, then it
can easily be moved from source to destination peg.
If we have 2 disks −
A B C
4 TOWER SHOW
The Tower of
Hanoi
4 Tower
A B C
The Tower of
Hanoi
4 Tower
A B C
The Tower of
Hanoi
4 Tower
A B C
The Tower of
Hanoi
4 Tower
A B C
The Tower of
Hanoi
4 Tower
A B C
The Tower of
Hanoi
4 Tower
A B C
The Tower of
Hanoi
4 Tower
A B C
The Tower of
Hanoi
4 Tower
A B C
The Tower of
Hanoi
4 Tower
A B C
The Tower of
Hanoi
4 Tower
A B C
The Tower of
Hanoi
4 Tower
A B C
The Tower of
Hanoi
4 Tower
A B C
The Tower of
Hanoi
4 Tower
A B C
The Tower of
Hanoi
4 Tower
A B C
The Tower of
Hanoi
4 Tower
15 Moves
A B C
The Tower of
Hanoi
5 Tower
A B C
5 TOWER SHOW
The Tower of
Hanoi
5 Tower
A B C
The Tower of
Hanoi
5 Tower
A B C
The Tower of
Hanoi
5 Tower
A B C
The Tower of
Hanoi
5 Tower
A B C
The Tower of
Hanoi
5 Tower
A B C
The Tower of
Hanoi
5 Tower
A B C
The Tower of
Hanoi
5 Tower
A B C
The Tower of
Hanoi
5 Tower
A B C
The Tower of
Hanoi
5 Tower
A B C
The Tower of
Hanoi
5 Tower
A B C
The Tower of
Hanoi
5 Tower
A B C
The Tower of
Hanoi
5 Tower
A B C
The Tower of
Hanoi
5 Tower
A B C
The Tower of
Hanoi
5 Tower
A B C
The Tower of
Hanoi
5 Tower
A B C
The Tower of
Hanoi
5 Tower
A B C
The Tower of
Hanoi
5 Tower
A B C
The Tower of
Hanoi
5 Tower
A B C
The Tower of
Hanoi
5 Tower
A B C
The Tower of
Hanoi
5 Tower
A B C
The Tower of
Hanoi
5 Tower
A B C
The Tower of
Hanoi
5 Tower
A B C
The Tower of
Hanoi
5 Tower
A B C
The Tower of
Hanoi
5 Tower
A B C
The Tower of
Hanoi
5 Tower
A B C
The Tower of
Hanoi
5 Tower
A B C
The Tower of
Hanoi
5 Tower
A B C
The Tower of
Hanoi
5 Tower
A B C
The Tower of
Hanoi
5 Tower
A B C
The Tower of
Hanoi
5 Tower
A B C
The Tower of
Hanoi
5 Tower
31 Moves
A B C
The Tower of Hanoi
}
Discs Moves
1 1
2 3 Un = 2Un-1 + 1
3 7
4 15 This is called a
5 31 recursive function.
6 63
7 127
8 255
n 2n?- 1 RESULTS
TABLE
EXPRESSION
(2 + 14)* 5 2 14 + 5 * * + 2 14 5
2 + 14 * 5 2 14 5 * + + * 2 14 5
(6 – 2) * (5 + 4) 6 2 - 5 4 +* * - 6 2 + 5 4
Algorithm
Suppose P is an arithmetic expression written in infix notation.
the equivalent postfix expression Q.
Infix to postfix conversion
infixVect
(a+b-c)*d–(e+f)
postfixVect
Infix to postfix conversion
stackVect
infixVect
a+b-c)*d–(e+f)
postfixVect
(
Infix to postfix conversion
stackVect
infixVect
+b-c)*d–(e+f)
postfixVect
a
(
Infix to postfix conversion
stackVect
infixVect
b-c)*d–(e+f)
postfixVect
a
+
(
Infix to postfix conversion
stackVect
infixVect
-c)*d–(e+f)
postfixVect
ab
+
(
Infix to postfix conversion
stackVect
infixVect
c)*d–(e+f)
postfixVect
ab+
-
(
Infix to postfix conversion
stackVect
infixVect
)*d–(e+f)
postfixVect
ab+c
-
(
Infix to postfix conversion
stackVect
infixVect
*d–(e+f)
postfixVect
ab+c-
Infix to postfix conversion
stackVect
infixVect
d–(e+f)
postfixVect
ab+c-
*
Infix to postfix conversion
stackVect
infixVect
–(e+f)
postfixVect
ab+c-d
*
Infix to postfix conversion
stackVect
infixVect
(e+f)
postfixVect
ab+c–d*
-
Infix to postfix conversion
stackVect
infixVect
e+f)
postfixVect
ab+c–d*
(
-
Infix to postfix conversion
stackVect
infixVect
+f)
postfixVect
ab+c–d*e
(
-
Infix to postfix conversion
stackVect
infixVect
f)
postfixVect
+ ab+c–d*e
(
-
Infix to postfix conversion
stackVect
infixVect
)
postfixVect
+ ab+c–d*ef
(
-
Infix to postfix conversion
stackVect
infixVect
postfixVect
ab+c–d*ef+
-
Infix to postfix conversion
stackVect
infixVect
postfixVect
ab+c–d*ef+-
Evaluating Postfix Expressions
Figure 7-8
The action of a postfix calculator when evaluating the expression 2 * (3 + 4)
Write algorithm and program to
convert
1- Prefix to Infix Conversion
the Infix Expression is ((b-c)/((p-q)+r))
2- Postfix to Infix conversion
the Infix Expression is (e+f-g)* (h-e)/(s-h+o)
3- Infix to Postfix Conversion
the Postfix Expression is 33*41-/62*+