-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list.cpp
147 lines (136 loc) · 3.97 KB
/
linked_list.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
#include <string>
#include <type_traits>
#include "i_link.hpp"
#include "linked_list.hpp"
#include "list_node.hpp"
#include "student.hpp"
template <typename T>
LinkedList<T>::LinkedList() {
head_ = nullptr;
tail_ = nullptr;
size_ = 0;
}
template <typename T>
LinkedList<T>::LinkedList(T value) {
tail_ = nullptr;
head_ = new ListNode<T>{ value, tail_ };
size_ = 1;
}
// template <typename T>
// LinkedList<T>::~LinkedList() {}
template <typename T>
ListNode<T>* LinkedList<T>::getHead() {
return head_;
}
template <typename T>
ListNode<T>* LinkedList<T>::getTail() {
return tail_;
}
template <typename T>
int LinkedList<T>::getSize() {
return size_;
}
template <typename T>
bool LinkedList<T>::exists(T value) {
ListNode<T>* current = head_;
while(current != nullptr) {
if(current->value == value)
return true;
current = current->next;
}
return false;
}
template <typename T>
void LinkedList<T>::add(T value) {
if(head_ == nullptr)
head_ = new ListNode<T>{ value, tail_ };
// tail = nullptr in this case
else if(size_ == 1) {
// Set tail to new value
tail_ = new ListNode<T>{ value, nullptr };
// Make head => tail (forward link)
head_->next = tail_;
}
else {
// Make a new node with the passed value that is forward linked to nullptr
ListNode<T>* neo = new ListNode<T>{ value, nullptr };
// Make tail => neo
tail_->next = neo;
// Make neo the new tail
tail_ = neo;
}
size_++;
}
template <typename T>
void LinkedList<T>::remove() {
ListNode<T>* oldTail = tail_;
ListNode<T>* previous = head_;
while(previous->next != tail_)
previous = previous->next;
previous->next = nullptr;
delete oldTail;
size_--;
}
template <typename T>
void LinkedList<T>::remove(T value) {
if(head_->value == value) {
ListNode<T>* oldHead = head_;
head_ = head_->next;
delete oldHead;
size_--;
return;
}
ListNode<T>* previous = head_;
ListNode<T>* current = head_->next;
while(current != nullptr) {
if(current->value == value) {
// Make previous's next, current's next
previous->next = current->next;
// Delete the removed node
delete current;
size_--;
}
current = current->next;
}
}
template <typename T>
std::string LinkedList<T>::toString() {
ListNode<T>* current = head_;
std::string output = "";
while(current != nullptr) {
if(current->next != nullptr) {
// If type is Student
if constexpr (std::is_same<T, Student>::value)
output += current->value.toString(false) + " => ";
// If type is std::string
else if constexpr (std::is_same<T, std::string>::value)
output += current->value + " => ";
// Otherwise, it's a primitive
else
output += std::to_string(current->value) + " => ";
}
else {
// If type is Student
if constexpr (std::is_same<T, Student>::value)
output += current->value.toString(false);
// If type is std::string
else if constexpr (std::is_same<T, std::string>::value)
output += current->value;
// Otherwise, it's a primitive
else
output += std::to_string(current->value);
}
current = current->next;
}
return output;
}
// Relevant Templates
template class LinkedList<int>;
template class LinkedList<char>;
template class LinkedList<bool>;
template class LinkedList<float>;
template class LinkedList<double>;
template class LinkedList<wchar_t>;
template class LinkedList<std::string>;
template class LinkedList<Student>;