2.1stack
2.1stack
Data Structes
Structuring or organizing the data in a specififc
manner so that the different operations on data can
be performed easily.
For Example:
If the student details in the college are not arranged
or organized then getting the details of a student
takes lot of time.
If the students details are organized according to
branch and year we get the details of a student
immediately.
Types of Data Structes
Primitive data structures: They only store the data
no facility to organized. Example: int, char, float
S[top]
S[3]
S[2]
S[1]
S[0]
Using a Stack
What Are Stacks Used For?
Most programming languages use a “call stack” to
implement function calling
When a method is called, its line number and other useful
information are pushed (inserted) on the call stack
When a method ends, it is popped (removed) from the call
stack and execution restarts at the indicated line number in
the method that is now at the top of the stack
Stack Implementation
The stack ADT can be implemented using
a variety of data structures. We will look
at two:
Arrays / static representation
Linked Lists /dynamic reprsentation
Basic Conditions: Stack
Creation of stack
push – insert an item at the top of the stack
pop – remove and return the top item
display– display the elements of the stack
Creation of Stack
int a[SIZE];
4
int top=-1; 3
top=-1
Algorithm for PUSH() operation in Stack using Array:
Step 1: Start
Step 4: Else, Then print "Stack Overflow" i.e, stack is full and
cannot be pushed with another element
Step 5: Stop
Push In push operation top is incremented by 1.
top=-1
Push In push operation top is incremented by 1.
if(top!=SIZE-1)
{ 4
top=-1
Push In push operation top is incremented by 1.
if(top!=SIZE-1)
{ 4
top++;
3
top=0 0
Push In push operation top is incremented by 1.
if(top==SIZE-1)
{ 4
top++;
a[top]=item; 3
}
2
top=0 0 100
Push In push operation top is incremented by 1.
Push(200)
4
top=1 1 200
0 100
Push In push operation top is incremented by 1.
Push(300)
4
top=2 2 300
1 200
0 100
Push In push operation top is incremented by 1.
Push(400)
4
top=3 3 400
2 300
1 200
0 100
Push In push operation top is incremented by 1.
Push(500)
top=4 4 500
3 400
2 300
1 200
0 100
Push In push operation top is incremented by 1.
top=5
Push(600)
4 500
3 400
STACK OVERFLOW
2 300
1 200
0 100
Algorithm for POP() operation in
Stack using Array
Step 1: Start
Step 5: Else, If top is greater than zero the stack is not empty,
then store the value pointed by top in a variable x=a[top] and
decrement top by 1. The popped element is x.
Pop In pop operation top is decremented by 1.
void pop( )
{ top=4 4 500
if(top==-1)
printf("\nStack Underflow“); 3 400
else
{ 2 300
printf("\nDeletedElement is %d”,a[top]);
top--; 1 200
}
} 0 100
Pop In pop operation top is decremented by 1.
else
{ 4
printf("\nDeletedElement is %d”,a[top]);
3 400
top--; top=3
}
2 300
1 200
0 100
Pop In pop operation top is decremented by 1.
Pop()
4
top=2 2 300
1 200
0 100
Algorithm for display() operation
in Stack using Arrays
Step 1: Start
Step 4: Stop
Display
void display()
{ 4
if(top==-1)
printf(”Stack is Empty”); 3
else
for(int i=top;i>=0;i--) top=2 2 300
printf(“%d”,a[i]);
} 1 200
0 100
Display
else
for(int i=top;i>=0;i--) 4
printf(“%d”,a[i]);
3
1 200
0 100
300
Display
else
for(int i=top;i>=0;i--) 4
printf(“%d”,a[i]);
3
top=2 2 300
i=1 1 200
0 100
300 200
Display
else
for(int i=top;i>=0;i--) 4
printf(“%d”,a[i]);
3
top=2 2 300
1 200
i=0 0 100
abc*+d-
Convert the infix expression a+b*(c-d) into postfix
abcd-*+
Convert into Postfix
1. (a/b)/(d%e)-f*g
2. (A+B)*(C-D)^(E*F)
3. ((A+B)+C*D)/((E+F)-G)
4. A+B*C+(D*E+F)*G
Convert into Postfix
1. (a/b)/(d%e)-f*g = ab/de%/fg*-
2. (A+B)*(C-D)^(E*F)=AB+CD-EF*^*
3. ((A+B)+C*D)/((E+F)-G)=AB+CD*+EF+G-/
4. A+B*C+(D*E+F)*G=ABC*+DE*F+G*+
Expression evaluation
Algorithm
1. Use a stack to evaluate an expression in postfix
notation.
2. The postfix expression to be evaluated is scanned
from left to right.
3. Variables or constants are pushed onto the stack.
2.)( a + b - c ) * d – ( e + f )
a b + c – d * e f + -
A=b=c=d=e=f=1
3.)62/3-41*+
Evaluate the postfix expression abc*+d- where a=4, b=2 c=3 and d=1
Substitute the values we get 423*+1-