-
Notifications
You must be signed in to change notification settings - Fork 12
/
loopInLinkedList.cpp
160 lines (124 loc) · 2.61 KB
/
loopInLinkedList.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
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
// C program to detect a loop in linked list and removing a loop from the list
#include
#include
struct node
{
int data;
struct node *next;
};
void print_list(struct node *head)
{
while(head)
{
printf(“%d “, head->data);
head = head->next;
}
printf(“\n\n”);
}
void insert_front(struct node **head, int dataue)
{
struct node * new_node = NULL;
new_node = (struct node *)malloc(sizeof(struct node));
if (new_node == NULL)
{
printf(“\nUnable to allocate memory\n”);
}
new_node->data = dataue;
new_node->next = *head;
*head = new_node;
}
void create_loop(struct node *head)
{
struct node *temp = head;
while(temp->next)
temp = temp->next;
temp->next = head->next;
}
void print_loop(struct node *head)
{
int n = 25;
while(n–)
{
printf(“%d “, head->data);
head = head->next;
}
printf(“\n\n”);
}
void detect_loop(struct node *head)
{
struct node *slow = head;
struct node *fast = head;
while(slow && fast->next && fast->next->next)
{
if ((slow == fast->next) || (slow == fast->next->next ))
{
printf(“\nLinked list has a loop.\n”);
return;
}
slow = slow->next;
fast = fast->next->next;
}
printf(“\nLinked list does not have any loop.\n”);
}
void remove_loop(struct node *head, struct node *loop_node)
{
struct node *near = head;
struct node *far = head;
struct node *ptr = loop_node;
struct node *prev = NULL;
while(ptr->next != loop_node)
{
ptr = ptr->next;
far = far->next;
}
prev = far;
far = far->next;
while(near != far)
{
prev = far;
far = far->next;
near = near->next;
}
prev->next = NULL;
}
void detect_and_remove_loop(struct node *head)
{
struct node *slow = head;
struct node *fast = head;
while(slow && fast->next && fast->next->next)
{
if ((slow == fast->next) || (slow == fast->next->next ))
{
printf(“\nLinked list has a loop\n”);
remove_loop(head, slow);
return;
}
slow = slow->next;
fast = fast->next->next;
}
printf(“\nLinked list does not have any loop\n”);
}
int main()
{
int n, i, data;
struct node * head = NULL;
printf(“\nEnter number of elements : “);
scanf(“%d”, &n);
printf(“\nEnter the elements : ” );
for (i = 0; i < n; i++)
{
scanf(“%d”, &data);
insert_front(&head, data);
}
printf(“\nThe Linked List is : “);
print_list(head);
detect_loop(head);
printf(“\nCreating loop…\n”);
create_loop(head);
printf(“\nPrinting list with loop\n”);
print_loop(head);
printf(“\nRemoving loop…\n”);
detect_and_remove_loop(head);
printf(“\nList after removing loop:\n”);
print_list(head);
}