-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2_stacks_using_array_space_optimization.c
More file actions
135 lines (123 loc) · 2.78 KB
/
2_stacks_using_array_space_optimization.c
File metadata and controls
135 lines (123 loc) · 2.78 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
127
128
129
130
131
132
133
134
135
#include <stdio.h>
#include <stdlib.h>
void push(int *arr, int stack_choice, int input, int *p)
{
if(stack_choice == 1)
{
(*p)++;
arr[*p] = input;
}
else if(stack_choice == 2)
{
(*p)--;
arr[*p] = input;
}
}
void pop(int stack_choice, int *arr, int *p)
{
if(stack_choice==1)
{
if(*p == -1)
{
printf("Underflow!");
return;
}
printf("[-] Data popped from STACK1 : %d.\n", arr[*p]);
arr[*p] = 0;
(*p)--;
}
else if(stack_choice==2)
{
if(*p == 49)
{
printf("Underflow!");
return;
}
printf("[-] Data popped from STACK2 : %d.\n", arr[*p]);
arr[*p] = 0;
(*p)++;
}
}
void show(int stack_choice, int *arr, int p)
{
if(stack_choice==1)
{
if(p == -1)
{
printf("Stack Empty!");
return;
}
else if(p == 49)
{
printf("Stack Full!");
}
printf("[+] STACK 1:\n[+] ");
for(int i = p; i >= 0; i--)
{
printf("%d ", arr[i]);
}
printf("\n");
}
else if(stack_choice == 2)
{
if(p == 0)
{
printf("Stack Full!");
return;
}
else if(p == 50)
{
printf("Stack Empty!");
}
printf("[+] STACK 2:\n[+] ");
for(int i = p; i <= 49; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
}
int main(void)
{
int *arr;
arr = (int*)calloc(50, sizeof(int));
printf("[+] 2 stacks can hold a total of 50 elements.\n");
printf("[+] Input Format:\n[+]\t\tStack_Number Input_Element\n");
char flag[2] = "";
int stack1 = -1;
int stack2 = 50;
int counter = 0;
printf("[+] INPUT:\n");
do {
int stack_choice;
int input;
scanf("%d %d", &stack_choice, &input);
if(stack_choice == 1) {
push(arr, stack_choice, input, &stack1);
counter++;
}
else if(stack_choice == 2) {
push(arr, stack_choice, input, &stack2);
counter++;
}
else
printf("\n[-] Sorry operation unsuccessful!\n");
if(counter >= 50)
break;
printf("\nWanna stop? type 'Y' else type 'N' ---->");
scanf("%s", flag);
}
while(flag[0] != 'Y');
show(1, arr, stack1);
show(2, arr, stack2);
printf("Pointers' current location: %d %d.\n", stack1, stack2);
pop(1, arr, &stack1);
pop(2, arr, &stack2);
pop(2, arr, &stack2);
show(1, arr, stack1);
show(2, arr, stack2);
printf("Pointers' current location: %d %d.\n", stack1, stack2);
arr = NULL;
free(arr);
return 0;
}