2210034_2ndLabreport
2210034_2ndLabreport
Write a C program to implement a stack using an array with push, pop, isfull, and
isempty operations
Theory: A stack is a LIFO (Last-In-First-Out) data structure where elements are added or
removed only from the top. Push adds an element, pop removes it, isFull checks if the stack is
full, and isEmpty checks if it’s empty. Stacks are useful in expression evaluation, undo actions,
and memory management.
Code:
#include<stdio.h>
int max=5;
int stack[5],top=-1;
int isfull(){
return top==max-1;
if(!isfull()){
stack[++top]=data;
else
int isempty(){
return top==-1;
}
int pop(){
if(!isempty()){
return stack[top--];
else
return -1;
int main(){
push(9);push(10);push(20);push(30);push(40);
printf("%d\n",stack[i]);
printf("%d\n",stack[i]);
Output: 9
10
20
30
40
Popped element is 40
Popped element is 30
Popped element is 20
10
DIscussion: This problem implements a stack using an array with operations like push, pop,
isFull, and isEmpty. A stack follows LIFO (Last-In-First-Out), where elements are added or
removed from the top. isFull checks if the stack is at maximum capacity, and isEmpty verifies if
it’s empty. Stacks are essential for tasks like expression evaluation, memory management, and
algorithm design.
Algorithm: A stack is implemented using an array, where push adds an element to the top, and
pop removes the top element. isFull checks if the stack is full before adding, and isEmpty
ensures the stack is not empty before removing. The operations follow the LIFO principle.
2. Write a C program to convert an infix expression into postfix expression and evaluate
the expression using stack.
Theory: Infix expressions (like 3 + 5) use operators between operands, while postfix
expressions (like 3 5 +) place operators after operands. Converting from infix to postfix
eliminates the need for parentheses and operator precedence by using a stack to hold operators.
The conversion uses precedence rules, and once converted, a stack is used to evaluate the postfix
expression by applying operators to operands in the correct order. This method is useful in
evaluating mathematical expressions and is a core concept in compiler design.
Code:
#include <stdio.h>
#include <ctype.h>
#define MAX 50
char stack[MAX];
stack[++top] = value;
char pop() {
return stack[top--];
return 0;
int i = 0, k = 0;
char ch;
ch = infix[i];
if (isdigit(ch)) {
postfix[k++] = ch;
postfix[k++] = pop();
pop();
} else {
postfix[k++] = pop();
push(ch);
i++;
postfix[k++] = pop();
postfix[k] = '\0';
int stack[MAX];
char ch;
ch = postfix[i];
if (isdigit(ch)) {
stack[++top] = ch - '0';
} else {
op2 = stack[top--];
op1 = stack[top--];
i++;
return stack[top];
int main() {
infixToPostfix(infix, postfix);
return 0;
}
Output:
Result: 20
Discussion:
This problem converts an infix expression to postfix and evaluates it. Infix uses standard
notation, while postfix eliminates parentheses and precedence issues. A stack converts infix to
postfix by handling operators and parentheses, and another stack evaluates the postfix by
processing operators and operands. This demonstrates stack use in expression parsing and
evaluation.
Algorithm: An infix expression is converted to postfix using a stack to handle operators and
parentheses. Operands are added directly to the postfix expression, while operators are pushed
or popped based on precedence. The postfix expression is then evaluated using a stack to
manage intermediate results.
3. Write a C program that reverses a stack using only stack operations push and pop.
Theory: Reversing a stack involves rearranging its elements using only push and pop
operations. The process removes elements recursively and inserts them back at the bottom in
reverse order. This demonstrates how basic stack operations can solve complex tasks while
adhering to the Last-In-First-Out (LIFO) principle.
Code:
#include <stdio.h>
#define MAX 50
int stack[MAX];
stack[++top] = value;
int pop() {
return stack[top--];
if (top == -1) {
push(value);
} else {
insertAtBottom(value);
push(temp);
void reverseStack() {
if (top != -1) {
reverseStack();
insertAtBottom(temp);
}
void displayStack() {
printf("\n");
int main() {
push(10);
push(20);
push(30);
push(40);
displayStack();
reverseStack();
displayStack();
return 0;
}
Output:
Original Stack: 10 20 30 40
Reversed Stack: 40 30 20 10
Discussion: Reversing a stack rearranges elements using only basic operations like push and
pop. The solution removes all elements recursively and re-inserts them at the bottom in reverse
order. This highlights the flexibility of stack operations in manipulating data order efficiently.
Algorithm: The stack is reversed by recursively removing all elements and re-inserting them at
the bottom. This process uses only push and pop operations, ensuring the order of elements is
reversed in the stack.