-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack_Using_Array_Using_Struct.c
More file actions
126 lines (116 loc) · 2.98 KB
/
Stack_Using_Array_Using_Struct.c
File metadata and controls
126 lines (116 loc) · 2.98 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
115
116
117
118
119
120
121
122
123
124
125
126
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct Stack
{
int top;
unsigned capacity;
int* array;
};
struct Stack* createStack(unsigned capacity)
{
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack -> capacity = capacity;
stack -> top = -1;
stack -> array = (int*)malloc(stack -> capacity * sizeof(int));
return stack;
}
bool isEmpty(struct Stack *ps)
{
if(ps -> top == -1)
return true;
else
return false;
}
bool isFull(struct Stack *ps)
{
return (ps -> top == ps -> capacity-1)?true:false;
}
void push(struct Stack* stack, int n)
{
stack->array[++stack->top] = n;
printf("%s", "Push Successful!");
}
void pop(struct Stack* stack)
{
stack->array[stack->top] = 0;
stack->top=stack->top-1;
printf("%s", "Pop Successful!");
}
void show(struct Stack* stack)
{
printf("The STACK:\n| ");
for(int i=0; i<=stack ->top; i++)
{
printf("%d | ", stack->array[i]);
}
}
int main(void)
{
struct Stack* stack = createStack(100);
printf("A stack of Size 100 has been predefined!");
char c=' ';
printf("\nDo you want to resize it? (Press Y for YES or N for NO)\n-->");
do{
scanf("%c", &c);
if(c == 'Y')
{
printf("Enter the new size for allocation: --> ");
int size;
scanf("%i", &size);
stack = createStack(size);
printf("The stack has been reinitialized to a size of %d.\n", size);
break;
}
else if(c == 'N')
{
break;
}
else {
printf(" Re-Enter your choice!\n-->");
}
scanf("%c", &c);
}
while(c!='N');
printf("The options for the STACK OPERATION:\n1. PUSH\n2. POP\n3. SHOW\n4. EXIT");
int n = 0;
while(sizeof(n)==sizeof(int))
{
printf("\nEnter your choice:\n-->");
scanf("%d",&n);
switch(n)
{
case 1:
if (isFull(stack))
printf("%s", "OverFlow Condition has occurred!\n");
else {
printf("Enter the number to push:\n-->");
int num;
scanf("%d", &num);
push(stack, num);
}
break;
case 2:
if (isEmpty(stack))
printf("%s", "UnderFlow Condition has occurred!\n");
else {
pop(stack);
}
break;
case 3:
if (isFull(stack))
printf("The stack is Full!\n");
else if (isEmpty(stack))
printf("The stack is EMPTY!\n");
else
show(stack);
break;
case 4:
printf("\n[+] PROCESS TERMINATED!");
exit(0);
default:
printf("[-] WRONG INPUT!\n");
break;
}
}
}