-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSinglyLinkedlist.cpp
More file actions
231 lines (209 loc) · 4.43 KB
/
SinglyLinkedlist.cpp
File metadata and controls
231 lines (209 loc) · 4.43 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include<bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node* next;
Node(int data)//constructor
{
this->data=data;
this->next=NULL; //make every node to point null
}
~Node()
{
int val=this->data;
if(this->next!=NULL)
{
delete next;
this->next=NULL;
}
}
};
void insertathead(Node* &head,int d)
{
Node* temp=new Node(d); //creating new node
temp->next=head;
temp=head;
}
void printlist(Node* &head)
{
Node* temp=head;
while (temp!=NULL)
{
/* code */
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
void insertattail(Node* &tail,int d)
{
//creating new node
Node* temp=new Node(d);
//inserting at tail
tail->next=temp;
tail=temp;
}
void insertatposition(Node* &tail,Node* &head,int posn,int d)
{
///if posn=1; at start
if(posn==1)
{
insertathead(head,d);
return;
}
///traverse for posn
Node* temp=head; //temp points head
int cnt=1; //starting at head
while (cnt<posn-1)
{
/* code */
temp=temp->next;
cnt++;
}
if(temp->next==NULL)
{
insertattail(tail,d);
return;
}
Node* nodetoinsert=new Node(d);
nodetoinsert->next=temp->next;
temp->next=nodetoinsert;
}
void deletenode(int posn,Node* &head)
{
if(posn==1)
{
Node* temp=head;
head=head->next;
//memory pointer free
temp->next=NULL;
delete temp;
}
else
{
Node *curr=head;
Node *prev=NULL;
int cnt=1;
while (cnt<posn)
{
/* code */
prev=curr;
curr=curr->next;
cnt++;
}
prev->next=curr->next;
curr->next=NULL;
delete curr;
}
}
bool detectcycle(Node* head)
{
if(head==NULL)
return false; //empty list
map<Node*,bool>visited;
Node* temp=head;
while(temp!=NULL)
{
if(visited[temp]==true)
cout<<"cycle at:"<<temp->data<<endl;
return true;
//if not then mark true
visited[temp]=true;
temp=temp->next;
}
return false; //not a cycle
}
Node* floyddetectionloop(Node* head)
{
if(head==NULL)
return NULL; //empty list
Node* slow=head;
Node* fast=head;
//both slow and fast pointing to head
while(slow!= NULL && fast!=NULL)
{
fast=fast->next; //1st time
//checkinf=g if fast is null
if(fast!=NULL)
{
fast=fast->next; //2nd time
}
slow=slow->next; //1 time
if(slow==fast)
{
//cycle detected
cout<<"cycle at: "<<slow->data<<endl;
return slow;
}
}
return NULL; //no
}
Node* getstartingnodeofloop(Node* head)
{
if(head==NULL)
return NULL; //empty list
Node* intersection=floyddetectionloop(head);
Node* slow=head;
if(intersection==NULL)
return NULL;
while (slow!=intersection)
{
/* code */
slow=slow->next;
intersection=intersection->next;
}
return slow;
}
Node* removeloop(Node* head)
{
if(head==NULL)
return NULL;
Node* startofloop=getstartingnodeofloop(head);
Node* temp=startofloop;
if(startofloop==NULL)
{
return head;
}
while (temp->next!=startofloop)
{
temp=temp->next;
/* code */
//temp points to node before startofloop
}
temp->next=NULL;
return head;
}
int main()
{
Node* node1=new Node(10); //stored in heap
// cout<<n->data<<endl;
// cout<<n->next<<endl;
Node* head=node1;
Node* tail=node1;
// printlist(head);
insertattail(tail,12);
// printlist(head);
insertattail(tail,15);
// printlist(head);
insertatposition(tail,head,4,22);
// printlist(head);
// cout<<"at head:"<<head->data<<endl;
// cout<<"at tail:"<<tail->data<<endl;
// deletenode(3,head);
// printlist(head);
tail->next=head->next;
if(floyddetectionloop(head))
{
cout<<"cycle is present"<<endl;
}
else
{
cout<<"cycle is not present"<<endl;
}
Node* loop=getstartingnodeofloop(head);
cout<<loop->data<<" is starting node of loop"<<endl;
removeloop(head);
return 0;
}