IT05 - Session 4
IT05 - Session 4
Learning Objectives:
1. Understand the fundamental concept of a stack and its LIFO (Last In,
First Out) principle.
5. Compare and contrast stacks with other data structures like queues.
Lesson Outline:
Key Operations:
Interactive Example:
o Example problems:
Group Activity:
o Divide the class into small groups to solve one complex problem
together (e.g., designing a custom stack that also tracks the
minimum element). Encourage collaboration and discussion.
Stack Variations:
Circular Stack
Priority Stack
Implementation Challenge:
Conceptual Comparison:
o Discuss the FIFO (First In, First Out) principle of queues and how
it contrasts with stacks.
Application Scenarios:
Interactive Coding:
Review Session:
Q&A Session:
10. Homework/Assignment:
Coding Project:
Assessment:
In-Class Participation:
Project Submission:
Follow-up:
Next Lesson:
o Plan future lessons on linked lists, trees, and graphs, tying back
to the fundamental concepts learned with stacks.
char pop() {
if (top == -1) {
return -1;
} else {
return stack[top--];
}
}
int main() {
char expr[MAX] = "(A+B)*(C+D)";
if (areParenthesesBalanced(expr)) {
printf("Parentheses are balanced\n");
} else {
printf("Parentheses are not balanced\n");
}
return 0;
}
#include <stdio.h>
#include <string.h>
char pop() {
if (top == -1) {
return -1;
} else {
return stack[top--];
}
}
int main() {
char str[MAX] = "Hello, World!";
reverseString(str);
printf("Reversed string: %s\n", str);
return 0;
}
Implementing a Stack-Based Browser History Navigation:
#include <stdio.h>
#include <string.h>
char* popBack() {
if (topBack == -1) {
return NULL;
} else {
return backStack[topBack--];
}
}
char* popForward() {
if (topForward == -1) {
return NULL;
} else {
return forwardStack[topForward--];
}
}
void navigateBack() {
if (topBack != -1) {
pushForward(backStack[topBack]);
printf("Navigating back to: %s\n", popBack());
} else {
printf("No more history to go back to.\n");
}
}
void navigateForward() {
if (topForward != -1) {
pushBack(forwardStack[topForward]);
printf("Navigating forward to: %s\n", popForward());
} else {
printf("No more history to go forward to.\n");
}
}
int main() {
pushBack("page1.com");
pushBack("page2.com");
pushBack("page3.com");
return 0;
}
Circular Stack:
o Implement a circular stack where the top wraps around to the start
when the end is reached.
#include <stdio.h>
#define MAX 5
int stack[MAX];
int top = -1;
int pop() {
if (top == -1)