forked from TechVine/DSA--Hacktoberfest-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked-list-deletion.cpp
188 lines (166 loc) · 4.27 KB
/
linked-list-deletion.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
#include <iostream>
using namespace std;
// Node
class Node {
public:
int data = 0;
Node *next;
Node () {
data = 0;
next = NULL;
}
Node(int data){
this->data = data;
next = NULL;
}
};
// Some Global Nodes
Node* head;
Node* first;
// LinkedList
class LinkedList {
public:
LinkedList(){
head = NULL;
}
// insert at begining
bool isEmpty(Node* head) {
if (head == NULL) return true;
return false;
}
void insertNode(int data);
// HacktoberFest Task Deletion
void deleteAtFront();
void deleteInBetween(int index);
void deleteAtLast();
// display Linked-List
void display(Node* head);
};
// insert Node
void LinkedList::insertNode(int data) {
// if list is not empty
if (!isEmpty(head))
{
first = head;
Node* node = new Node(data);
while (first->next != NULL)
{
first = first->next;
}
first->next = node;
cout << "inserted " << node->data << endl;
}
else {
head = new Node(data);
cout << "inserted " << head->data << endl;
}
}
/********************** Deletion *************************/
void LinkedList::deleteAtFront(){
if (!isEmpty(head))
{
Node *temp = head;
head = temp->next;
temp->next = NULL;
cout << "deleted front: " << temp->data << endl ;
delete temp;
} else {
cout << "List is empty...\n";
}
}
void LinkedList::deleteInBetween(int index){
if (!isEmpty(head))
{
Node *temp = head;
int listLen = 0;
while (temp != NULL)
{
temp = temp->next;
listLen++;
}
if (index < listLen-1 && index != 0) // if index is zero means the head Node
{
Node* prev;
temp = head;
for (int i = 0; i < index; i++)
{
prev = temp;
temp = temp->next;
}
prev->next = temp->next;
temp->next = NULL;
cout << "deleted " << temp->data << " Index: " << index << endl ;
delete temp;
}
else
{
cout << "\n--------- IndexOutOfRangeError" << endl ;
cout << "\tcan't delete at index: " << index << endl;
}
} else {
cout << "List is empty...\n";
}
}
void LinkedList::deleteAtLast(){
if (!isEmpty(head))
{
// store the head into temp
Node *temp = head, *lastSecond;
while (temp->next != NULL) // lastSecond will point to lastSecond Node
{
lastSecond = temp;
temp = temp->next;
}
if (head->next == NULL) // it means, there is only one single-node, i.e. head
head = NULL;
else // break the link of last-Node and lastSecond node
lastSecond->next = NULL;
cout << "deleted last: " << temp->data << endl;
delete temp;
}
else
cout << "List is empty...\n";
}
void LinkedList::display(Node* head) {
Node* temp = head;
if (head != NULL)
{
cout << "\nLinked-List elements: ";
while (temp != NULL)
{
cout << temp->data << " " ;
temp = temp->next;
}
cout << endl ;
return;
}
cout << "Linked-List is empty...\n";
}
int main()
{
LinkedList ll;
ll.insertNode(1);
ll.insertNode(2);
ll.insertNode(3);
ll.insertNode(4);
ll.insertNode(5);
ll.insertNode(6);
ll.insertNode(7);
ll.insertNode(8);
ll.insertNode(9);
ll.insertNode(10);
ll.deleteAtFront();
ll.deleteAtFront();
ll.deleteAtLast();
ll.deleteAtLast();
// 1 <= index <= last_element_index-1 ------> if it's not then IndexOutOfRangeError
ll.deleteInBetween(0); // IndexOutOfRangeError
ll.display(head);
ll.deleteInBetween(1);
ll.display(head);
ll.deleteInBetween(3);
ll.display(head);
ll.deleteInBetween(4); // IndexOutOfRangeError because at this time there are only 4 elements are present
ll.display(head);
return 0;
}