-
Notifications
You must be signed in to change notification settings - Fork 127
/
2_in_onestack (1).c
111 lines (104 loc) · 1.78 KB
/
2_in_onestack (1).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
// WAP to Implement two stacks in one array
#include <stdio.h>
#define SIZE 20
int array[SIZE];
int top1 = -1;
int top2 = SIZE;
void push1(int data)
{
if (top1 < top2 - 1)
{
top1++;
array[top1] = data;
}
else
{
printf("Stack is full\n");
}
}
void push2(int data)
{
if (top1 < top2 - 1)
{
top2--;
array[top2] = data;
}
else
{
printf("Stack is full..\n");
}
}
void pop1()
{
if (top1 >= 0)
{
int popped_element = array[top1];
top1--;
printf("%d is being popped from Stack 1\n", popped_element);
}
else
{
printf("Stack is Empty \n");
}
}
void pop2()
{
if (top2 < SIZE)
{
int popped_element = array[top2];
top2--;
printf("%d is being popped from Stack 1\n", popped_element);
}
else
{
printf("Stack is Empty!\n");
}
}
void display_stack1()
{
int i;
for (i = top1; i >= 0; --i)
{
printf("%d ", array[i]);
}
printf("\n");
}
void display_stack2()
{
int i;
for (i = top2; i < SIZE; ++i)
{
printf("%d ", array[i]);
}
printf("\n");
}
int main()
{
int ar[SIZE];
int i;
int num_of_ele;
printf("We can push a total of 20 values\n");
for (i = 1; i <= 10; ++i)
{
push1(i);
printf("Value Pushed in Stack 1 is %d\n", i);
}
for (i = 11; i <= 20; ++i)
{
push2(i);
printf("Value Pushed in Stack 2 is %d\n", i);
}
display_stack1();
display_stack2();
//printf("Pushing Value in Stack 1 is %d\n", 11);
printf("Trying to push value into stack 1: ");
push1(11);
num_of_ele = top1 + 1;
while (num_of_ele)
{
pop1();
--num_of_ele;
}
pop1();
return 0;
}