forked from CSUF-CPSC-131-Fall2019/Data-Structures-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSinglyLinkedList.hpp
More file actions
175 lines (139 loc) · 4.16 KB
/
SinglyLinkedList.hpp
File metadata and controls
175 lines (139 loc) · 4.16 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#pragma once
#include <stdexcept>
template<typename T>
struct Node {
T data;
Node<T>* next;
Node() = delete; // Intentionally no default constructor
Node( const T & element ) : data( element ), next( nullptr ) {}
};
template<typename T>
class SinglyLinkedList {
private:
Node<T>* head;
Node<T>* tail;
public:
// Constructors
SinglyLinkedList();
SinglyLinkedList(const SinglyLinkedList&);
SinglyLinkedList& operator=(const SinglyLinkedList&); // assignment operator
~SinglyLinkedList(); // destructor
// Getters / Setters
bool empty();
int size() = delete; // INTENTIONALLY NOT IMPLEMENTED !!
void append( const T& );
void prepend( const T& );
void insertAfter( Node<T>*, const T& );
void removeAfter( Node<T>* );
void pop_front(); // remove element at front of list
T& front(); // return list's front element
T& back(); // return list's back element
void clear();
};
template<typename T>
SinglyLinkedList<T>::SinglyLinkedList() : head( nullptr ), tail( nullptr ) {}
template<typename T>
bool SinglyLinkedList<T>::empty() {
return head == nullptr;
}
template<typename T>
void SinglyLinkedList<T>::append( const T& newData ) {
Node<T> * newNode = new Node<T>( newData ); // create new node
if (head == nullptr) { // List empty
head = newNode;
tail = newNode;
}
else{
tail->next = newNode;
tail = newNode;
}
}
template<typename T>
void SinglyLinkedList<T>::prepend( const T& newData ) {
Node<T> * newNode = new Node<T>( newData ); // create new node
if (head == nullptr) { // list empty
head = newNode;
tail = newNode;
}
else {
newNode->next = head;
head = newNode;
}
}
template<typename T>
void SinglyLinkedList<T>::insertAfter(Node<T>* curNode, const T& newData) {
// Construct new node
Node<T>* newNode = new Node<T>( newData );
if (head == nullptr) { // List empty
head = newNode;
tail = newNode;
} else if (curNode == tail) { // Insert after tail
tail->next = newNode;
tail = newNode;
} else {
newNode->next = curNode->next;
curNode->next = newNode;
}
}
template<typename T>
void SinglyLinkedList<T>::removeAfter(Node<T>* curNode) {
if( empty() ) throw std::length_error( "empty list" );
// Special case, remove head
if (curNode == nullptr && head != nullptr) {
Node<T> *sucNode = head->next;
head = sucNode;
if (sucNode == nullptr) { // Removed last item
tail = nullptr;
}
}
else if (curNode->next != nullptr) {
Node<T> *sucNode = curNode->next->next;
curNode->next = sucNode;
if (sucNode == nullptr) { // Removed tail
tail = curNode;
}
}
}
template <typename T>
void SinglyLinkedList<T>::pop_front() {
removeAfter(nullptr);
}
template <typename T>
T& SinglyLinkedList<T>::front() {
if( empty() ) throw std::length_error( "empty list" );
return head->data;
}
template <typename T>
T& SinglyLinkedList<T>::back() {
if( empty() ) throw std::length_error( "empty list" );
return tail->data;
}
template<typename T>
void SinglyLinkedList<T>::clear() {
while( !empty() )
pop_front();
}
template<typename T>
SinglyLinkedList<T>::~SinglyLinkedList() {
clear();
}
template <typename T>
SinglyLinkedList<T>::SinglyLinkedList( const SinglyLinkedList<T> & original ) : SinglyLinkedList() {
// Walk the original list adding copies of the elements to this list maintaining order
for( Node<T> * position = original.head; position != nullptr; position = position->next ) {
append( position->data );
}
}
template <typename T>
SinglyLinkedList<T> & SinglyLinkedList<T>::operator=( const SinglyLinkedList<T> & rhs ) {
if( this != &rhs ) // avoid self assignment
{
// Release the contents of this list first
clear(); // An optimization might be possible by reusing already allocated nodes
// Walk the right hand side list adding copies of the elements to this list maintaining order
for( Node<T> * position = rhs.head; position != nullptr; position = position->next ) {
append( position->data );
}
}
return *this;
}