forked from mohit0003/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram4.cpp
More file actions
161 lines (151 loc) · 2.97 KB
/
program4.cpp
File metadata and controls
161 lines (151 loc) · 2.97 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#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=NULL;
}
node(int ele,node*ptr=NULL)
{
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=NULL;
}
void add(int);
void display();
void split();
};
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==NULL)
{
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;
if(temp){
while(temp->next!=head)
{
cout<<temp->info<<" ";
temp=temp->next;
}
cout<<temp->info;
}
}
void circular::split()
{
/*
objective:to split a circular linklist into two halves
input parameters:none
return value:none
approach:-using floyd cycle algorithm
using two pointers
fast_ptr-it moves one step fast as compared to slow_ptr
slow_ptr-it moves one step slow as compared to fast_ptr
*/
if(head)
{
circular c1,c2;
node *fast_ptr=head,*slow_ptr=head;
node *head1,*head2;
cout<<"\n\tAFTER SPLITTING:";
if(head->next==head)
{
c1.head=head;
c2.head=NULL;
}
else
{
while(fast_ptr->next!=head&&fast_ptr->next->next!=head)
{
fast_ptr=fast_ptr->next->next;
slow_ptr=slow_ptr->next;
}
if(fast_ptr->next->next==head)
fast_ptr=fast_ptr->next;
head1=head;
head2=slow_ptr->next;
slow_ptr->next=head1;
fast_ptr->next=head2;
c1.head=head1;
c2.head=head2;
}
cout<<"\n\tFIRST CIRCULAR LINKLIST:";
c1.display();
cout<<"\n\tSECOND CIRCULAR LINKLIST:";
c2.display();
}
}
int main()
{
/*
objective:to split a circular linklist into two halves
input parameters:
ele-(integer value)-element to be inserted into cirular linklist
return value:none
approach:-using function split
*/
circular c;
int ele;
char ch;
do
{
cout<<"\n\tENTER ELEMENT:";
cin>>ele;
c.add(ele);
cout<<"\n\twant to enter more(enter y or Y)";
cin>>ch;
}while(ch=='y'||ch=='Y');
cout<<"\n\tELEMENTS OF CIRCULAR LINKLIST:";
c.display();
c.split();
getch();
}