-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram2.c
More file actions
101 lines (82 loc) · 2.17 KB
/
program2.c
File metadata and controls
101 lines (82 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
// Structure to represent the stack
struct Stack {
int arr[MAX];
int top;
};
// Function to initialize the stack
void initialize(struct Stack *s) {
s->top = -1;
}
// Function to check if the stack is empty
int isEmpty(struct Stack *s) {
return s->top == -1;
}
// Function to check if the stack is full
int isFull(struct Stack *s) {
return s->top == MAX - 1;
}
// Function to push an element onto the stack
void push(struct Stack *s, int value) {
if (isFull(s)) {
printf("Stack Overflow! Cannot push element %d\n", value);
} else {
s->arr[++s->top] = value;
printf("Element %d pushed onto the stack\n", value);
}
}
// Function to pop an element from the stack
void pop(struct Stack *s) {
if (isEmpty(s)) {
printf("Stack Underflow! Cannot pop from an empty stack\n");
} else {
printf("Element %d popped from the stack\n", s->arr[s->top--]);
}
}
// Function to display the status of the stack
void display(struct Stack *s) {
if (isEmpty(s)) {
printf("Stack is empty\n");
} else {
printf("Stack status: ");
for (int i = 0; i <= s->top; i++) {
printf("%d ", s->arr[i]);
}
printf("\n");
}
}
int main() {
struct Stack stack;
initialize(&stack);
int choice, element;
do {
printf("\nMenu:\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display Stack\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter element to push: ");
scanf("%d", &element);
push(&stack, element);
break;
case 2:
pop(&stack);
break;
case 3:
display(&stack);
break;
case 4:
printf("Exiting program\n");
exit(0);
default:
printf("Invalid choice! Please enter a valid option.\n");
}
} while (1); // Infinite loop for the menu
return 0;
}