forked from crandino/Linked_List
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DList.h
113 lines (100 loc) · 1.71 KB
/
DList.h
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
#ifndef __DLIST_H__
#define __DLIST_H__
class DList {
private:
node* start;
public:
DList()
{
start = NULL;
}
unsigned int count() const
{
unsigned int counter = 0;
node* tmp = start;
while (tmp != NULL)
{
tmp = tmp->next;
counter++;
}
return counter;
}
void add(int _value)
{
node* new_node = new node;
new_node->value = _value;
new_node->next = NULL;
if (start != NULL)
{
node* tmp = start;
while (tmp->next != NULL)
tmp = tmp->next;
tmp->next = new_node;
new_node->previous = tmp;
}
else
{
new_node->previous = NULL;
start = new_node;
}
}
node* getNodeAtPos(unsigned int _pos) const
{
// Node 1 is zero, node 2 is one, etc.
if (start != NULL && _pos < count())
{
unsigned int pos_counter = 0;
node* tmp = start;
while (pos_counter != _pos)
{
tmp = tmp->next;
pos_counter++;
}
return tmp;
}
else
return NULL;
}
void del(node* _node)
{
if (start != NULL && _node != NULL)
{
if (start != _node)
{
node* tmp = start;
while (tmp->next != _node)
tmp = tmp->next;
tmp->next = _node->next;
if (_node->next != NULL)
_node->next->previous = tmp;
}
else
{
start = _node->next;
_node->next->previous = NULL;
}
delete _node;
}
}
void info() const
{
if (start != NULL) {
node* tmp = start;
unsigned int node_num = 1;
while (tmp != NULL)
{
printf("%s %d: %s %d | %s %p | %s %p | %s %p\n",
"Number", node_num,
"Value", tmp->value,
"Node", tmp,
"Next", tmp->next,
"Previous", tmp->previous);
tmp = tmp->next;
node_num++;
}
}
else
printf("%s", "Empty Double Linked List");
}
};
#endif // !__DLIST_H__