-
Notifications
You must be signed in to change notification settings - Fork 45
/
Circular Linkedlist.cpp
174 lines (171 loc) · 3.73 KB
/
Circular Linkedlist.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node *next;
Node(int x)
{
data = x;
next = NULL;
}
};
void printlist(Node *head) //this is used to print all the elem in a clili
{
if (head == NULL)
return;
Node *p = head;
do
{
cout << p->data << " ";
p = p->next;
} while (p != head);
}
Node *insertBegin(Node *head, int data) //this is in O(n)
{
Node *temp = new Node(data);
if (head == NULL)
{
temp->next = temp;
}
else
{
Node *curr = head;
while (curr != head)
{
curr = curr->next;
}
curr->next = temp;
temp->next = head;
}
return temp;
}
Node *smartInsert(Node *head, int data)
{
Node *temp = new Node(data);
if (head == NULL)
{
temp->next = temp;
return temp;
}
else
{
temp->next = head->next;
head->next = temp;
int t = head->data;
head->data = temp->data;
temp->data = t;
return head;
}
} // this is smart insert here instead of finding the
//last node aka tail we ccan just add a node at 2nd place and exachange value
//with the head so the node will remain same ie the last node will still point to the
//head node , but the value will be exached with 2nd node. heence we can insert in
// contant time. GGWP
Node *insertEnd(Node *head, int data)
{
if (head == NULL)
{
Node *temp = new Node(data);
temp->next = temp;
return temp;
}
else
{
Node *curr = head;
while (curr->next != head)
{
curr = curr->next;
}
Node *temp = new Node(data);
curr->next = temp;
temp->next = head;
return head;
}
}
Node *smartEnd(Node *head, int data)
{
Node *temp = new Node(data);
if (head == NULL)
{
temp->next = temp;
return temp;
}
else
{
temp->next = head->next;
head->next = temp;
int t = head->data;
temp->data = t;
head->data = data;
}
return temp;
}
//similar to the smart insert , this one inserts in constant time in the end
//of the circularlili (same concept)
Node *delHead(Node *head) //O(n)time
{
if (head == NULL)
{
return NULL;
}
if (head->next == head)
{
delete head;
return NULL;
}
Node *curr = head;
while (curr->next != head)
{
curr = curr->next;
}
curr->next = head->next;
delete head;
return (curr->next);
}
Node *smartDel(Node *head)
{
if (head == NULL)
{
return NULL;
}
if (head->next == head)
{
delete head;
return NULL;
}
Node *curr = head->next;
head->data = head->next->data;
head->next = head->next->next;
delete curr;
return head;
}
Node *delKth(Node *head, int k)
{
if (head == NULL)
return head;
if (k == 1)
return smartDel(head);
Node *curr = head;
for (int i = 0; i < k - 2; i++)
{
curr = curr->next;
}
Node *temp = curr->next;
curr->next = curr->next->next;
delete temp;
return head;
}
int main()
{
Node *head = new Node(10); //this is a better way to makeing a lili
head->next = new Node(20); //without using temp variables
head->next->next = new Node(30);
head->next->next->next = new Node(40);
head->next->next->next->next = head;
head = smartInsert(head, 5);
// Node *head = NULL;
head = smartEnd(head, 60);
head = smartDel(head);
printlist(head);
}