forked from aarghost106/beginner-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.cpp
240 lines (208 loc) · 4.21 KB
/
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
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
232
233
234
235
236
237
238
239
240
#include <iostream>
using namespace std;
struct Node
{
int data;
Node* next;
Node(int val)
{
data = val;
next = NULL;
}
};
int size(Node* &head)
{
int size = 0;
Node* n = head;
while(n!=NULL)
{
n = n->next;
size++;
}
return(size);
}
void insertAtStart(Node* &head, int x)
{
Node* new_node = new Node(x);
new_node->next = head;
head = new_node;
}
void insertAtTail(Node* &head, int x)
{
Node* n = new Node(x);
if(head == NULL)
{
head = n;
return;
}
Node* temp = head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next = n;
}
void insert(Node* &head, int x, int k) //inserting a node with data x at position k where k = 0, 1, 2, 3.... Here if k> size of linked list, it inserts element at the last
{
Node* n =new Node(x);
if(k==0)
{
insertAtStart(head, x);
return;
}
Node* prev = head;
Node* current;
while(--k)
{
if(prev->next == NULL)
{
insertAtTail(head,x);
return;
}
prev = prev->next;
}
current = prev->next;
prev->next = n;
n->next = current;
}
void deleteNode(Node* &head, int k) //deleting a node at the kth position where k = 0, 1, 2, 3...
{
if(k==0)
{
head = head->next;
return;
}
Node* prev = head;
Node* current;
while(--k)
{
prev = prev->next;
if (prev==NULL)
{
cout<<"There is no node at this position\n";
return;
}
}
current = prev->next->next;
prev->next = current;
}
Node* reverse(Node* n)
{
Node* current = n;
Node* prev = NULL;
Node* temp;
while(current!=NULL)
{
temp = current->next;
current->next = prev;
prev = current;
current = temp;
}
return prev;
}
void reverseUsingRecursion(Node* n, Node* &head)
{
if(n->next == NULL)
{
head = n;
return;
}
reverseUsingRecursion(n->next, head);
Node* q = n->next;
q->next = n;
n->next = NULL;
}
void reverseSetOfNodes(Node* &head, int start, int end) //reversing set of nodes from positons start to end, both inclusive
{
if(start>end)
{
cout<<"Invlid parameters\n";
return;
}
Node* s = head;
Node* e = head;
Node* prev = NULL;
for(int i = 0; i<end; i++)
{
if(i<start)
{
prev = s;
s = s->next;
}
if(e->next == NULL)
break;
e = e->next;
}
Node* tail = e->next;
e->next = NULL; //by making e->next = NULL, we create a independent linked list from start to end. So now pointer of start can be sent to reverse() to reverse the linked list from start to end
if(start==0)
{
e = reverse(s);
head = e;
s->next = tail;
return;
}
e = reverse(s);
prev->next = e;
s->next = tail;
}
void reversek(Node* &head, int k) //reverses every k nodes
{
int count;
if(size(head)%k==0)
count = size(head)/k;
else
count = (size(head)/k)+1;
int start = 0, end = k-1;
for(int i=0;i<count;i++)
{
reverseSetOfNodes(head,start,end);
start+=k;
end+=k;
}
}
void printList(Node* n)
{
while(n!=NULL)
{
cout<<n->data<<" ";
n=n->next;
}
cout<<"\n";
}
void printReverse(Node* n)
{
if(n == NULL)
return;
printReverse(n->next);
cout<<n->data<<" ";
}
int main()
{
Node* head = NULL;
Node* second = NULL;
Node* third = NULL;
head = new Node(1);
second = new Node(2);
third = new Node(5);
head->next=second;
second->next=third;
third->next=NULL;
insertAtTail(head, 10);
insertAtStart(head, 23);
insertAtStart(head, 54);
insertAtStart(head, 14);
insertAtTail(head,23);
insert(head, 15, 2);
insert(head, 7, 0);
insert(head, 85, 100);
head = reverse(head);
deleteNode(head, 2);
deleteNode(head, 0);
reverseUsingRecursion(head, head);
printList(head);
reverseSetOfNodes(head,2,5);
printList(head);
reversek(head,7);
printList(head);
}