-
Notifications
You must be signed in to change notification settings - Fork 0
/
doubleLL.hpp
105 lines (94 loc) · 1.72 KB
/
doubleLL.hpp
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
#ifndef __DOUBLELL_HPP__
#define __DOUBLELL_HPP__
#include <iostream>
#include <vector>
using namespace std;
struct node {
int data;
struct node* next;
struct node* prev;
};
typedef struct node Node;
class DLL {
protected:
Node* _head;
Node* _tail;
unsigned int _numElems;
void deleteNode(Node* node);
Node* searchNode(const int data);
public:
DLL() {
_head = nullptr;
_tail = nullptr;
_numElems = 0;
}
void addNode(const int data);
void delNode(const int data); //searches data and deletes the first node
//containing data
void printList();
void printRev();
unsigned int numElems() {
return _numElems;
}
};
void DLL::printList() {
Node* curr = _head;
while(curr) {
cout << curr->data << "->";
curr = curr->next;
}
cout << "NULL" << endl;
}
void DLL::printRev() {
Node* curr = _tail;
while(curr) {
cout << curr->data << "<-";
curr = curr->prev;
}
cout << "NULL" << endl;
}
void DLL::delNode(const int data) {
Node* node = searchNode(data);
if(node)
deleteNode(node);
else
cout << "Node does not exist" << endl;
}
Node* DLL::searchNode(const int data) {
Node* curr = _head;
while(curr) {
if(curr->data == data)
return curr;
curr = curr->next;
}
return curr;
}
void DLL::deleteNode(Node* node) {
if(node->prev)
node->prev->next = node->next;
else
_head = node->next;
if(node->next)
node->next->prev = node->prev;
else
_tail = node->prev;
delete node;
_numElems--;
}
void DLL::addNode(const int data) {
Node* temp = new Node;
temp->data = data;
temp->next = nullptr;
if(!_head) {
_head = temp;
_tail = temp;
temp->prev = nullptr;
_numElems++;
return;
}
_tail->next = temp;
temp->prev = _tail;
_tail = temp;
_numElems++;
}
#endif // __DOUBLELL_HPP__