Definition of Stack: A Stack Is A in Which Insertions and Deletions Are Allowed Only Called The
Definition of Stack: A Stack Is A in Which Insertions and Deletions Are Allowed Only Called The
Definition of Stack: A Stack Is A in Which Insertions and Deletions Are Allowed Only Called The
/HEAT.
When we define a stack as an ADT (or Abstract Data Type), then we are
only interested in knowing the stack operations from the user point of
view.
DATA
PR1MAY STACK OPERATIONS
DATA
□
~
push(1)
□ push(2)
□ push(3)
□ pop()
□ isFull)
•
isFull() = FALS E
Array implementation
ARRAY IMPLEMENTATION
For this, we will keep a variable top which will keep track of the
last inserted element or the topmost element in an array.
EMPTY STACK
.
For the push operation:
# top is incremented by I.
# New element is pushed at the position top.
PUSH(1)
push(2)
push(3)
push(5)
push(10)
push(11)
1 2 3 5 10 11
push(2)
push(3)
push(S)
push(10)
push(11)
SERIES OF PUSH AND POP OPERATIONS
push(2)
push(3) Not possible. There is not
push(5) enough space in the stack.
push(10) This state is called the
overflow state and the
push(11) new element can't be
push(12) pushed.
10 11
] } u r
push(2)
push(3)
push(5)
push(10)
push(11)
pop
pop
Question 1 IF the sequence of operations - push (1), push (2), pop, push (1),
push (2), pop, pop, pop, push (2), pop are performed on a stack,
the sequence ot popped out values
(A) 7, 8, 9, 5, ( B) 5, 9, 6, 7,8
6 ( D) 9, 8, 7, 5, 6 ISRO CS 92017
(C) 7, 8, 9, 6, 5
Question 2.
Stack A has the entries a, b, c (with a on top). Stack B is
empty. An entry popped out of stack A can be printed immediately or
pushed to stack B. An entry popped out of the stack B can be only be
printed. In this
arrangement, which of the following permutations of a, b, c are not possible?
(A) b, a, c (B) b, c, a
ISRO CS 2018
(C) c, a, b (D) a, b, c
Simple Answer • Use linked list when the size of the stack is not
known in advance.
Size is known in
stack 1
advance.
stack No limitation
on the number
of nodes we
can" create:)
We will select the beginning of the linked list as the top of the stack.
stack
We will select the beginning of the linked list as the top of the stack.
stack
For pop operation, everytime the first node of the linked list will be
deleted. This automatically holds because the top pointer is pointing to the
first node
of the linked list.
Let's try to delete the first node of the linked list.
stack
•
head
tcD
Time complexity of adding a node at the end: 0(1)
stack
head
stack
head
stack 50
head
0(1) 0(1)
tack
.
underflow occurs when top is equal to NU LL.
}
return 0;
}
void stack_freespace()
{
float per;
per=((size-I )-top)* 100/size;
printf("stack free percentage is %f'',per);
}
void is_full()
{
if(top==size-1)
printf("stack is overflow");
else
printf("stack is not full");
}
void is_empty()
{
if(top==- I)
{
printf("stack is underflow");
}
else
{
printf("stack is not empty");
}
}
void stack_top()
{
printf("the top of the stack element is %d" ,stack_array[top]);
}
void stack_print()
{
inti;
if(top==- I)
{
printf("stack is empty");
}
else
{
printf("the stack elements are:");
for(i=top;i>=O;i--)
{
printfU"%d\t",stack_array[i]);
}
}
}
void push(int ele)
{
if(size- I ==top)
{
printf("\n stack is overflow");
}
else
{
top=top+ 1;
stack_array[top]=ele;
}
}
int pop()
{
int value;
if(top==- I)
{
printf(" stack underflow");
}
else
{
value=stack_array[top];
top=top-1;
}
return value;
}
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
} *stack_head;
void print();
int ch,ele;
while(l)
scanf("¾d" ,&ch);
switch(ch)
scanf("¾d" ,&ele);
push(ele);
break;
case 2:print();
break;
case 3 :pop();
break;
return O;
}
void print()
if(stack_head==NULL)
else
node temp=stack_head;
while(temp!=NULL)
printfU"%d\t",temp->data);
temp=temp->link;
}
void push(int ele)
if(stack_head==NULL)
stack_head=(node*)malloc(sizeof(node));
if(stack_head== NULL)
stack_head->data=ele;
stack_head->link=NULL;
else
node *top=(node*)malloc(sizeof(node));
if(top==NULL)
printf("stack overflow");
exit(O);
top->data=ele;
top->link=stack_head;
stack_head=top;
}}
void pop()
if(stack_head==NULL)
exit(O);
else
node temp=stack_head;
temp=temp->link;
stack_head=temp;
}
Stack applications
PRECEDENCE OF OPERATORS
Priority
(Exponentiation) 1 (Highest)
-.-··
(Multiplication) and /
(Division]
A 1 £
I1!1!l 10 + 4 5 = 70
hrsl
Maltiolication
10 +4{5=
3 +5 7 - 42
-ii
Operators Priority
(Exponentiation 1 (Highest)
)
3 +56-7
254 + 10 - 9
365 k
52 k 7 + 10 I 10
3 + 5 * (7 4) AN
2
8 k
(5 i\
4 + 2) - 6 A
2 I (9 k
3)
Two ways to represent an expression:
Infix Notation
WHAT IS AN INFIX EXPRESSION?
Infix Notation: A + B
The operators are written in between there operands.
sample
L I - ' - • -
A+B/C
.
The expression literally
means
first add A and B, then divide the result by C
MOE INFORMATION
Earnele
A+B/C
The usual rule says that the division must be performed before
addition and subtraction.
A +B/C'D-E/(F+G)
A AB/CD-E/(F+G)
A+B/CD-E/(F+G),
The above expression requires multiple scans and every time we have to
reach a different place in an expression for evaluation,
A+B/C'DE/(F+G)
lr
Post f ix Notation
..
\WHAT IS A POSTFIX NOTAT10N?
Notation: AB +
ABC/ +
l
Infix Expression
Step-1
A+ B / C * D - E / (FG+)
Step-2
A + BC/ D - E / (FG+)
Step-3
A +[BC/ D - E / (FG+)
• I
Operand 2
A + BC/D - E / (FG+)
Step-4
Step-5
ABC/D*+ - EFG+/
Step-6
Postfix Expression
ABC/D+EFG+/•
.
• I
I
.
. i
•
. . i-i
A+B/CD-E/(F+G) ABC/D+EFG+/•
ALGORITHM
h
can the syntals cf the expression from left to right and for each symbul,
do the following:
a. If symbol is an operand
Print that symbol onto the screen
b. I+ symbol is a left parenthesis
a Push it an the stack,
c. If symbol is a right parenthesis
m Popi all the oneratnr Frum the stark uintn the first left
parenthesis and print them on the screen_.
a Discard the left and right
parentheses, d. If symbol is an operator
TF the prwcdrnc n" the operators in the stack arr greater than tr
equal to the current uperalur, then
Pop the operators out of the stack and print them onto
the screen, and push the current nprratar onto the stack,
Output: 7
output: 7535
7+53/51+(3-2)
Output:
7+53/51+(3-2)
Output: 75351/+32-¢ %
Example2:
Evolution of postfix Expression
Begin
if ch is an operator 0 , then
res:= b 0 a
done
End
Example:
Let the given expression be "2 31+ 9-". We scan all elements one by one.
1) Scan '2', it's a number, so push it to stack. Stack contains '2'
2) Scan '3', again a number, push it to stack, stack now contains '2 3'
(from bottom to top)
3) Scan '1, again a number, push it to stack, stack now contains '2 31
4) Scan '*', it's an operator, pop two operands from stack, apply the operator
on operands, we get 3*1 which results in 3. We push the result '3' to stack.
The stack now becomes '2 3'.
5) Scan '+', it's an operator, pop two operands from stack, apply the +
operator on operands, we get 3 + 2 which results in 5. We push the result '5'
to stack. The stack now becomes '5'.
6) Scan '9', it's a number, we push it to the stack. The stack now becomes '5
9'.
7) Scan '-', it's an operator, pop two operands from stack, apply the --
operator on operands, we get 5 - 9 which results in -4. We push the result '-4'
to the stack. The stack now becomes '-4'.
8) There are no more elements to scan, we return the top element from
the stack (which is the only element left in a stack).