-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2) Double Linked List.cpp
201 lines (166 loc) · 3.92 KB
/
2) Double 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
#include<iostream>
using namespace std;
class Node{
public:
int data;
Node* prev;
Node* next;
// Constructor
Node(int val) : data(val) , prev(NULL) , next(NULL) {} ;
};
class DoubleLinkedList{
int size;
Node* head;
Node* tail;
public:
DoubleLinkedList() : head(NULL) , tail(NULL) ,size(0) {} ;
void insertHead(int);
void insertTail(int);
void insertAtIndex(int,int);
bool isempty();
void DeleteEnd();
void DeleteHead();
void deleteAtIndex(int);
int len();
void display();
void Reverse();
// void reverseRecursion(Node*,Node*,int);
};
int DoubleLinkedList :: len(){
return size;
}
void DoubleLinkedList :: insertHead(int val){
size++;
Node* newnode = new Node(val);
if(head==NULL){
head = tail = newnode;
return;
}
newnode->next = head;
head->prev = newnode;
head = newnode;
}
void DoubleLinkedList :: insertTail(int val){
size++;
Node* newnode = new Node(val);
if(tail==NULL){
head = tail = newnode;
return;
}
tail->next = newnode;
newnode->prev = tail;
tail = newnode;
}
void DoubleLinkedList :: insertAtIndex(int val,int pos){
if(pos==0){
insertHead(val);
}
else if(pos==len()){
insertTail(val);
}
else{
size++;
Node* newnode = new Node(val);
int currpos = 0 ;
Node* temp = head;
while(temp && currpos!=pos-1){
temp = temp->next;
currpos++;
}
newnode->next = temp->next;
newnode->prev = temp;
temp->next->prev = newnode;
temp->next = newnode;
}
}
void DoubleLinkedList :: DeleteHead(){
if(!head || !tail) cout << "Nothing to delete\n";
else{
size--;
head = head->next;
head->prev = NULL;
}
}
void DoubleLinkedList :: DeleteEnd(){
if(!head || !tail) cout << "Nothing to delete\n";
else{
size--;
tail = tail->prev;
tail->next = NULL;
}
}
void DoubleLinkedList :: deleteAtIndex(int pos){
if(pos==0){
DeleteHead();
}
else if(pos==len()-1){
DeleteEnd();
}
else{
size--;
int currpos = 0;
Node* temp = head;
while(temp && currpos!=pos){
temp = temp->next;
currpos++;
}
// Now currpos is at pos
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
}
}
void DoubleLinkedList :: Reverse(){
if(!head || !tail) return ;
Node* curr = head;
while(curr!=nullptr){
Node* next_item_ptr = curr->next;
curr->next = curr->prev;
curr->prev = next_item_ptr;
curr = next_item_ptr;
}
// Now swap head and tail
swap(head,tail);
return;
}
// void DoubleLinkedList :: reverseRecursion(Node* curr,Node* next_item_ptr,int l){
// if(l==len()){
// swap(head,tail);
// return ;
// }
// if(next_item_ptr!=nullptr && curr->next!=nullptr){
// curr->next = curr->prev;
// curr->prev = next_item_ptr;
// next_item_ptr = curr->next; // Save the next pointer before updating it
// curr = next_item_ptr;
// reverseRecursion(curr, next_item_ptr,l+1);
// }
// }
void DoubleLinkedList :: display(){
if(head && tail){
Node* temp = head;
cout << "NULL<-";
while(temp){
cout << temp->data << "<->" ;
temp = temp->next;
}cout << "NULL\n";
}
}
int main(){
DoubleLinkedList dll;
for(int i=1;i<=5;i++){
dll.insertTail(i);
}
dll.display();
dll.insertHead(121);
dll.display();
dll.DeleteEnd();
dll.DeleteHead();
dll.display();
dll.insertAtIndex(500,3);
dll.display();
dll.deleteAtIndex(3);
dll.display();
dll.Reverse();
dll.display();
return 0;
}