1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+
4
+ template <class Item >
5
+ class LinearList {
6
+ private:
7
+ int MaxSize;
8
+ Item *element; // 1D dynamic array
9
+ int len;
10
+ public:
11
+ /* Default constructor.
12
+ * Should create an empty list that not contain any elements*/
13
+ LinearList ()
14
+ {
15
+ element=nullptr ;
16
+ len=0 ;
17
+ };
18
+
19
+ /* This constructor should create a list containing MaxSize elements. You can intialize the elements with any values.*/
20
+ LinearList (const int & MaxListSize)
21
+ {
22
+ MaxSize=MaxListSize;
23
+ element = new Item[MaxSize];
24
+ len=0 ;
25
+ /* for(int i=0;i<MaxSize;i++)
26
+ {
27
+ cout << element[i] << endl;
28
+ }*/
29
+
30
+ };
31
+
32
+ /* Destructor.
33
+ * Must free all the memory contained by the list */
34
+ ~LinearList (){};
35
+
36
+ /* Indexing operator.
37
+ * It should behave the same way array indexing does. i.e,
38
+ * LinearList L;
39
+ * L[0] should refer to the first element;
40
+ * L[1] to the second element and so on.
41
+ * */
42
+ Item& operator [](const int & i)
43
+ {
44
+ return element[i];
45
+ }; // return i'th element of list
46
+
47
+ /* Returns true if the list is empty, false otherwise.
48
+ * */
49
+ bool isEmpty ()
50
+ {
51
+ if (len==0 )
52
+ {
53
+ return true ;
54
+ }
55
+ else
56
+ {
57
+ return false ;
58
+ }
59
+ };
60
+
61
+ /* Returns the actual length (number of elements) in the list.
62
+ * */
63
+ int length ()
64
+ {
65
+ return len;
66
+ };
67
+
68
+ /* Returns the maximum size of the list.
69
+ * */
70
+ int maxSize ()
71
+ {
72
+ return maxSize;
73
+ };
74
+
75
+ /* Returns the k-th element of the list.
76
+ * */
77
+ Item returnListElement (const int k)
78
+ {
79
+ return element[k-1 ];
80
+ };
81
+ /* Set x to the k-th element and
82
+ * return true if k-th element is present otherwise return false.
83
+ * */
84
+ bool find (const int k, Item& x)
85
+ {
86
+ if (x==element[k-1 ])
87
+ {
88
+ return true ;
89
+ }
90
+ else
91
+ {
92
+
93
+ return false ;
94
+ }
95
+ };
96
+
97
+ /* Search for x and
98
+ * return the position if found, else return 0.
99
+ * */
100
+ int search (Item& x)
101
+ {
102
+ for (int i=0 ;i<len;i++)
103
+ {
104
+ if (element[i]==x)
105
+ {
106
+ return i+1 ;
107
+ }
108
+ }
109
+ return 0 ;
110
+
111
+ };
112
+
113
+ /* Set x to the k-th element and delete that k-th element.
114
+ * */
115
+ void deleteElement (const int k, Item& x)
116
+ {
117
+ x=element[k-1 ];
118
+ for (int i=k;i<len;i++)
119
+ {
120
+ element[i-1 ]=element[i];
121
+ }
122
+ len=len-1 ;
123
+ };
124
+
125
+ /* Insert x after k-th element.
126
+ * */
127
+ void insert (const int k, Item& x)
128
+ {
129
+ for (int i=len;i>k;i--)
130
+ {
131
+ element[i]=element[i-1 ];
132
+ }
133
+ element[k]=x;
134
+ len=len+1 ;
135
+ };
136
+
137
+
138
+ };
139
+
140
+ int main ()
141
+ {
142
+ LinearList<int > a (250 );
143
+ int k,n,i,j,temp;
144
+ cout << " Enter number of children: " ;
145
+ cin >> n;
146
+ cout << " Enter elimination rule: " ;
147
+ cin >> i;
148
+ for (j=0 ;j<n;j++)
149
+ {
150
+ temp=j+1 ;
151
+ a.insert (j,temp);
152
+ }
153
+ for (j=0 ;j<n;j++)
154
+ {
155
+ cout << a[j] << " " ;
156
+ }
157
+ cout << endl;
158
+ j=1 ;
159
+ k=0 ;
160
+ while (1 )
161
+ {
162
+ if (a.length ()==1 )
163
+ {
164
+ break ;
165
+ }
166
+ if (j%i!=0 )
167
+ {
168
+ a.deleteElement (1 ,temp);
169
+ a.insert (a.length (),temp);
170
+ j++;
171
+ }
172
+ else
173
+ {
174
+ /* cout << j << " " << i << endl;
175
+ for(k=0;k<a.length();k++)
176
+ {
177
+ cout << a[k] << " ";
178
+ }
179
+ cout << endl;*/
180
+ a.deleteElement (1 ,temp);
181
+ cout << " person at position " << temp << " is removed" << endl;
182
+ j++;
183
+ }
184
+
185
+ }
186
+ cout << " Hence person at position " ;
187
+ for (j=0 ;j<a.length ();j++)
188
+ {
189
+ cout << a[j] << " " ;
190
+ }
191
+ cout << " survives." << endl;
192
+
193
+ }
0 commit comments