-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdoubleLinkedList.cpp
More file actions
106 lines (99 loc) · 1.45 KB
/
doubleLinkedList.cpp
File metadata and controls
106 lines (99 loc) · 1.45 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
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
Node* next;
Node* prev;
};
class doublyLinkedList
{
Node* head;
Node* tail;
public:
doublyLinkedList()
{
head = NULL;
tail = NULL;
}
void addNode(int value)
{
Node* temp = new Node;
temp->data = value;
temp->prev = NULL;
temp->next = NULL;
if(head == NULL)
{
head = temp;
tail = temp;
}
else
{
tail->next = temp;
temp->prev = tail;
temp->next = NULL;
tail = temp;
}
}
bool findNode(int value)
{
Node* temp = tail;
while(temp != NULL)
{
if(temp->data == value)
{
return true;
}
else
{
temp = temp->prev;
}
}
return false;
}
bool deleteNode(int value)
{
if(head == NULL) return false;
Node* currNode = head;
Node* prevNode = NULL;
while(currNode != NULL)
{
if(currNode->data == value){
prevNode->next = currNode->next;
Node* temp = currNode->next;
temp->prev = prevNode;
return true;
}
else
{
prevNode = currNode;
currNode = currNode->next;
}
}
return false;
}
void display()
{
Node* temp = head;
while(temp != NULL)
{
cout << temp->data << "<=>";
temp = temp->next;
}
cout << endl;
}
};
int main()
{
doublyLinkedList obj;
obj.addNode(4);
obj.addNode(5);
obj.addNode(7);
obj.display();
cout << obj.findNode(5) << endl;
cout << obj.findNode(12) << endl;
obj.addNode(12);
cout << obj.deleteNode(7) << endl;
obj.display();
return 0;
}