Module 4 Stack
Module 4 Stack
● Stack Overflow occurs when there is no more space to push new elements onto the
stack because it has exceeded its capacity. This happens when the stack reaches its
maximum limit.
● Stack Underflow happens when there is an attempt to pop an element from an empty
stack, which doesn't have any elements to remove.
Structure A linear data structure. A linear data structure, but follows LIFO
(Last In, First Out) principle.
Insertion/Deletion Can insert or delete at any Can only insert (push) at the top and
position. remove (pop) from the top.
3. How does a stack implemented using a linked list differ from a stack
implemented using an array?
● peek(): This function returns the top element of the stack without removing it.
● pop(): This function removes and returns the top element of the stack.
In postfix and prefix expressions, the order of operations is already defined due
to the position of operators relative to operands. The placement of operators in these
expressions ensures that the operations are carried out in the correct order, making
parentheses unnecessary.
Stacks are used in non-recursive programs to simulate the function call stack manually.
For example, when solving problems like tree traversals, expression evaluation, or
depth-first search, a stack can be used to store intermediate states instead of relying on
recursion.
A multiple stack refers to having multiple stacks within a single data structure, such as
a fixed-size array or memory block. It's useful when you want to manage multiple
collections of data (stacks) but need to use a single contiguous memory space. This is
typically implemented by dividing a memory block into segments for each stack.
Conversion to Postfix:
(a) A - B + C → A B - C +
(b) A * B + C / D → A B * C D / +
(c) (A - B) + C * D / E - C → A B - C D * E / + C -
(d) (A * B) + (C / D) - (D + E) → A B * C D / + D E + -
(e) (A - B) + D / (E + F) * G → A B - D E F + / G * +
(f) (A - 2 * B + C) / D * E + F → A 2 B * - C + D / E * F +
(g) 14 / 7 % 3 - 4 + 9 / 2 → 14 7 / 3 % 4 - 9 2 / +
(a) A - B + C → A B - C +
(b) A * B + C / D → A B * C D / +
(c) (A - B) + C * D / E - C → A B - C D * E / + C -
(d) (A * B) + (C / D) - (D + E) → A B * C D / + D E + -
(e) (A - B) + D / (E + F) * G → A B - D E F + / G * +
(f) (A - 2 * B + C) / D * E + F → A 2 B * - C + D / E * F +
(g) 14 / 7 % 3 - 4 + 9 / 2 → 14 7 / 3 % 4 - 9 2 / +
(a) AB + C * D - → (A + B) * C - D
(b) ABC * + D - → A + (B * C) - D
(a) *-+ABCD
○ Infix Expression: (A + B) * (C - D)
(b) +-a*BCD
○ Infix Expression: a + (B * C) - D
12. Convert the expression given below into its corresponding postfix
expression and then evaluate it. Also, write a program to evaluate a postfix
expression.
Postfix Expression:
10 7 5 - 10 + 2 / +
Evaluation:
1. 10 7 5 - → 7 - 5 = 2 → 10 2 10 + 2 / +
2. 10 2 10 + → 2 + 10 = 12 → 10 12 2 / +
3. 10 12 2 / → 12 / 2 = 6 → 10 6 +
4. 10 6 + → 10 + 6 = 16
Result: 16
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX 50
int stack[MAX];
int top = -1;
int pop() {
if (top == -1) {
printf("Stack Underflow\n");
return -1;
} else {
return stack[top--];
}
}
int main() {
char expression[] = "10 7 5 - 10 + 2 / +";
printf("Result: %d\n", evaluatePostfix(expression));
return 0;
}
13. Write a function that accepts two stacks. Copy the contents of the first
stack in the second stack. Note that the order of elements must be
preserved. (Hint: use a temporary stack)
#include <stdio.h>
#include <stdlib.h>
#define MAX 50
printf("Stack 1: ");
displayStack(stack1, top1);
return 0;
}
14. Draw the stack structure in each case when the following operations are
performed on an empty stack.
(a) Add A, B, C, D, E, F
Stack after each operation:
Push A → [A]
Push B → [A, B]
Push C → [A, B, C]
Push D → [A, B, C, D]
Push E → [A, B, C, D, E]
Push F → [A, B, C, D, E, F]
Pop → [A, B, C, D, E]
Pop → [A, B, C, D]
(c) Add G
Push G → [A, B, C, D, G]
(d) Add H
Push H → [A, B, C, D, G, H]
(f) Add I
Push I → [A, I]
Definition Uses loops (for, while) for Calls itself to solve a problem.
repeated execution.
Memory Uses constant memory for Uses memory for each function call,
Usage execution. leading to a stack overflow risk.
Performance Generally more efficient, avoids Can be slower due to function call
overhead of multiple function overhead.
calls.
When to Use Preferred when the problem can Preferred when the problem is naturally
be easily solved using loops. recursive (e.g., tree traversals,
factorials).
The Tower of Hanoi is a classic problem in computer science that involves three rods
and a number of disks of different sizes. The objective is to move all disks from the
source rod to the target rod, following these rules:
● Move n-1 disks from the source rod to the auxiliary rod.
● Move the nth disk to the target rod.
● Move n-1 disks from the auxiliary rod to the target rod.
The minimum number of moves required to solve the Tower of Hanoi problem is 2^n -
1, where n is the number of disks.
Programming Exercises
1. Write a program to implement a stack using a linked list.
2. Write a program to convert the expression “a+b” into “ab+".
3. Write a program to convert the expression “a+b” into “+ab”.
4. Write a program to implement a stack that stores names of students in the class.
5. Write a program to input two stacks and compare their contents.
Answers
int pop() {
if (!top) return -1;
Node* temp = top;
int value = temp->data;
top = top->next;
free(temp);
return value;
}
int main() {
push(10); push(20);
printf("Popped: %d\n", pop());
return 0;
}
int main() {
char infix[] = "a+b", postfix[4];
postfix[0] = infix[0];
postfix[1] = infix[2];
postfix[2] = infix[1];
postfix[3] = '\0';
printf("Postfix: %s\n", postfix);
return 0;
}
int main() {
char infix[] = "a+b", prefix[4];
prefix[0] = infix[1];
prefix[1] = infix[0];
prefix[2] = infix[2];
prefix[3] = '\0';
printf("Prefix: %s\n", prefix);
return 0;
}
int main() {
push("Alice"); push("Bob");
printf("Top: %s\n", top->name);
}
int main() {
Node *s1 = NULL, *s2 = NULL;
printf("Stacks are %s\n", compare(s1, s2) ? "equal" : "not
equal");
return 0;
}
int main() {
push(10);
push(20); }
2. Pop Operation (Delete Element from Stack)
#include <stdio.h>
#define SIZE 5
int pop() {
if (top == -1) {
printf("Stack Underflow\n");
return -1;
}
return stack[top--];
}
int main() {
push(10); push(20);
printf("Popped: %d\n", pop());
return 0;
}
int main() {
push(10); push(20);
printf("Topmost element: %d\n", peep());
return 0;
}
void display() {
if (top == -1) {
printf("Stack is empty\n");
return;
}
for (int i = top; i >= 0; i--)
printf("%d ", stack[i]);
printf("\n");
}
int main() {
push(10); push(20); push(30);
printf("Stack: ");
display();
return 0;
}
int main() {
push(10);
push(20); }
2. Pop Operation (Delete Element from Stack Using Linked List)
#include <stdio.h>
#include <stdlib.h>
int pop() {
if (!top) {
printf("Stack Underflow\n");
return -1;
}
Node* temp = top;
int value = temp->data;
top = top->next;
free(temp);
return value;
}
int main() {
push(10); push(20);
printf("Popped: %d\n", pop());
return 0;
}
3. Peep Operation (Return Topmost Element Without Deleting)
#include <stdio.h>
#include <stdlib.h>
int peep() {
if (!top) {
printf("Stack is empty\n");
return -1;
}
return top->data;
}
int main() {
push(10); push(20);
printf("Topmost element: %d\n", peep());
}
void display() {
if (!top) {
printf("Stack is empty\n");
return;
}
Node* temp = top;
while (temp) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
push(10); push(20); push(30);
printf("Stack: ");
display();
return 0;
}