-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDLinkedList.cpp
70 lines (58 loc) · 1.7 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
#include "DLinkedList.hpp"
// Constructor
DLinkedList::DLinkedList(){
header = new DNode;
trailer = new DNode;
header->next = trailer;
trailer->prev = header;
}
// Destructor
DLinkedList::~DLinkedList(){
while(!isEmpty()) removeFront();
delete header;
delete trailer;
}
// Check if the doubly linked list is empty
bool DLinkedList::isEmpty() const{
return (header->next == trailer);
}
// Return the first element of the doubly linked list
const Elem& DLinkedList::front() const{
return header->next->elem;
}
// Return the last element of the doubly linked list
const Elem& DLinkedList::back() const{
return trailer->prev->elem;
}
// Add a new element at the front of the doubly linked list
void DLinkedList::addFront(const Elem& element){
add(header->next, element);
}
// Add a new element at the back of the doubly linked list
void DLinkedList::addBack(const Elem& element){
add(trailer, element);
}
// Remove the first element from the front of the doubly linked list
void DLinkedList::removeFront(){
remove(header->next);
}
// Remove the last element from the back of the doubly linked list
void DLinkedList::removeBack(){
remove(trailer->prev);
}
// Insert a new node before the reference node
void DLinkedList::add(DNode* refNode, const Elem& element){
DNode* newNode = new DNode;
newNode->elem = element;
newNode->next = refNode;
newNode->prev = refNode->prev;
refNode->prev->next = refNode->prev = newNode;
}
// Remove the reference node
void DLinkedList::remove(DNode* refNode){
DNode* predecessor = refNode->prev;
DNode* successor = refNode->next;
predecessor->next = successor;
successor->prev = predecessor;
delete refNode;
}