-
Notifications
You must be signed in to change notification settings - Fork 0
/
SinglylinkedList.cpp
205 lines (192 loc) · 4.15 KB
/
SinglylinkedList.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
// Implemented by Kritagya Kumra
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node *next = 0;
Node(int data)
{
this->data = data;
this->next = NULL;
}
~Node()
{
int value = this->data;
// Trying to free up the memory
if (this->next != NULL)
{
delete next;
this->next = NULL;
}
cout << "memory is free for node with data " << value << endl;
}
};
void insertAtHead(Node *&head, int d)
{
// For empty list
if (head == NULL)
{
Node *temp = new Node(d);
head = temp;
}
else
{
// Create new node
Node *temp = new Node(d);
temp->next = head;
head = temp;
}
}
void insertAtTail(Node *&tail, int d)
{
// Create new node
Node *temp = new Node(d);
tail->next = temp;
tail = tail->next;
}
void insertAtMiddle(Node *&head, Node *&tail, int d, int position)
{
if (position == 1)
{
insertAtHead(head, d);
return;
}
int count = 1;
// Create new node
Node *newNode = new Node(d);
Node *temp = head;
if (temp->next == NULL)
{
insertAtTail(tail, d);
return;
}
while (temp != NULL)
{
if (count != (position - 1))
{
count++;
}
else
{
newNode->next = temp->next;
temp->next = newNode;
break;
}
temp = temp->next;
}
}
void deleteElementByPosition(int position, Node *&head, Node *&tail)
{
// deleting first or start node
if (position == 1)
{
Node *temp = head;
head = head->next;
// memory free start ndoe
temp->next = NULL;
delete temp;
}
else
{
// deleting any middle node or last node
Node *curr = head;
Node *prev = NULL;
int cnt = 1;
while (cnt < position)
{
prev = curr;
curr = curr->next;
cnt++;
}
if (curr->next == NULL)
{
tail = prev;
}
prev->next = curr->next;
curr->next = NULL;
delete curr;
}
}
// void deleteElementByPosition(Node *&head, int position)
// {
// Node *temp = head;
// if (position == 1)
// {
// head = head->next;
// temp = temp->next;
// delete temp;
// return;
// }
// Node *element = head->next;
// int count = 1;
// while (count != position - 1)
// {
// temp = temp->next;
// count++;
// element = element->next;
// }
// temp->next = element->next;
// }
// void deleteElementByPosition(Node *&head, int position)
// {
// Node *temp = head;
// int count = 1;
// while (count != position - 1)
// {
// temp = temp->next;
// count++;
// }
// temp->next = (temp->next)->next;
// }
void deleteElementByValue(Node *&head, int value)
{
Node *temp = head;
Node *element = head->next;
while (element->data != value)
{
temp = temp->next;
element = element->next;
}
temp->next = element->next;
delete temp;
// temp->next = (temp->next)->next;
}
void printElements(Node *&head)
{
Node *temp = head;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
int main()
{
// Node *node1 = new Node(10);
// cout << node1->data << endl;
// cout << node1->next << endl;
// Node *head = node1;
// Node *tail = node1;
Node *head = NULL;
Node *tail = NULL;
insertAtHead(head, 11);
// insertAtHead(head, 12);
printElements(head);
insertAtTail(tail, 11);
insertAtTail(tail, 15);
insertAtMiddle(head, tail, 12, 3);
printElements(head);
// deleteElementByPosition(head, 3);
deleteElementByPosition(4, head, tail);
// cout << tail->data << endl;
// deleteElementByPosition(1, head);
// deleteElementByPosition(2, head);
// cout << tail->data << endl;
// cout << head->data;
// deleteElementByValue(head, 11);
printElements(head);
}
// Implemented by Kritagya Kumra