forked from Abhinav-22/helloworld
-
Notifications
You must be signed in to change notification settings - Fork 0
/
han.c
134 lines (120 loc) · 2.96 KB
/
han.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
133
134
#include<stdio.h>
#include<math.h>
int item;
void push(int a[10],int *top)
{
*top++;
a[*top]=item;
}
void pop(int a[10],int *top)
{
item=a[*top];
*top--;
if(*top==0)
a[*top]==0;
}
void display(int a[10],int *top)
{
int i;
for(int i=*top;i>=1;i--)
{
printf("%d",a[i]);
}
}
void main()
{
int lim,n,i,atop=0,btop=0,ctop=0,a[10],b[10],c[10];
a[0]=0;
b[0]=0;
c[0]=0;
printf("enter the number of elements");
scanf("%d",&n);
for(i=n;i>=1;i--)
{
atop++;
a[atop]=i;
}
lim=pow(2,n);
for(i=1;i<=lim;i++)
{
printf("\n\n");
printf("\nStack number 1:\t");
if(a[atop]!=0)
display(a,&atop);
printf("\nStack number 2:\t");
if(b[btop]!=0)
display(b,&btop);
printf("\nStack number 3:\t");
if(c[ctop]!=0)
display(c,&ctop);
if(i%3==1)
{
if(a[atop]==0 && c[ctop]!=0)
{
pop(c,&ctop);
push(a,&atop);
}
else if (a[atop]!=0 && c[ctop]==0)
{
pop(a,&atop);
push(c,&ctop);
}
else if(a[atop]<c[ctop])
{
pop(a,&atop);
push(c,&ctop);
}
else if(a[atop]>c[ctop])
{
pop(c,&ctop);
push(a,&atop);
}
}
else if(i%3==2)
{
if(a[atop]==0 && b[btop]!=0)
{
pop(b,&btop);
push(a,&atop);
}
else if (a[atop]!=0 && b[btop]==0)
{
pop(a,&atop);
push(b,&btop);
}
else if(a[atop]<b[btop])
{
pop(a,&atop);
push(b,&btop);
}
else if(a[atop]>b[btop])
{
pop(b,&btop);
push(a,&atop);
}
}
else if(i%3==0)
{
if(b[btop]==0 && c[ctop]!=0)
{
pop(c,&ctop);
push(b,&btop);
}
else if (b[btop]!=0 && c[ctop]==0)
{
pop(b,&btop);
push(c,&ctop);
}
else if(b[btop]<c[ctop])
{
pop(b,&btop);
push(c,&ctop);
}
else if(b[btop]>c[ctop])
{
pop(c,&ctop);
push(b,&btop);
}
}
}
}