-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircularLinkedList.cpp
More file actions
81 lines (67 loc) · 1.14 KB
/
circularLinkedList.cpp
File metadata and controls
81 lines (67 loc) · 1.14 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
#include<iostream>
#include<stdlib.h>
using namespace std;
struct circularList{
int data;
circularList* link;
};
circularList *start, *ptr, *ptr1;
int number=0;
void addf(){
ptr=(struct circularList *)malloc(sizeof(struct circularList));
cout<<endl<<"Enter Data: ";
cin>>ptr->data;
start=ptr;
number++;
}
void addn(){
ptr1=(struct circularList *)malloc(sizeof(struct circularList));
ptr->link=ptr1;
cout<<endl<<"Enter Data: ";
cin>>ptr1->data;
ptr=ptr1;
number++;
}
void display(){
int count=0;
ptr=start;
while(count<number){
cout<<ptr->data<<" ";
ptr=ptr->link;
count++;
}
}
void josephus(int pos){
ptr=start;
if (pos>number+1) cout<<endl<<"Invalid Entry! ";
for(int i=0; i<pos-1; i++){
ptr=ptr->link;
}
circularList *newp;
newp=ptr->link;
ptr->link=newp->link;
number--;
start=newp->link;
}
int main(){
addf();
char ch='y';
while(ch=='y'||ch=='Y'){
addn();
cout<<endl<<"Enter More(y/n)?: ";
cin>>ch;
}
ptr1->link=start;
display();
cout<<endl;
cout<<endl<<"Enter position for josephus: ";
int n;
cin>>n;
n=n-1;
while(number>=n-1){
josephus(n);
display();
cout<<endl;
}
return 0;
}