-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoublyList.cpp
More file actions
93 lines (87 loc) · 1.71 KB
/
DoublyList.cpp
File metadata and controls
93 lines (87 loc) · 1.71 KB
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
//Doubly Linked List
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
Node *next;
Node *prev;
int data;
Node(int x) {
prev = nullptr;
next = nullptr;
data = x;
}
};
//5<->11<->4<->8<->7-->nullptr
int deleteNode(Node** head, int index) {
if (index == 0) {
Node* temp = *head;
*head = (*head)->next;
int x = temp->data;
delete temp;
return x;
}
else {
Node*cur = *head;
while (index != 0 && cur->next != nullptr) {
cur = cur->next;
index--;
}
Node*pre = cur->prev;
pre->next = cur->next;
if (cur->next) {
cur->next->prev = cur->prev;
}
int x = cur->data;
delete cur;
return x;
}
}
void insertNode(Node** head, int data, int index) {
Node* new_node = new Node(data);
if (index == 0) {
new_node->next = *head;
if (*head != nullptr) {
(*head)->prev = new_node;
}
*head = new_node;
}
else {
Node*cur = *head;
while (index != 0 && cur->next != nullptr) {
cur = cur -> next;
index--;
}
Node*pre = cur->prev;
pre->next = new_node;
new_node->prev = pre;
new_node->next = cur;
cur->prev = new_node;
}
}
void printDoublyList(Node *head) {
while (head != nullptr) {
cout << head->data << " ";
head = head->next;
}
cout << endl;
}
int main() {
Node* head = nullptr;
insertNode(&head, 20, 0);
insertNode(&head, 22, 0);
printDoublyList(head);
insertNode(&head, 24, 1);
printDoublyList(head);
insertNode(&head, 10, 1);
printDoublyList(head);
insertNode(&head, 12, 2);
printDoublyList(head);
deleteNode(&head, 0);
deleteNode(&head, 0);
printDoublyList(head);
deleteNode(&head, 2);
deleteNode(&head, 1);
printDoublyList(head);
return 0;
}