-
Notifications
You must be signed in to change notification settings - Fork 1
/
linked_list.cpp
executable file
·214 lines (211 loc) · 4.94 KB
/
linked_list.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
#include <iostream>
using namespace std;
//generic implementation of Singly Linked List using templates
template <class T>
struct node
{
T data;
struct node *next;
};
template <class T>
class LinkedList
{
private:
int size;
struct node<T> *start;
public:
LinkedList()
{
size = 0;
start = NULL;
}
struct node<T> *create_node(const T value)
{
struct node<T> *temp = new (struct node<T>);
temp->next = NULL;
temp->data = value;
return temp;
}
// functions to insert values
void insert_begin(const T value)
{
struct node<T> *temp = create_node(value);
temp->next = start;
start = temp;
size++;
}
void insert(const T value)
{
struct node<T> *temp = start;
if (temp != NULL)
{
while (temp->next != NULL)
temp = temp->next;
temp->next = create_node(value);
size++;
}
else
{
insert_begin(value);
}
}
void insert_pos(const T value, int pos)
{
if (pos == 1)
{
insert_begin(value);
}
else if (pos == size + 1)
{
insert(value);
}
else if (pos <= size)
{
struct node<T> *curr = create_node(value), *temp = start;
for (int i = 1; i < pos - 1; i++)
{
temp = temp->next;
}
curr->next = temp->next;
temp->next = curr;
size++;
}
else
{
cout << "\nInsertion at position " << pos << " is not possible\n";
}
}
// search the list
int search(const T value)
{
struct node<T> *temp = start;
for (int pos = 1; temp != NULL; pos++)
{
if (temp->data == value)
{
return pos;
}
temp = temp->next;
}
return -1;
}
// delete from list
void remove_pos(const int pos)
{
if (pos > 1 && pos <= size)
{
struct node<T> *curr = NULL, *temp = start;
for (int i = 1; i < pos - 1; i++)
{
temp = temp->next;
}
curr = temp->next;
temp->next = curr->next;
delete curr;
size--;
}
else if (pos == 1 && size >= pos)
{
struct node<T> *temp = start->next;
delete start;
start = temp;
size--;
}
else
{
cout << "\nNo such entry in list\n";
}
}
void remove(const T value)
{
remove_pos(search(value));
}
void reverse()
{
if (start != NULL && start->next != NULL)
{
struct node<T> *p1 = NULL, *curr = start, *p2 = start->next;
while (p2 != NULL)
{
curr->next = p1;
p1 = curr;
curr = p2;
p2 = p2->next;
}
curr->next=p1;
start = curr;
}
}
void sort()
{
T x;
struct node<T>* p1=NULL,*p2=NULL,*last=NULL;
if(start!=NULL && start->next!=NULL)
{
for(p1=start;p1!=NULL;p1=p1->next)
{
for(p2=start;p2->next!=last;p2=p2->next)
{
if(p2->data > p1->data)
{
x=p1->data;
p1->data=p2->data;
p2->data=x;
}
if(p2->next==last)
{ last=p2;
}
}
}
}
}
// getters
int getSize()
{
return size;
}
struct node<T> *getStart()
{
return start;
}
};
int main()
{
LinkedList<int> ll;
ll.insert_begin(5);
ll.insert_begin(6);
ll.insert(78);
ll.insert_pos(54, 4);
ll.insert_pos(8, 4);
ll.insert_pos(7, 1);
ll.insert_pos(8888, 12);
//7 6 5 78 8 54
struct node<int> *temp = ll.getStart();
for (; temp != NULL; temp = temp->next)
cout << temp->data << "\n";
cout << "\nsize=" << ll.getSize() << "\n";
cout << "\n78 found at position: " << ll.search(78) << "\n";
ll.remove(78);
ll.remove_pos(5);
ll.remove_pos(1);
ll.remove_pos(10);
//6 5 8
temp = ll.getStart();
for (; temp != NULL; temp = temp->next)
cout << temp->data << "\n";
cout << "\nsize=" << ll.getSize() << "\n";
ll.reverse();
temp = ll.getStart();
for (; temp != NULL; temp = temp->next)
cout << temp->data << "\n";
cout << "\nsize=" << ll.getSize() << "\n";
ll.insert(-1);
ll.insert(34);
ll.sort();
//-1 5 6 8 34
temp = ll.getStart();
for (; temp != NULL; temp = temp->next)
cout << temp->data << "\n";
cout << "\nsize=" << ll.getSize() << "\n";
return 0;
}