forked from mohitarora3/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprogram5.cpp
More file actions
147 lines (134 loc) · 2.95 KB
/
program5.cpp
File metadata and controls
147 lines (134 loc) · 2.95 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include<iostream>
#include<conio.h>
using namespace std;
class node
{
/*
objective: Create a class for implementing node
input parameters: none
output value: none
description: class definition
approach: Class defines data member and member function of the node class
*/
int info;
node *next;
friend class circular;
public:
node()
{
next=0;
}
node(int ele,node *ptr=0)
{
info=ele;
next=ptr;
}
};
class circular
{
/*
objective: Create a class for implementing circular linklist
input parameters: none
output value: none
description: class definition
approach: Class defines data member and member function of the circular class
*/
node *head;
public:
circular()
{
head=0;
}
void add(int);
void display();
int eliminate(int);
};
int circular::eliminate(int m)
{
/*
objective:to select a leader among n persons sitting in a circle by eliminating mth element again and again
input parameters:
m-(integer value)-it represents person number to be eliminated
return value:-(integer value)-person number who is elected as a leader
approach:while there is not a single element in the circular linklist
1.select previous node of mth node
2.insert mth next into previous next
3.delete mth node
*/
if(head->next==head)
{
return head->info;
}
node *temp,*prev=head;
temp=head->next;
while(temp->next!=temp)
{
for(int i=0;i<m-1;i++)
{
prev=temp;
temp=temp->next;
}
prev->next=temp->next;
delete temp;
temp=prev->next;
}
return temp->info;
}
void circular::add(int ele)
{
/*
objective:to insert element at the end of the circular linklist
input parameters:
ele-(integer value)-element to be inserted into circular linklist
return value:none
*/
if(head==0)
{
head=new node(ele);
head->next=head;
}
else
{
head->next=new node(ele,head->next);
head=head->next;
}
}
void circular::display()
{
/*
objective:to display elements of circular linklist
input parameters:none
return value:none
approach:-starting from head display every element of circular linklist
*/
node *temp=head;
while(temp&&temp->next!=head)
{
cout<<temp->info;
temp=temp->next;
}
cout<<temp->info;
}
int main()
{
/*
objective:to select a leader among n persons sitting in a circle by eliminating mth element again and again
input parameters:
n-(integer value)-number of persons among whom leader is to be chosen
m-(integer value)-it represents person number to be eliminated
return value:none
approach:by invoking function eliminate
*/
circular c;
int n,m,ele;
cout<<"\n\tENTER SIZE(number of persons):";
cin>>n;
for(int i=1;i<=n;i++)
c.add(i);
cout<<"\n\tELEMENTS OF CIRCULAR LINKLIST:";
c.display();
cout<<"\n\tENTER M:";
cin>>m;
cout<<"\n\tPOSITION OF LEADER IS:"<<c.eliminate(m)<<"(i.e the last person remaining)";
getch();
}