-
Notifications
You must be signed in to change notification settings - Fork 0
/
DlinkedList.cpp
84 lines (81 loc) · 1.55 KB
/
DlinkedList.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
#include "DlinkedList.h"
#include <string>
#include <iostream>
using namespace std;
template <class D> DoublyLinkedList<D>::DoublyLinkedList(){
root = NULL;
}
template <class D> DoublyLinkedList<D>::DoublyLinkedList(D _data){
//in development
add(_data);
}
template <class D> DoublyLinkedList<D>::~DoublyLinkedList(){
if(root == NULL){
return;
}
else{
Node<D> *tmp = root;
//get to the end of the linkedlist
while(tmp->next != NULL){
tmp = tmp->next;
}
//deletes every Node starting from the back
while(tmp != NULL){
Node<D> *unwanted = tmp;
tmp = tmp->previous;
delete unwanted;
}
//delete tmp;
}
}
template <class D> int DoublyLinkedList<D>::add(D data){
Node<D> *p = new Node<D>(data);
if(root == NULL){
root = p;
root->data.setId(0);
return 0;
}
else{
int count = 1;
Node<D> *tmp = root;
while(tmp->next != NULL){
tmp = tmp->next;
count++;
}
tmp->next = p;
tmp->next->data.setId(count);
return 0;
}
}
template <class D> int DoublyLinkedList<D>::remove(int _id){
//development
if(root == NULL){
return 1;
}
else{
Node<D> *tmp = root;
while(tmp != NULL){
if(tmp->data.getId() == _id){
(tmp->previous)->next = tmp->next;
(tmp->next)->previous = tmp->previous;
delete tmp;
return 0;
}
tmp = tmp->next;
}
return 1;
}
}
template <class D> Node<D>::Node(){
//data = NULL;
previous = NULL;
next = NULL;
}
template <class D> Node<D>::Node(D _data){
data = _data;
previous = NULL;
next = NULL;
}
template <class D> Node<D>* DoublyLinkedList<D>::getRoot(){
return root;
}