-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.c
132 lines (119 loc) · 2.99 KB
/
Stack.c
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <stdio.h>
#include <stdlib.h>
struct stack {
int size;
int top;
int *arr;
};
int create(struct stack *s) {
s = (struct stack*)malloc(sizeof(struct stack));
if (s == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
printf("Enter Size of Stack: ");
scanf("%d", &s->size);
s->arr = (int*)malloc(s->size * sizeof(int));
if (s->arr == NULL) {
printf("Memory allocation for stack array failed.\n");
free(s);
return 1;
}
s->top = -1;
}
int isFull(struct stack *s1) {
if (s1->top == s1->size - 1) {
return 1;
}
return 0;
}
int isEmpty(struct stack *s1) {
if (s1->top == -1) {
return 1;
}
return 0;
}
int push(struct stack *s, int ele) {
if (isFull(s)) {
printf("%d cannot be added, Stack Overflow\n");
} else {
s->top++;
s->arr[s->top] = ele;
}
return 0;
}
int pop(struct stack *s) {
return s->arr[s->top--];
}
int peek(struct stack *s) {
int p;
p = s->arr[s->top];
return p;
}
void display(struct stack *s) {
for (int i = 0; i <= s->top; i++) {
printf("%d ", s->arr[i]);
}
printf("\n");
}
int main() {
struct stack *s;
if(create(s)){
return 0;
}
int choice;
do {
printf("\nStack Menu:\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Peek\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
if (isFull(s)) {
printf("Stack is full. Cannot push.\n");
} else {
int element;
printf("Enter the element to push: ");
scanf("%d", &element);
push(s, element);
}
break;
case 2:
if (isEmpty(s)) {
printf("Stack is empty. Cannot pop.\n");
} else {
int data = pop(s);
printf("Element popped %d.\n",data);
}
break;
case 3:
if (isEmpty(s)) {
printf("Stack is empty. Peek not possible.\n");
} else {
int top = peek(s);
printf("The peek element is %d\n", top);
}
break;
case 4:
if (isEmpty(s)) {
printf("Stack is empty. Nothing to display.\n");
} else {
printf("Stack contents: ");
display(s);
}
break;
case 5:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 5);
free(s->arr);
free(s);
return 0;
}