Implement stack using array with following operations: push, pop, print, peek, peep, change, exit.
๐งพ Step-by-Step Explanation
๐น Stack Initialization
int stack[MAX];
int top = -1;
stack[MAX]
: Array to store stack elements (MAX = 100)top
: Index of top element, initialized to -1 (empty stack)
๐น Push Operation
void push(int value) {
if (top >= MAX - 1) {
printf("Stack Overflow! Cannot push.\n");
} else {
top++;
stack[top] = value;
printf("%d pushed to stack.\n", value);
}
}
- Checks for stack overflow (when stack is full)
- Increments
top
and adds new element - Provides feedback about the operation
๐น Pop Operation
int pop() {
if (top == -1) {
printf("Stack Underflow! Cannot pop.\n");
return -1;
} else {
return stack[top--];
}
}
- Checks for stack underflow (when stack is empty)
- Returns top element and decrements
top
- Returns -1 if stack is empty
๐น Peek Operation
void peek() {
if (top == -1) {
printf("Stack is empty!\n");
} else {
printf("Top element is: %d\n", stack[top]);
}
}
- Displays the top element without removing it
- Checks if stack is empty first
๐น Peep Operation
void peep(int position) {
int index = top - position + 1;
if (index < 0 || index > top) {
printf("Invalid position! Cannot peep.\n");
} else {
printf("Element at position %d from top is: %d\n", position, stack[index]);
}
}
- Views element at specific position from top
- Calculates index using
top - position + 1
- Validates position is within bounds
๐น Change Operation
void change(int position, int value) {
int index = top - position + 1;
if (index < 0 || index > top) {
printf("Invalid position! Cannot change.\n");
} else {
stack[index] = value;
printf("Element at position %d changed to %d.\n", position, value);
}
}
- Modifies element at specific position from top
- Uses same position calculation as peep operation
- Provides feedback about the change
๐น Print Operation
void print() {
if (top == -1) {
printf("Stack is empty!\n");
} else {
printf("Stack elements (top to bottom):\n");
for (int i = top; i >= 0; i--) {
printf("%d\n", stack[i]);
}
}
}
- Displays all stack elements from top to bottom
- Checks if stack is empty first
- Uses reverse loop to show top element first
๐งช Sample Output
***** STACK OPERATIONS MENU *****
1. Push
2. Pop
3. Peek
4. Peep
5. Change
6. Print
7. Exit
Enter your choice: 1
Enter value to push: 10
10 pushed to stack.
Enter your choice: 1
Enter value to push: 20
20 pushed to stack.
Enter your choice: 3
Top element is: 20
Enter your choice: 4
Enter position from top to peep: 2
Element at position 2 from top is: 10
Enter your choice: 5
Enter position from top to change: 2
Enter new value: 15
Element at position 2 changed to 15.
Enter your choice: 6
Stack elements (top to bottom):
20
15
Enter your choice: 2
Popped value: 20
Enter your choice: 7
Exiting program...
๐ Key Concepts Covered
Stack
LIFO (Last In First Out) data structure
Array Implementation
Using static memory allocation with fixed size
Core Operations
Push, Pop, Peek, Peep, Change, Print
Position Calculation
top - position + 1
for peep/change operationsError Handling
Stack overflow and underflow conditions checked
No comments:
Post a Comment