72 void copy_all_nodes_from_list(
const list& other);
76 void insert(int32_t new_elem);
81 int32_t
traverse(int32_t index)
const;
103 Node* temp =
nullptr;
105 new_node->
next =
nullptr;
110 while (temp->
next !=
nullptr) {
113 temp->
next = new_node;
115 }
catch (std::bad_alloc& exception) {
116 std::cerr <<
"bad_alloc detected: " << exception.what() <<
"\n";
127 Node* prev =
nullptr;
128 Node* next_node =
nullptr;
129 while (curr !=
nullptr) {
130 next_node = curr->
next;
146 throw std::logic_error(
"List is empty");
156 while (t->
next !=
nullptr) {
161 throw std::logic_error(
"List is empty");
169 Node* current = head;
172 while (current !=
nullptr) {
173 if (count == index) {
174 return (current->val);
177 current = current->
next;
189 while (head !=
nullptr) {
190 const auto tmp_node = head->
next;
198void list::copy_all_nodes_from_list(
const list& other) {
200 head = copy_all_nodes(other.head);
212 if (
this == &other) {
217 copy_all_nodes_from_list(other);
238 assert(L.
top() == 11);
239 assert(L.
last() == 18);
242 assert(L.
top() == 18);
248 assert(L.
last() == 11);
249 std::cout <<
"All tests have successfully passed!" << std::endl;
252void test_copy_constructor() {
258 otherList.insert(40);
262 assert(L.
top() == 10);
263 assert(otherList.top() == 10);
265 assert(otherList.traverse(1) == 20);
268 assert(otherList.traverse(2) == 30);
270 assert(L.
last() == 400);
271 assert(otherList.last() == 40);
274void test_assignment_operator() {
285 assert(L.
top() == 10);
286 assert(otherList.
top() == 10);
288 assert(otherList.
traverse(1) == 20);
291 assert(otherList.
traverse(2) == 30);
293 assert(L.
last() == 400);
294 assert(otherList.
last() == 40);
303 test_copy_constructor();
304 test_assignment_operator();
Node * next
value of the current link
int32_t traverse(int32_t index) const
Utility function to find the i th element of the list.
int32_t top() const
Utility function to find the top element of the list.
void insert(int32_t new_elem)
Utility function that adds a new element at the end of the list.
void reverseList()
Utility function for reversing a list.
void delete_all_nodes()
calls delete operator on every node in the represented list
list & operator=(const list &other)
assignment operator creating a deep copy of every node of the input
std::shared_ptr< link > last
last link on the list
Functions for singly linked list algorithm.
Node * copy_all_nodes(const Node *const node)
creates a deep copy of a list starting at the input node
static void test()
Self-test implementations.