Stack Operations
Stack Operations
h>
int stack[100], i, j, choice=0, n, top=-1;
void push () {
int val;
if (top == n-1 )
printf("\n Overflow");
else {
printf("Enter the value?");
scanf("%d",&val);
top = top +1;
stack[top] = val;
}
}
void pop () {
int ele;
if(top == -1)
printf("Underflow");
else {
ele = stack[top];
void peek () {
if(top == -1)
printf("Underflow");
else
printf("Peeked element is %d\n", stack[top]);
}
void show() {
for (i=top;i>=0;i--)
printf("%d\n",stack[i]);
if(top == -1)
printf("Stack is empty");
}
int main () {
printf("Enter the number of elements in the stack ");
scanf("%d",&n);
printf("*********Stack operations using array*********");
printf("\n----------------------------------------------\n");
while(choice != 4) {
printf("Chose one from the below options...\n");
printf("\n1.Push\n 2.Pop\n 3.Show\n 4.Peek\n 5.Exit");
printf("\n Enter your choice \n");
scanf("%d", &choice);
switch(choice) {
case 1:
push();
break;
case 2:
pop();
break;
case 3:
show();
break;
case 4:
peek();
break;
case 5:
printf("Exiting....");
break;
default:
printf("Please Enter valid choice ");
};
}
return 0;
}
A stack is an ordered collection of items where the addition of new items
and the removal of existing items always take place at the same end.
This end is commonly referred to as the “top”. The working principle of
stack is LIFO [Last In First Out].
#include <stdio.h> #include <stdlib.h>
int top = -1;
int size;
int *stack=NULL;
void create();
void push();
int pop();
bool isEmpty();
bool isFull();
int main() {
printf("STATIC ARRAY (Total Capacity: %d)\n", N);
int choice;
while(1) {
printf("\n Choose any of the following options:\n");
printf(" 4: Check if the stack is empty 5: Check if the stack is full\n\
n");
scanf("%d", &choice);
switch(choice){
case 0: exit(0);
case 1: push(); break;
case 2: pop(); break;
case 3: peek(); break;
case 4: isEmpty(); break;
case 5: isFull(); break;
default: printf("Please choose a correct option!");
}
}
free(stack);
return 0;
}
void create() {
if(stack == NULL) {
printf(“Enter the size of stack\n”);
scanf(“%d”, &size);
stack=(int*)malloc(size*sizeof(int));
}
else
printf(“Already memory for stack is allocated”);
}
void push() {
if(top == N-1)
printf("Overflow State: can't add more elements into the stack\n");
else {
int x;
printf("Enter element to be pushed into the stack: ");
scanf("%d", &x);
top+=1;
*(stack+top) = x;
}
}
int pop() {
if(top == -1)
printf("Underflow State: Stack already empty, can't remove any element\
n");
else {
int x = *(stack+top);
printf("Popping %d out of the stack\n", x);
top-=1;
return x;
}
return -1;
}
int isEmpty() {
if(top == -1) {
printf("Stack is empty: Underflow State\n");
return 1;
}
printf("Stack is not empty\n");
return 0;
}
int isFull() {
if(top == N-1) {
printf("Stack is full: Overflow State\n");
return 1;
}
printf("Stack is not full\n");
return 0;
}
Infix to postfix conversion using stack
Scan all the symbols one by one from left to right in the given Infix
Expression.
If the reading symbol is left parenthesis ‘( ‘, then Push it onto the Stack.
If the reading symbol is right parenthesis ‘)’, then Pop all the contents
of the stack until the respective left parenthesis is popped and append each
popped symbol to Postfix Expression.
If the reading symbol is an operator (+, –, *, /), then Push it onto the
Stack. However, first, pop the operators which are already on the stack
that have higher or equal precedence than the current operator and append
them to the postfix. If an open parenthesis is there on top of the stack then
push the operator into the stack.
If the input is over, pop all the remaining symbols from the stack and
append them to the postfix.
Convert below infix expression to postfix expression using stack: (2-
3+4)*(5+6*7)
Infix Expression Stack Postfix Expression
(2-3+4)*(5+6*7) (
(2-3+4)*(5+6*7) ( 2
(2-3+4)*(5+6*7) (- 2
(2-3+4)*(5+6*7) (- 23
(2-3+4)*(5+6*7) (+ 23-
(2-3+4)*(5+6*7) (+ 23-4
(2-3+4)*(5+6*7) - 23-4+
(2-3+4)*(5+6*7) * 23-4+
(2-3+4)*(5+6*7) *( 23-4+
(2-3+4)*(5+6*7) *( 23-4+5
(2-3+4)*(5+6*7) *(+ 23-4+5
(2-3+4)*(5+6*7) *(+ 23-4+56
(2-3+4)*(5+6*7) *(+* 23-4+56
(2-3+4)*(5+6*7) *(+* 23-4+567
(2-3+4)*(5+6*7) * 23-4+567*+
Convert below infix expression to postfix expression using stack:
((A+B)-C*(D/E))+F
Infix Expression Stack Postfix Expression
((A+B)-C*(D/E))+F (
((A+B)-C*(D/E))+F ((
((A+B)-C*(D/E))+F (( A
((A+B)-C*(D/E))+F ((+ A
((A+B)-C*(D/E))+F ((+ AB
((A+B)-C*(D/E))+F ( AB+
((A+B)-C*(D/E))+F (- AB+
((A+B)-C*(D/E))+F (- AB+C
((A+B)-C*(D/E))+F (-* AB+C
((A+B)-C*(D/E))+F (-*( AB+C
((A+B)-C*(D/E))+F (-*( AB+CD
((A+B)-C*(D/E))+F (-*(/ AB+CD
((A+B)-C*(D/E))+F (-*(/ AB+CDE
((A+B)-C*(D/E))+F (-* AB+CDE/
((A+B)-C*(D/E))+F + AB+CDE/*-
((A+B)-C*(D/E))+F + AB+CDE/*-F
((A+B)-C*(D/E))+F AB+CDE/*-F+
Convert below infix expression to postfix expression using stack:
(a+b)*d+e/(f+a*d)+c
Infix Expression Stack Postfix Expression
(a+b)*d+e/(f+a*d)+c ( -
(a+b)*d+e/(f+a*d)+c ( a
(a+b)*d+e/(f+a*d)+c (+ a
(a+b)*d+e/(f+a*d)+c (+ ab
(a+b)*d+e/(f+a*d)+c - ab+
(a+b)*d+e/(f+a*d)+c * ab+
(a+b)*d+e/(f+a*d)+c * ab+d
(a+b)*d+e/(f+a*d)+c + ab+d*
(a+b)*d+e/(f+a*d)+c + ab+d*e
(a+b)*d+e/(f+a*d)+c +/ ab+d*e
(a+b)*d+e/(f+a*d)+c +/( ab+d*e
(a+b)*d+e/(f+a*d)+c +/( ab+d*ef
(a+b)*d+e/(f+a*d)+c +/(+ ab+d*ef
(a+b)*d+e/(f+a*d)+c +/(+ ab+d*efa
(a+b)*d+e/(f+a*d)+c +/(+* ab+d*efa
(a+b)*d+e/(f+a*d)+c +/(+* ab+d*efad
(a+b)*d+e/(f+a*d)+c +/ ab+d*efad*+
(a+b)*d+e/(f+a*d)+c + ab+d*efad*+/+
(a+b)*d+e/(f+a*d)+c + ab+d*efad*+/+c
(a+b)*d+e/(f+a*d)+c - ab+d*efad*+/+c+
Evaluate the given postfix expression using stack 623+-382/+*2$3+
The below diagram shows the evaluation of postfix expression using
stack. Kindly refer diagram from left to right and then below lines.