-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq1.cpp
More file actions
88 lines (87 loc) · 1.25 KB
/
q1.cpp
File metadata and controls
88 lines (87 loc) · 1.25 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
#include <iostream>
using namespace std;
template <typename T>
struct node {
T data;
node* next;
};
template <typename T>
class queue {
node<T>* top;
node<T>* tail;
public:
queue() {
top = tail = NULL;
}
void push(T x) {
node<T>* t = new node<T>;
t->data = x;
t->next = NULL;
if (tail == NULL) {
tail = t;
top = t;
}
else {
tail->next = t;
tail = t;
}
}
T pop() {
if (!isempty()) {
node<T>* y = top;
top = top->next;
return y->data;
}
else {
cout << "empty!";
}
}
bool isempty() {
if (top == NULL) {
return 1;
}
return 0;
}
void print() {
node<T>* p = top;
while (p != NULL) {
cout << p->data << " "; p = p->next;
}
}
};
int main() {
queue<int> q1;
int x = 0, size = 0;
cout << "enter size >>: ";
cin >> size;
cout << "enter k >>: ";
cin >> x;
for (int i = 1; i <= size; i++)
{
q1.push(i);
}
bool f = 0;
int i = 0, k = 0;
while (k != size - 1) {
if (i == x - 1) {// to be killed
q1.pop();
k++;
f = 1;
}
else { // the survivers pushed back again from start
q1.push(q1.pop());
}
if (f == 1) {
i = 0; f = 0;
}
else { i++; }
/*while (!q1.isempty()) {
queue<int> tt;
tt.push(q1.pop());
}*/
q1.print();
cout << endl;
cout << endl;
}
q1.print();
}