-
Notifications
You must be signed in to change notification settings - Fork 0
/
singlyLinkedLists.cpp
176 lines (175 loc) · 3.89 KB
/
singlyLinkedLists.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
/*
It is not always necessary to have a tail pointer when working with linked lists. If you need to frequently work with the end of the list, adding a tail pointer can be a convenient optimization. If you use a tail pointer, make sure all operations on the linked list take the tail pointer into account.
*/
#include <iostream>
#include <map>
using namespace std;
class Node
{
public:
int data;
Node *next;
Node(int data)
{
this->data = data;
this->next = NULL;
}
~Node()
{
int value = this->data;
if (this->next != NULL)
{
delete next;
this->next = NULL;
}
cout << "Memory is now free for node with data = " << value << endl;
}
};
void display(Node *head)
{
Node *temp = head;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
void insertAtHead(Node *&head, Node *&tail, int data)
{
Node *temp = new Node(data);
if (head == NULL)
{
head = temp;
tail = temp;
}
else
{
temp->next = head;
head = temp;
}
}
void insertAtTail(Node *&tail, int data)
{
Node *temp = new Node(data);
tail->next = temp;
tail = temp;
}
void insertAtPosition(Node *&head, Node *&tail, int position, int data) // NOTE: position != index
{
Node *temp = head;
if (position == 1)
insertAtHead(head, tail, data);
else
{
for (int i = 1; i <= position - 2; i++)
temp = temp->next;
if (temp->next == NULL)
insertAtTail(tail, data);
else
{
Node *nodeToInsert = new Node(data);
nodeToInsert->next = temp->next;
temp->next = nodeToInsert;
}
}
}
void deleteFromHead(Node *&head, Node *&tail)
{
if (head == NULL)
{
cout << "Empty linked list!" << endl;
return;
}
Node *temp = head;
head = head->next;
temp->next = NULL;
delete temp;
if (head == NULL)
tail = NULL;
}
void deleteNodeGivenPosition(Node *&head, Node *&tail, int position) // 'curr' = node to be deleted, 'prev' = node before curr
{
if (position == 1)
deleteFromHead(head, tail);
else
{
Node *curr = head;
Node *prev = NULL;
int count = 1;
while (count < position)
{
prev = curr;
curr = curr->next;
count++;
}
prev->next = curr->next;
if (curr == tail)
tail = prev;
curr->next = NULL;
delete curr;
}
}
void deleteNodeGivenValue(Node *&head, Node *&tail, int val)
{
if (head == NULL)
{
cout << "Empty linked list!" << endl;
return;
}
if (head->data == val)
{
deleteFromHead(head, tail);
return;
}
Node *prev = head;
while (prev->next != NULL && prev->next->data != val)
prev = prev->next;
if (prev->next == NULL)
{
cout << "Value not found in the linked list!" << endl;
return;
}
Node *curr = prev->next;
prev->next = curr->next;
if (curr == tail)
tail = prev;
curr->next = NULL;
delete curr;
}
Node *reverseIterative(Node *head)
{
if (head == NULL || head->next == NULL)
return head;
Node *prev = NULL;
Node *curr = head;
Node *forward = NULL;
while (curr != NULL)
{
forward = curr->next;
curr->next = prev;
prev = curr;
curr = forward;
}
return prev;
}
Node *reverseRecursive(Node *head)
{
if (head == NULL || head->next == NULL)
return head;
Node *reversedTail = reverseRecursive(head->next);
head->next->next = head;
head->next = NULL;
return reversedTail;
}
int getLength(Node *head)
{
int length = 0;
Node *temp = head;
while (temp != NULL)
{
length++;
temp = temp->next;
}
return length;
}