-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathdoublestack.c
107 lines (87 loc) · 1.91 KB
/
doublestack.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
#include<stdio.h>
#define MAX 5
//Declaration of Double Stack
typedef struct
{
int top1;
int top2;
int ele[MAX];
}DStack;
//Initialization of Double Stack
void init( DStack *s )
{
s->top1 = -1;
s->top2 = MAX;
}
//Push Operation on Stack1
void pushA( DStack *s, int item )
{
if( s->top2 == s->top1 + 1 )
{
printf("\nStack Overflow Stack1");
return;
}
s->top1++;
s->ele[s->top1] = item;
printf("\nInserted item in Stack1 : %d",item);
}
//Push Operation on Stack2
void pushB( DStack *s, int item )
{
if( s->top2 == s->top1 + 1 )
{
printf("\nStack Overflow Stack2");
return;
}
s->top2--;
s->ele[s->top2] = item;
printf("\nInserted item in Stack2 : %d",item);
}
//Pop Operation on Stack1
int popA( DStack *s, int *item )
{
if( s->top1 == -1 )
{
printf("\nStack Underflow Stack1");
return -1;
}
*item = s->ele[s->top1--];
return 0;
}
//Pop Operation on Stack2
int popB( DStack *s, int *item )
{
if( s->top2 == MAX )
{
printf("\nStack Underflow Stack2");
return -1;
}
*item = s->ele[s->top2++];
return 0;
}
int main()
{
int item = 0;
DStack s;
init(&s);
pushA( &s, 10);
pushA( &s, 20);
pushA( &s, 30);
pushB( &s, 40);
pushB( &s, 50);
pushB( &s, 60);
if( popA(&s, &item) == 0 )
printf("\nDeleted item From Stack1 : %d",item);
if( popA(&s, &item) == 0 )
printf("\nDeleted item From Stack1 : %d",item);
if( popA(&s, &item) == 0 )
printf("\nDeleted item From Stack1 : %d",item);
if( popB(&s, &item) == 0 )
printf("\nDeleted item From Stack2 : %d",item);
if( popB(&s, &item) == 0 )
printf("\nDeleted item From Stack2 : %d",item);
if( popB(&s, &item) == 0 )
printf("\nDeleted item From Stack2 : %d",item);
printf("\n");
return 0;
}