-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnannakuPrematho.cpp
More file actions
117 lines (74 loc) · 1.33 KB
/
nannakuPrematho.cpp
File metadata and controls
117 lines (74 loc) · 1.33 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
/* 100 people standing in a circle in an order 1 to 100. No. 1 has a sword.
He kills the next person (i.e. No. 2) and gives the sword to the next (i.e. No. 3).
All people do the same until only 1 survives. Who will survive at the last?
*/
#include<iostream>
using namespace std;
struct node
{ int data;
node *next;
};
class List
{
public:
node *h,*t;
List() { h=NULL; t=NULL; }
void insert(int data)
{
node *n=new node();
n->data=data;
if(h==NULL)
{
h=n;t=n;
}
else
{
t->next=n;
t=n;
}
if(data==100)
{
t->next=h;
}
}
void killProcess()
{
kill(h);
}
void kill(node *root )
{
node *crnt=root;
while(1)
{
crnt->next=crnt->next->next;
crnt=crnt->next;
root=crnt;
if(root->next==root->next->next)
{
cout<<root->data<<" bachh gya sala "<<endl;
break;
}
}
//cout<<root->data;
} // ending of kill function
void print()
{
node *tmp;
tmp=h;
while(tmp!=NULL)
{
cout<<tmp->data<<" ";
tmp=tmp->next;
}
}
}; // ending of class
int main()
{
List a;
for(int i=1;i<=100;i++)
{
a.insert(i);
}
a.killProcess();
//a.print();
}