-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.cpp
More file actions
200 lines (147 loc) · 4.21 KB
/
Copy pathLinkedList.cpp
File metadata and controls
200 lines (147 loc) · 4.21 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/** @file
*
* @course CS1521
* @section 1
* @term Spring 2023
*
* Implementation file for a pointer-based implementation of the ADT
* list that uses smart pointers.
*
* Adapted from pages 298-300 in Carrano 7e.
*
* @author Frank M. Carrano
* @author Timothy Henry
* @author Steve Holtz
*
* @date 14 Mar 2023
*
* @version 7.0 */
//#define NDEBUG
#include <string>
#include <new>
#include <memory>
#include <cassert>
#include "PrecondViolatedExcep.h"
#include "LinkedList.h"
template <typename ItemType>
LinkedList<ItemType>::LinkedList(const LinkedList<ItemType>& aList) {
itemCount = aList.itemCount;
if (aList.headPtr) {
auto aListPtr(aList.headPtr);
try {
headPtr = std::make_shared<Node<ItemType>>(aListPtr->getItem() );
auto thisListPtr(headPtr);
aListPtr = aListPtr->getNext();
while (aListPtr) {
thisListPtr->setNext(
std::make_shared<Node<ItemType>>(aListPtr->getItem())
);
thisListPtr = thisListPtr->getNext();
aListPtr = aListPtr->getNext();
}
}
catch (const std::bad_alloc&) {
clear();
throw;
}
}
}
#ifdef DTOR_TEST
#include <iostream>
template <typename ItemType>
LinkedList<ItemType>::~LinkedList() {
std::cerr << "LinkedList destructor called on:\n"
<< "\theadPtr: "
<< headPtr
<< "\n"
<< "\titemCount: "
<< itemCount
<< std::endl;
}
#endif
template <typename ItemType>
bool LinkedList<ItemType>::isEmpty() const {
return !itemCount;
}
template <typename ItemType>
int LinkedList<ItemType>::getLength() const {
return itemCount;
}
template <typename ItemType>
bool LinkedList<ItemType>::insert(int newPosition,
const ItemType& newEntry) {
if (newPosition >= 1 && newPosition <= itemCount + 1) {
try {
if (newPosition == 1) {
headPtr = std::make_shared<Node<ItemType>>(newEntry,
headPtr);
}
else {
auto prevPtr(getNodeAt(newPosition - 1) );
prevPtr->setNext(
std::make_shared<Node<ItemType>>(newEntry,
prevPtr->getNext())
);
}
++itemCount;
return true;
}
catch (const std::bad_alloc&) {
return false;
}
}
return false;
}
template <typename ItemType>
bool LinkedList<ItemType>::remove(int position) {
if (position >= 1 && position <= itemCount) {
if (position == 1) {
headPtr = headPtr->getNext();
}
else {
auto prevPtr(getNodeAt(position - 1) );
auto curPtr(prevPtr->getNext() );
prevPtr->setNext(curPtr->getNext() );
}
--itemCount;
return true;
}
return false;
}
template <typename ItemType>
void LinkedList<ItemType>::clear() {
headPtr = nullptr; // or headPtr.reset();
itemCount = 0;
}
template <typename ItemType>
ItemType LinkedList<ItemType>::getEntry(int position) const {
if (position >= 1 && position <= itemCount) {
auto nodePtr(getNodeAt(position) );
return nodePtr->getItem();
}
std::string message("LinkedList::getEntry() called with an empty ");
message += "list or an invalid position.";
throw PrecondViolatedExcep(message);
}
template <typename ItemType>
ItemType LinkedList<ItemType>::replace(int position,
const ItemType& newEntry) {
if (position >= 1 && position <= itemCount) {
auto nodePtr(getNodeAt(position) );
ItemType oldEntry(nodePtr->getItem() );
nodePtr->setItem(newEntry);
return oldEntry;
}
std::string message("LinkedList::setEntry() called with an empty ");
message += "list or an invalid position.";
throw PrecondViolatedExcep(message);
}
template <typename ItemType>
auto LinkedList<ItemType>::getNodeAt(int position) const {
assert(position >= 1 && position <= itemCount);
auto curPtr(headPtr);
for (int skip(1); skip < position; ++skip) {
curPtr = curPtr->getNext();
}
return curPtr;
}