Professional Documents
Culture Documents
2
Stack
push
push pop
pop
AAstack
stackisisopen
openatat one
oneendend
(the
(thetop)
top) only.
only. You
Youcan
can
push
pushentry
entryonto
ontothe
thetop,
top, or
or
pop
pop the
the top
topentry
entry out
out of
ofthe
the top
top
stack.
stack. C
B
Note
Notethat
that you
you cannot
cannot A
add/extract
add/extractentry
entryin
inthe
the
middle
middle of
of the
thestack.
stack. bottom
bottom
3
Last-in First-out (LIFO)
A B C
C
Insert(A);
Insert(B);
C The
Thelastlastone
one
Insert(C); B
B inserted
insertedin in isis the
the
A A
A first
first one
one removed
removed
B out!
out! (LIFO)
(LIFO)
C
Remove();
Remove();
Remove(); When
Whenwe weinsert
insert entries
entries
B onto
ontothe
thestack
stackand
andthen
then
A A remove
removethem
them out
out one
onebyby
one,
one, we
wewill
willget
getthe
theentries
entries
in
inreverse
reverseorder.
order.
4
03/10/21 Dr. Kazi A. Kalpoma 5
Abstract Data Type ADT
• A data type whose properties (domain and
operations) are specified independently of any
particular implementation
#define
#definestack_size
stack_size 6;
6; ? 5 (stacksize-1)
? 4
int
inttop;
top; ? 3
int ? 2
ints[stack_size];
s[stack_size];
? 1
? 0
s
top = 2
stack
Initialization of top index of Stack
stack_initialize()
stack_initialize() ? 5 (stacksize-1)
{{ ? 4
top ? 3
top==-1;
-1; ? 2
}} ? 1
? 0
top E--1ntry
top = 0
It can be initialize in different way too,
Suppose
top = 0;
Push(X)
void
voidpush(int
push(int X)X)
{{
if(top
if(top==
== stack_size-1)
stack_size-1)
cout<<“Overflow:
cout<<“Overflow: stack stackisisfull”;
full”;
else ? 5 (stacksize-1)
else ? 4
{{
? 3
top++;
top++; /increase
/increasetop topby
by11 ? 2
s[top]
s[top] == X;
X; /insert
/insertthe
theitem
itemon
ontop
top ? 1
}} top Xtop=0 0 1
}}
top++; entry
if(top== stack_size) S[++top] = X; 11
stack[top] = X;
cout <<”overflow: stack is full”;
else
{s[top] = X;
top++;}
Pop()
void
voidpop()
pop() //pop
//popfunction
functionisisnot
notreturning
returning
{{
if(top<0)
if(top<0) ? 5
cout<<"underflow:
cout<<"underflow: stack
stackisisempty”;
empty”; ? 4
else
else ? 3
{{ ? 2
top--; ? 1
top--; ? 0
}} E--1ntry
top entry
}}
top= 10
if(top==0)
cout <<"underflow: stack is empty”;
else
{
top--;}
Pop() //pop function is returning the top element
int
int pop()
pop()
{{
if(top<0)
if(top<0)
cout<<"underflow:
cout<<"underflow: stack
stackisisempty”;
empty”;
else ? 5
else ? 4
{{ ? 3
int
int XX==s[top];
s[top]; ? 2
top--;
top--; ? 1
return
returnX;X; ? 0
}} top E--1ntry
entry
}} top= 10
Top()
int
int top()
top()//top
//top function
functionisisreturning
returning top
topvalue
value
{{
return
return s[top];
s[top]; //but
//but not
notreduce
reducetoptop index
index
}}
? 5
? 4
? 3
? 2
top= 10
top R 1
K 0
int Top() //top function is returning top value
{ entry
return s[top-1]; //but not reduce top index
}
Other operations
int IsEmpty() ? 5 (stacksize-1)
{ ? 4
return ( top < 0 ); ? 3
} ? 2
? 1
int IsFull() ? 0
{
return ( top >= stack_size-1);stack
} top = 2
int IsEmpty()
{
return ( top == 0 );
}
int IsFull()
{
return ( top > stack_size-1);
} 15
// ……Program for stack implementation
? 5 (stacksize-1)
#include <iostream> ? 4
using namespace std; 4 3
8 2
// here add all the initializations and 7 1
//functions’ definitions of stack. 9 0
int main() s
{
top = 2
push(9);
push(7);
push (8);
push (4);
pop ( );
push (5);
pop ( );
pop ( );
cout<< " top element of stack is = " << top() << endl;
cout << " top index is = " << top << endl;
}
// ……Program for stack implementation
? 5 (stacksize-1)
#include <iostream> ? 4
using namespace std; ? 3
8 2
// here add all the initializations and 7 1
//functions’ definitions of stack. 9 0
#include <iostream>
#include <stack>
int main()
{ stack <int> st;
st.push(9);
st.push(7);
st.push (8);
st.push (9); top element of stack is = 7
st.pop ( ); top index is = 1
st.push (5);
st.pop ( );
st.pop ( );
cout<< " top element of stack is = " << st.top() << endl;
bout << " top index is = " << st.size()-1 << endl;
}
Traversing a stack
void show() ?
?
{ top D 3
int i; C 2
OUTPUT:
D C B A
21
03/10/21 Dr. Kazi A. Kalpoma 22
03/10/21 Dr. Kazi A. Kalpoma 23
Application of stack
• Syntax parsing, Expression evaluation and
Expression conversion.
• Towers of Hanoi
• Validation
1. There is an equal number of closing and opening
parentheses
2. Every closing parenthesis is preceded by a matching
opening parenthesis
Algorithm
• Whenever an opening is encountered, PUSH()
on to stack
• Whenever a closing is encountered,
– If stack is empty, expression is invalid.
– If stack is nonempty, POP() the stack and check
with corresponding closing parenthesis. If match
occurs continue. Otherwise expression is invalid.
• When the end of expression is reached, stack
must be empty; otherwise expression invalid.
Symbol Stack
[ [
( [(
Example: A [(
+ [(
[(A+B)-{(C+D)-E}] B [(
) [
- [
{ [{
( [{(
C [{(
+ [{(
D [{(
) [{
- [{
E [{
} [
]
Algebraic Expression
• An algebraic expression is a legal combination of
operands and the operators.
• Operand is the quantity (unit of data) on which a
mathematical operation is performed.
• Operand may be a variable like x, y, z or a constant like
5, 4,0,9,1 etc.
• Operator is a symbol which signifies a mathematical or
logical operation between the operands. Example of
familiar operators include +,-,*, /, ^ , %
• Considering these definitions of operands and operators
now we can write an example of expression as
x+y*z
Infix, Postfix and Prefix Expressions
• INFIX: From our schools times we have been familiar with the
expressions in which operands surround the operator, e.g.
x+y, 6*3 etc this way of writing the Expressions is called infix
notation.
Delimiters
• Sub expression within delimiters is treated as a
single operand, independent from the remainder of
the expression.
– (a + b) * (c – d) / (e – f)
WHY PREFIX and POSTFIX ?
623+-382/+*2^3+)
5
6
65-382/+*2^3+) 1382/+*2^3+)
8 2 4
3 3 8 3
1 1 1 3 1
1
Cont. of Evaluation- 623+-382/+*2^3+
134+*2^3+)
4
3 7
1 1
72^3+) 493+) )
2 3
= 52
7 49 49 52
7
Algorithm: To evaluate a prefix value
head
top 1
1 7 5 2
7
5
2
Stack Operation: List
int pop()
{ int x = head->data;
Node* p = head;
head = head->Next;
delete p;
return x;
}
head
1 7 5 2
top 7 p
5
2
Stack Operation: List
void push(int x)
{
Node* newNode = new Node();
newNode->data = x;
newNode->Next = head;
head = newNode;
}
head
top 9
7 5 2
7
5
newNode 9
2
push(9)
Stack Operation: List
int top()
{
return ( head->data );
}
int IsEmpty()
{
return ( head == NULL );
}