-
Notifications
You must be signed in to change notification settings - Fork 1
/
Linkedlist.h
207 lines (197 loc) · 4.96 KB
/
Linkedlist.h
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
#include <iostream>
#include <vector>
using namespace std;
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
template<typename T>
class LinkedListNode {
public:
T val;
LinkedListNode* next;
LinkedListNode(T data) {
next = NULL;
val = data;
}
~LinkedListNode()
{
if (next != NULL)
delete next;
}
};
template<typename T>
class LinkedList {
LinkedListNode<T>* head;
int s;
public:
LinkedList()
{
head = NULL;
s = 0;
}
LinkedList(vector<T> ve) {
s = 0;
int temp = ve.size();
for(int i=temp-1;i>=0;i--)
pushAtFront(ve[i]);
//cout<<s;
}
//LinkedList(LinkedListNode<T>* root) {
// head = root;
// }
void pushAtBack(T data) {
if (head == NULL) {
head = new LinkedListNode<T>(data);
return;
}
else {
LinkedListNode<T>* temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = new LinkedListNode<T>(data);
}
s++;
}
void pushAtFront(T data) {
LinkedListNode<T>* temp = new LinkedListNode<T>(data);
temp->next = head;
head = temp;
s++;
}
void print(){
LinkedListNode<T>* temp = head;
for(int i=0;i<s-1;i++) {
cout << temp->val << " -> ";
temp = temp->next;
}
cout<<temp->val<<endl;
cout<<"Size is "<<s<<endl;
}
bool isCircular(){
/// SLow and fast pointers
if (head == NULL)
return false;
LinkedListNode<T>* slow = head;
LinkedListNode<T>* fast = head;
while (fast != NULL&& fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast)
return true;
}
return false;
}
void makeCircular(int pos){
/// To make Linked List CIrcular
/// join end to the position
LinkedListNode<T> *temp = head;
LinkedListNode<T>* nodeToJoin;
int i = 0;
while (temp->next != NULL) {
if (i == pos)
nodeToJoin = temp;
temp = temp->next;
i++;
}
temp->next = nodeToJoin;
}
void swap(int pos1, int pos2){
if (head == NULL)
if (pos1 > pos2) {
/// Swap pos1 and pos2
pos1 = pos1 ^ pos2;
pos2 = pos1 ^ pos2;
pos1 = pos2 ^ pos1;
}
LinkedListNode<T> *prev1 = head, *prev2 = head, *temp, *temp2;
for (int i = 0; i < pos1 - 2; i++)
prev1 = prev1->next;
for (int i = 0; i < pos2 - 2; i++)
prev2 = prev2->next;
temp = prev1->next->next;
prev1->next->next = prev2->next->next;
prev2->next->next = temp;
temp2= prev1->next;
prev1->next = prev2->next;
prev2->next = temp2;
}
int size() {
return s;
}
///At
// square brackets
///reverse
T& operator[](int pos) {
if(pos>s) {
/// if pos is greater create nodes
for(int i=0;i<s-pos;i++)
{
LinkedListNode<T>* temp = new LinkedListNode<T>();
pushAtBack(temp);
}
}
return *at(pos);
}
T* at(int pos) {
if(pos>=s)
return NULL;
else {
LinkedListNode<T>* temp = head;
for(int i=0;i<pos;i++)
temp = temp->next;
return temp;
}
}
void reverse() {
head = rlist(head);
}
LinkedListNode<T>* reverse(LinkedListNode<T>* head) {
if(head==NULL||head->next==NULL)
return head;
LinkedListNode<T>* temp = reverse(head->next);
head->next->next = head;
head->next = NULL;
return temp;
}
void rotateRight(int pos) {
/// Rotating element ro the right;
LinkedListNode<T>* temp = head;
pos = pos % s ;
while(temp->next!=NULL)
temp = temp->next;
temp->next = head;
temp = head;
for(int i=0;i<pos-1;i++)
temp = temp->next;
head = temp->next;
temp->next = NULL;
}
void erase(int pos) {
/// Delete an element
LinkedListNode<T>* temp;
if(pos>=s)
return ;
else if(pos==s-1)
{
at(pos-1)->next = NULL;
return;
}
temp = at(pos-1);
temp->next = temp->next->next;
}
template<typename S>
S* mergePoint(S* head1, S* head2) {
S* temp1=head1;
S *temp2=head2;
if(head1==NULL||head2==NULL)
return NULL;
while(head1!=head2) {
head1 = head1->next;
head2 = head2->next;
if(head1==NULL && head2==NULL) return NULL;
if(head1==NULL) head1 = temp2;
if(head2==NULL) head2 = temp1;
}
return head1;
}
};
#endif