Repository navigation
Expand file tree
/
Copy pathstack.c
More file actions
115 lines (102 loc) · 2.35 KB
/
Copy pathstack.c
File metadata and controls
115 lines (102 loc) · 2.35 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
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <stdio.h>
#define MAX_SIZE 100
int top = -1;
int stack[MAX_SIZE];
void push(int item) {
if (top == MAX_SIZE-1) {
printf("Stack overflow\n");
return;
}
top++;
stack[top] = item;
printf("Element successfully added to stack\n");
}
int pop() {
if (top == -1) {
printf("Stack underflow\n");
return 0;
}
int item = stack[top];
top--;
printf("Element successfully removed from the stack is %d\n", item);
return item;
}
void peek() {
if (top == -1) {
printf("Stack is empty\n");
return;
}
printf("The element at the top of the stack is %d\n", stack[top]);
}
void isFull(int size) {
if (top == size - 1) {
printf("The stack is full\n");
} else {
printf("Stack is not full\n");
}
}
void isEmpty() {
if (top == -1) {
printf("The stack is empty\n");
} else {
printf("Stack is not empty\n");
}
}
int main() {
int size;
printf("Enter the maximum size of the stack: ");
scanf("%d", &size);
if(size > MAX_SIZE) {
printf("Maximum size cannot be greater than %d\n", MAX_SIZE);
return 1;
}
while (1) {
printf("Menu:\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Peek\n");
printf("4. Check if stack is full\n");
printf("5. Check if stack is empty\n");
printf("6. Exit\n");
printf("Enter your choice: ");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1: {
int item;
printf("Enter the item to push: ");
scanf("%d", &item);
push(item);
break;
}
case 2: {
pop();
break;
}
case 3: {
peek();
break;
}
case 4: {
isFull(size);
break;
}
case 5: {
isEmpty();
break;
}
case 6: {
return 0;
}
default: {
printf("Invalid choice\n");
break;
}
}
if(choice == 'x' || choice == 'X'){
printf("Error.......");
break;
}
}
return 0;
}