-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJosephus Problem.cpp
94 lines (92 loc) · 1.18 KB
/
Josephus Problem.cpp
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
#include<iostream>
using namespace std;
struct node
{
node *next;
int value;
};
class random
{
private:
node *head,*temp,*current,*del;
int key,value,size;
public:
random()
{
head=NULL;
temp=head;
}
void insert()
{
if(head==NULL)
{
head=new node;
cin>>head->value;
head->next=head;
size++;return;
}
temp=head;
cout<<"Enter the value to put after the node";
cin>>key;
do
{
if(temp->value==key)
{
current=new node;
cin>>current->value;
current->next=temp->next;
temp->next=current;
size++;
return;
}
temp=temp->next;
}while(temp!=head);
}
void print()
{temp=head;
cout<<"The List is ";
do
{
cout<<temp->value;
temp=temp->next;
}while(temp!=head);
}
void remove()
{
for(int i=0;i<4;i++)
{
if(head!=NULL)
{temp=head;value=3;
for(int i=0;i<value;i++)
{temp=temp->next;}
while(temp->next!=head)
{
del=temp->next;
temp->next=del->next;
delete del;
temp=temp->next;
}
}
}
}
void pprint()
{
temp=head;
cout<<"The List is ";
do
{
cout<<temp->value;
temp=temp->next;
}while(temp!=head);
}
};
int main()
{
random rr;
for(int i=0;i<5;i++)
{
rr.insert();
}
rr.remove();
rr.pprint();
}