0% found this document useful (0 votes)
6 views

2210034_2ndLabreport

The document provides C programs to implement stack operations using arrays, convert infix expressions to postfix, and reverse a stack. It discusses the theory behind stacks, including their LIFO nature and applications in expression evaluation and memory management. Each section includes code examples, outputs, and algorithms explaining the processes involved.

Uploaded by

reavervandal45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

2210034_2ndLabreport

The document provides C programs to implement stack operations using arrays, convert infix expressions to postfix, and reverse a stack. It discusses the theory behind stacks, including their LIFO nature and applications in expression evaluation and memory management. Each section includes code examples, outputs, and algorithms explaining the processes involved.

Uploaded by

reavervandal45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

1.

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;

void push(int data){

if(!isfull()){

stack[++top]=data;

else

printf("Data can't be entered");

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);

for (int i=0;i<=top;i++){

printf("%d\n",stack[i]);

printf("Popped element is %d\n",pop());

printf("Popped element is %d\n",pop());

printf("Popped element is %d\n",pop());

printf("After pop operation\n");

for (int i=0;i<=top;i++){

printf("%d\n",stack[i]);

Output: 9

10

20

30

40

Popped element is 40
Popped element is 30

Popped element is 20

After pop operation

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];

int top = -1;

void push(char value) {

stack[++top] = value;

char pop() {

return stack[top--];

int precedence(char op) {

if (op == '+' || op == '-') return 1;

if (op == '*' || op == '/') return 2;

return 0;

void infixToPostfix(char* infix, char* postfix) {

int i = 0, k = 0;

char ch;

while (infix[i] != '\0') {

ch = infix[i];

if (isdigit(ch)) {

postfix[k++] = ch;

} else if (ch == '(') {


push(ch);

} else if (ch == ')') {

while (top != -1 && stack[top] != '(') {

postfix[k++] = pop();

pop();

} else {

while (top != -1 && precedence(stack[top]) >= precedence(ch))


{

postfix[k++] = pop();

push(ch);

i++;

while (top != -1) {

postfix[k++] = pop();

postfix[k] = '\0';

int evaluatePostfix(char* postfix) {

int stack[MAX];

int top = -1, i = 0, op1, op2;

char ch;

while (postfix[i] != '\0') {

ch = postfix[i];
if (isdigit(ch)) {

stack[++top] = ch - '0';

} else {

op2 = stack[top--];

op1 = stack[top--];

if (ch == '+') stack[++top] = op1 + op2;

if (ch == '-') stack[++top] = op1 - op2;

if (ch == '*') stack[++top] = op1 * op2;

if (ch == '/') stack[++top] = op1 / op2;

i++;

return stack[top];

int main() {

char infix[MAX], postfix[MAX];

printf("Enter an infix expression: ");

fgets(infix, MAX, stdin);

infixToPostfix(infix, postfix);

printf("Postfix Expression: %s\n", postfix);

printf("Result: %d\n", evaluatePostfix(postfix));

return 0;

}
Output:

Enter an infix expression: 2*(4+6)

Postfix Expression: 246+*

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];

int top = -1;


void push(int value) {

stack[++top] = value;

int pop() {

return stack[top--];

void insertAtBottom(int value) {

if (top == -1) {

push(value);

} else {

int temp = pop();

insertAtBottom(value);

push(temp);

void reverseStack() {

if (top != -1) {

int temp = pop();

reverseStack();

insertAtBottom(temp);

}
void displayStack() {

for (int i = 0; i <= top; i++) {

printf("%d ", stack[i]);

printf("\n");

int main() {

push(10);

push(20);

push(30);

push(40);

printf("Original Stack: ");

displayStack();

reverseStack();

printf("Reversed Stack: ");

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.

You might also like