-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrailroad-queue.cpp
More file actions
194 lines (189 loc) · 3.03 KB
/
railroad-queue.cpp
File metadata and controls
194 lines (189 loc) · 3.03 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include<iostream>
using namespace std;
int len=0;
const int size=9;
int in[size]={5,8,1,7,4,2,9,6,3};
int out[size]={0,0,0,0,0,0,0,0,0};
class node
{
public:
int data;
node *next;
node(int val)
{
data=val;
next=NULL;
}
};
class Queue
{
private:
int length;
node *front;
node *rear;
public:
Queue()
{
length=0;
front=NULL;
rear=NULL;
}
void in_que(int val);
int de_que();
int peak();
int ft();
bool is_empty();
bool is_full();
};
bool Queue::is_full()
{
if(length==3)
{
return true;
}
return false;
}
int Queue::ft()
{
if(front==NULL)
{
return 0;
}
return front->data;
}
bool Queue::is_empty()
{
if(front==NULL)
{
return true;
}
return false;
}
int Queue::peak()
{
int x;
x=rear->data;
return x;
}
void Queue::in_que(int val)
{
node *temp;
if(front==NULL)
{
node *n=new node(val);
front=n;
rear=n;
length++;
}
else
{
node *n=new node(val);
temp=rear;
temp->next=n;
rear=n;
length++;
}
}
int Queue::de_que()
{
node *temp;
int x;
temp=front;
front=front->next;
temp->next=NULL;
x=temp->data;
delete temp;
return x;
}
Queue H1,H2,H3;
void arrange();
void output();
int main(void)
{
arrange();
return 0;
}
void output()
{
for(int i=0;i<9;i++)
{
cout<<out[i]<<" ";
}
}
void arrange()
{
int turn=1;
int i=8;
int p=0;
len=0;
while(len!=9)
{
if(H3.is_empty()==0)
{
cout<<"This can not be handle by this technique .";
break;
}
else if(turn==in[i])
{
out[p]=in[i];
p++;
i--;
len++;
turn++;
}
else if(turn==H1.ft())
{
out[p]=H1.de_que();
p++;
len++;
turn++;
}
else if(turn==H2.ft())
{
out[p]=H2.de_que();
p++;
len++;
turn++;
}
else if(H1.is_empty()==1)
{
H1.in_que(in[i]);
i--;
}
else if(H1.is_empty()==0 and H1.is_full()==0)
{
if(H1.peak()<in[i])
{
H1.in_que(in[i]);
i--;
}
else
{
H2.in_que(in[i]);
i--;
}
}
else if(H1.is_full()==1 and H2.is_empty()==1)
{
H2.in_que(in[i]);
i--;
}
else if(H2.is_empty()==0)
{
if(H2.peak()<in[i])
{
H2.in_que(in[i]);
i--;
}
else
{
H3.in_que(in[i]);
i--;
}
}
}
if(out[0]==1 and out[size-1]==9)
{
output();
}
}