-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions on doubly linkedlist.cpp
170 lines (170 loc) · 3.11 KB
/
functions on doubly linkedlist.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
#include<iostream>
using namespace std;
template<class T>
class DoublyLinked {
public:
class node {
public:
T data;
node* next;
node* prev;
node(T val) {
data = val;
next = NULL;
prev = NULL;
}
};
DoublyLinked()
{
head = NULL;
tail = NULL;
}
void InsertAtHead(T const element) {
node* newNode = new node(element);
if (head == NULL) {
head = newNode;
tail = newNode;
}
else {
newNode->next = head;
head->prev = newNode;
head = newNode;
}
}
void InsertAtTail(T const element) {
node* newNode = new node(element);
if (head == NULL) {
head = newNode;
tail = newNode;
}
else {
node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
tail = newNode;
}
}
void print() {
node* temp = head;
while (temp != NULL) {
cout << temp->data << "->";
temp = temp->next;
}
cout << "NULL" << endl;
}
void reversePrint() {
node* temp = head;
while (temp != NULL) {
temp = temp->next;
}
temp=tail;
while (temp != NULL) {
cout << temp->data << "->";
temp = temp->prev;
}
cout << "NULL" << endl;
}
void DeleteAtHead() {
if (head == NULL) {
return;
}
node* temp = head;
if (head->next != NULL) {
head = temp->next;
head->prev = NULL;
}
else {
head = NULL;
tail = NULL;
}
delete temp;
}
void DeleteAtTail() {
if (head == NULL) {
return;
}
node* temp = head;
while (temp->next->next != NULL) {
temp = temp->next;
}
temp->next= NULL;
tail = temp;
delete temp->next;
}
void deleteAtPosition(int k) {
int SIZE = 0;
node* size = head;
node* temp=NULL, * current = head;
while (size != NULL) {
size = size->next;
SIZE++;
}
if (k == 0 || k > SIZE) {
return;
}
else if (k == 1) {
current = head;
head = head->next;
}
else if (k == size) {
while (current->next->next != NULL) {
current = current->next;
}
current->next = NULL;
}
else {
SIZE = 1;
while (current != NULL && SIZE != k) {
temp = current;
current = current->next;
SIZE++;
}
temp->next = current->next;
current->next->prev = current->prev;
}
delete current;
}
~DoublyLinked() {
while (head != nullptr) {
node* temp = head;
head = head->next;
delete temp;
}
}
private:
node* head;
node* tail;
};
int main() {
DoublyLinked<int> L1;
int k;
cout << "Inserting At Head : " << endl;
L1.InsertAtHead(5);
L1.InsertAtHead(4);
L1.InsertAtHead(3);
L1.InsertAtHead(2);
L1.InsertAtHead(1);
L1.print();
cout << "Inserting At Tail : " << endl;
L1.InsertAtTail(6);
L1.InsertAtTail(7);
L1.InsertAtTail(8);
L1.InsertAtTail(9);
L1.print();
cout << "Reverse Linked List : " << endl;
L1.reversePrint();
cout << "Delete At Start : "<<endl;
L1.DeleteAtHead();
L1.print();
cout << "Delete At Tail : " << endl;
L1.DeleteAtTail();
L1.print();
cout << "Delete At Postion : ";
cin >> k;
L1.deleteAtPosition(k);
L1.print();
return 0;
}