Brumski's DSA Learning Project 1.3
For Learning Data Structures and Algorithms in C++
Loading...
Searching...
No Matches
linkedlist.hpp
1#pragma once
2
3#include <iostream>
4#include <memory>
5
11template<class LLType>
13private:
17 template<class NType>
18 class Node {
19 public:
21 std::unique_ptr<Node<NType>> next = nullptr;
26 Node() = default;
27 };
28private:
29 std::unique_ptr<Node<LLType>> head;
30
31public:
35 LinkedList() = default;
36
41
46 LinkedList(LinkedList<LLType>&& other) noexcept = default;
47
53
58 virtual LinkedList<LLType>& operator=(LinkedList<LLType>&& other) noexcept = default;
59
63 virtual ~LinkedList() = default;
64
65public:
70 virtual LinkedList& insertAtFront(const LLType& value) {
71 std::unique_ptr<Node<LLType>> newNode = std::make_unique<Node<LLType>>();
72 newNode->data = value;
73 newNode->next = std::move(head);
74
75 head = std::move(newNode);
76 return *this;
77 }
78
83 virtual LinkedList& insertAtEnd(const LLType& value) {
84 std::unique_ptr<Node<LLType>> newNode = std::make_unique<Node<LLType>>();
85 newNode->data = value;
86 newNode->next = nullptr;
87
88 if (!head) {
89 head = std::move(newNode);
90 return *this;
91 }
92
93 Node<LLType>* lastNode = head.get();
94 while (lastNode->next != nullptr) {
95 lastNode = lastNode->next.get();
96 }
97 lastNode->next = std::move(newNode);
98 return *this;
99 }
100
104 friend std::ostream& operator<<(std::ostream& os, const LinkedList<LLType>& obj) {
105 Node<LLType>* PRINT_node = obj.head.get();
106 if (!obj.head) return (os << "[ ]");
107
108 os << "[";
109 while (PRINT_node->next != nullptr) {
110 std::cout << PRINT_node->data <<", ";
111 PRINT_node = PRINT_node->next.get();
112 }
113 os << PRINT_node->data;
114 PRINT_node = PRINT_node->next.get();
115 os << "]";
116
117 return os;
118 }
119};
120
121
125void ListExample();
126
130void ListExample2();
This is a template class represents a single element of the linked list, and it will be called "Node"...
Definition linkedlist.hpp:18
std::unique_ptr< Node< NType > > next
Stores the address of the next node.
Definition linkedlist.hpp:21
Node()=default
Default Constructor.
NType data
Stores the data for each node.
Definition linkedlist.hpp:20
This is a template class that represents the linked list.
Definition linkedlist.hpp:12
virtual LinkedList & insertAtFront(const LLType &value)
Inserts a value at the front of the linked list.
Definition linkedlist.hpp:70
LinkedList(LinkedList< LLType > &&other) noexcept=default
Default Move constructor.
virtual LinkedList< LLType > & operator=(LinkedList< LLType > &&other) noexcept=default
Move assignment operator.
friend std::ostream & operator<<(std::ostream &os, const LinkedList< LLType > &obj)
Outputs the values in the linked list.
Definition linkedlist.hpp:104
LinkedList()=default
Default Constructor.
virtual LinkedList< LLType > & operator=(const LinkedList< LLType > &)=delete
Default Copy assignment operator Prevents assigning the values of existing LinkedList<T> objects to o...
LinkedList(const LinkedList< LLType > &)=delete
Deleted copy constructor.
virtual LinkedList & insertAtEnd(const LLType &value)
Inserts a value at the end of the linked list.
Definition linkedlist.hpp:83
virtual ~LinkedList()=default
Default Destructor.