-
Notifications
You must be signed in to change notification settings - Fork 0
/
DLL_delete.cpp
115 lines (109 loc) · 2.23 KB
/
DLL_delete.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
#include <iostream>
using namespace std;
class Node{
public:
Node* prev;
int data;
Node* next;
Node(int data = 0){
this->prev = NULL;
this->data = data;
this->next = NULL;
}
~Node(){
cout<<"Deleted: "<<this->data<<endl;
prev = NULL;
next = NULL;
delete prev;
delete next;
}
};
void print(Node* &head){
Node* temp = head;
while(temp){
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
}
int length(Node* &head){
Node* temp = head;
int c = 0;
while(temp){
c++;
temp = temp->next;
}
return c;
}
void insertAtHead(Node* &head, int data){
Node* temp = new Node(data);
if(head == NULL){
head = temp;
return;
}
temp->next = head;
head->prev = temp;
head = temp;
}
void deleteNode(Node* &head, int pos){
if(head == NULL){
cout<<"Empty LL"<<endl;
return;
}
Node* temp = head;
// if single node is present
if(head->next == NULL){
head = NULL;
delete temp;
return;
}
// Head deletion
if(pos == 0){
head = head->next;
head->prev = NULL;
delete temp;
return ;
}
// Invalid pos
if(pos >= length(head)){
cout<<"Invalid pos"<<endl;
return ;
}
// last node
if(pos == length(head)-1){
Node *p = head;
while(temp->next) temp = temp->next;
p = temp->prev;
p->next = NULL;
delete temp;
return;
}
// Middle node
else{
Node* p = NULL;
int i = 1;
while(i <= pos && temp){
p = temp;
temp = temp->next;
i++;
}
temp->next->prev = p;
p->next = temp->next;
delete temp;
}
}
int main()
{
Node* head = NULL;
insertAtHead(head,20);
insertAtHead(head,30);
insertAtHead(head,40);
insertAtHead(head,50);
insertAtHead(head,60);
insertAtHead(head,70);
deleteNode(head,5);
// deleteNode(head,2);
// deleteNode(head,0);
print(head);
return 0;
}