-
Notifications
You must be signed in to change notification settings - Fork 54
/
doubly_linked_list.cpp
165 lines (131 loc) · 3.26 KB
/
doubly_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
#include <iostream>
using namespace std;
class Node {
public:
Node *prev;
int data;
Node *next;
Node() {
this->prev = nullptr;
this->data = 0;
this->next = nullptr;
}
Node(int val) {
this->prev = nullptr;
this->data = val;
this->next = nullptr;
}
};
class DoublyLinkedList {
private:
Node *head;
public:
void traverse() {
Node *temp = this->head;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
temp = this->head;
while (temp->next != nullptr) {
temp = temp->next;
}
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->prev;
}
cout << endl;
}
void insertAtBeginning(int val) {
Node *newNode = new Node(val);
newNode->next = this->head;
this->head->prev = newNode;
this->head = newNode;
}
void insertAtGivenPostion(int val, int position) {
Node *temp = this->head;
for (int i = 2; i < position; i++) {
if (temp->next != nullptr) {
temp = temp->next;
}
}
Node *temp2 = temp;
Node *newNode = new Node(val);
temp2 = temp2->next;
newNode->next = temp2;
temp2->prev = newNode;
newNode->prev = temp;
temp->next = newNode;
}
void insertAtEnd(int val) {
Node *newNode = new Node(val);
if (this->head == nullptr) {
this->head = newNode;
return;
}
Node *temp = this->head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
void deleteAtBeginning() {
Node *temp = this->head;
temp = temp->next;
temp->prev = nullptr;
this->head = temp;
}
void deleteAtGivenPosition(int position) {
Node *temp = this->head;
for (int i = 2; i < position; i++) {
temp = temp->next;
}
Node *temp2 = temp;
temp->next = temp->next->next;
temp = temp->next;
temp->prev = temp2;
}
void deleteAtEnd() {
Node *temp = this->head;
while (temp->next->next != nullptr) {
temp = temp->next;
}
Node *temp2 = temp;
temp->next = nullptr;
}
int search(int val) {
Node *temp = this->head;
int inc = 1;
while (temp != nullptr) {
if (temp->data == val) {
return inc++;
}
temp = temp->next;
inc++;
}
return 0;
}
};
int main(int argc, char *argv[]) {
DoublyLinkedList l = DoublyLinkedList();
l.insertAtEnd(1);
l.insertAtEnd(2);
l.insertAtEnd(3);
l.insertAtEnd(4);
l.insertAtEnd(5);
l.insertAtEnd(6);
l.insertAtEnd(7);
l.insertAtBeginning(0);
l.insertAtGivenPostion(10, 6);
cout << l.search(10) << endl;
l.deleteAtBeginning();
l.deleteAtEnd();
l.deleteAtGivenPosition(5);
l.traverse();
cout << l.search(10) << endl;
cout << l.search(1) << endl;
cout << l.search(3) << endl;
return 0;
}