TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
operations_on_datastructures::circular_linked_list::CircularLinkedList Class Reference

A class that implements a Circular Linked List. More...

Collaboration diagram for operations_on_datastructures::circular_linked_list::CircularLinkedList:
[legend]

Public Member Functions

 CircularLinkedList ()
 Creates an empty CircularLinkedList.
 
 CircularLinkedList (const CircularLinkedList &copy)
 Copy constructor for CircularLinkedList.
 
 CircularLinkedList (CircularLinkedList &&source) noexcept
 Move constructor for CircularLinkedList.
 
CircularLinkedListoperator= (const CircularLinkedList &other)
 Copy assignment operator.
 
CircularLinkedListoperator= (CircularLinkedList &&other) noexcept
 Move assignment operator.
 
 ~CircularLinkedList ()
 Cleans up memory when destroyed.
 
void erase ()
 
void insert (const std::vector< int64_t > &values)
 Inserts all the values from a vector into the Circular Linked List.
 
void insert (int64_t data)
 Inserts a single value into the Circular Linked List.
 
void insert (Node *node)
 Inserts a given Node into the Circular Linked List.
 
void print ()
 Prints the values of the Circular Linked List, beginning from the root Node.
 
void print (Node *root)
 Prints the values of the Circular Linked List, beginning from a given Node to be used as the root.
 
std::vector< int64_t > values ()
 Returns a std::vector of the values of the Circular Linked List.
 
std::vector< int64_t > values (Node *root)
 Returns a std::vector of the values of the Circular Linked List, beginning from a given Node.
 

Private Attributes

Noderoot
 Pointer to the root Node.
 
Nodeend {}
 Pointer to the last Node.
 

Detailed Description

A class that implements a Circular Linked List.

Definition at line 57 of file circular_linked_list.cpp.

Constructor & Destructor Documentation

◆ CircularLinkedList() [1/3]

operations_on_datastructures::circular_linked_list::CircularLinkedList::CircularLinkedList ( )
inline

Creates an empty CircularLinkedList.

Definition at line 66 of file circular_linked_list.cpp.

◆ CircularLinkedList() [2/3]

operations_on_datastructures::circular_linked_list::CircularLinkedList::CircularLinkedList ( const CircularLinkedList & copy)
inline

Copy constructor for CircularLinkedList.

Definition at line 73 of file circular_linked_list.cpp.

73 {
74 erase();
75 root = nullptr;
76 Node* node = copy.root;
77 while (node != nullptr) {
78 insert(node->data);
79 node = node->next;
80 }
81 }
struct node { int data; int height; struct node *left; struct node *right;} node
for std::queue
Definition avltree.cpp:13
void insert(const std::vector< int64_t > &values)
Inserts all the values from a vector into the Circular Linked List.
Node * root
Pointer to the root Node.

◆ CircularLinkedList() [3/3]

operations_on_datastructures::circular_linked_list::CircularLinkedList::CircularLinkedList ( CircularLinkedList && source)
inlinenoexcept

Move constructor for CircularLinkedList.

Parameters
sourcervalue reference to a Circular Linked List

Definition at line 86 of file circular_linked_list.cpp.

86 {
87 root = source.root;
88 end = source.end;
89 source.root = nullptr;
90 source.end = nullptr;
91 }
Node * end
Pointer to the last Node.

◆ ~CircularLinkedList()

operations_on_datastructures::circular_linked_list::CircularLinkedList::~CircularLinkedList ( )
inline

Cleans up memory when destroyed.

Definition at line 122 of file circular_linked_list.cpp.

122{ erase(); }

Member Function Documentation

◆ erase()

void operations_on_datastructures::circular_linked_list::CircularLinkedList::erase ( )
inline

Iteratively frees each node in the Circular Linked List from the heap

Definition at line 126 of file circular_linked_list.cpp.

126 {
127 if (root == nullptr) {
128 return;
129 }
130 Node* node = root;
131 do {
132 Node* temp = node;
133 node = node->next;
134 delete (temp);
135 } while (node != root);
136 root = nullptr;
137 end = nullptr;
138 }

◆ insert() [1/3]

void operations_on_datastructures::circular_linked_list::CircularLinkedList::insert ( const std::vector< int64_t > & values)
inline

Inserts all the values from a vector into the Circular Linked List.

Goes through each element in the vector sequentially, inserting it into the list

Parameters
valuesThe vector of integer values that is to be inserted
Returns
void

Definition at line 146 of file circular_linked_list.cpp.

146 {
147 for (int64_t value : values) {
148 insert(value);
149 }
150 }
std::vector< int64_t > values()
Returns a std::vector of the values of the Circular Linked List.

◆ insert() [2/3]

void operations_on_datastructures::circular_linked_list::CircularLinkedList::insert ( int64_t data)
inline

Inserts a single value into the Circular Linked List.

Creates a Node with the given value, pointing to the root Node and inserts it into the list

Parameters
dataThe integer valus to be inserted
Returns
void

Definition at line 158 of file circular_linked_list.cpp.

158 {
159 Node* node = new Node(data, root);
160 insert(node);
161 }
int data[MAX]
test data

◆ insert() [3/3]

void operations_on_datastructures::circular_linked_list::CircularLinkedList::insert ( Node * node)
inline

Inserts a given Node into the Circular Linked List.

Checks wheter the list is empty, and inserts the Node, modifying the end pointer

Parameters
nodeThe Node that is to be inserted
Returns
void

< Set node as the root

< Point node to itself

< Set the end to the root

< Append node to the end

< Set the next value to the root

< Make end point to node

Definition at line 169 of file circular_linked_list.cpp.

169 {
170 if (root == nullptr) {
171 root = node;
172 node->next = root;
173 end = root;
174 } else {
175 end->next = node;
176 node->next = root;
177 end = node;
178 }
179 }

◆ operator=() [1/2]

CircularLinkedList & operations_on_datastructures::circular_linked_list::CircularLinkedList::operator= ( CircularLinkedList && other)
inlinenoexcept

Move assignment operator.

Parameters
otherrvalue reference to a Circular Linked List
Returns
Reference to CircularLinkedList

Definition at line 112 of file circular_linked_list.cpp.

112 {
113 root = other.root;
114 end = other.end;
115 other.root = nullptr;
116 other.end = nullptr;
117 return *this;
118 }

◆ operator=() [2/2]

CircularLinkedList & operations_on_datastructures::circular_linked_list::CircularLinkedList::operator= ( const CircularLinkedList & other)
inline

Copy assignment operator.

Parameters
otherReference to a Circular Linked List
Returns
Reference to CircularLinkedList

Definition at line 97 of file circular_linked_list.cpp.

97 {
98 erase();
99 root = nullptr;
100 Node* node = other.root;
101 while (node != nullptr) {
102 insert(node->data);
103 node = node->next;
104 }
105 return *this;
106 }

◆ print() [1/2]

void operations_on_datastructures::circular_linked_list::CircularLinkedList::print ( )
inline

Prints the values of the Circular Linked List, beginning from the root Node.

Goes through each Node from the root and prints them out in order

Returns
void

Definition at line 187 of file circular_linked_list.cpp.

187{ print(root); }
void print()
Prints the values of the Circular Linked List, beginning from the root Node.

◆ print() [2/2]

void operations_on_datastructures::circular_linked_list::CircularLinkedList::print ( Node * root)
inline

Prints the values of the Circular Linked List, beginning from a given Node to be used as the root.

Goes through each Node from the given Node and prints them out in order. If the list is empty, it prints the message 'Empty List!'

Parameters
rootThe Node to start at
Returns
void

Definition at line 196 of file circular_linked_list.cpp.

196 {
197 Node* temp = root;
198 if (root == nullptr) {
199 std::cout << "Empty List!\n";
200 return;
201 }
202 do {
203 std::cout << temp->data << " ";
204 temp = temp->next;
205 } while (temp != root);
206 std::cout << "\n";
207 }

◆ values() [1/2]

std::vector< int64_t > operations_on_datastructures::circular_linked_list::CircularLinkedList::values ( )
inline

Returns a std::vector of the values of the Circular Linked List.

Starting from the root Node, appends each value of the list to a std::vector and returns it

Returns
A std::vector of the list's values

Definition at line 214 of file circular_linked_list.cpp.

214{ return values(root); }

◆ values() [2/2]

std::vector< int64_t > operations_on_datastructures::circular_linked_list::CircularLinkedList::values ( Node * root)
inline

Returns a std::vector of the values of the Circular Linked List, beginning from a given Node.

Starting from a given Node, appends each value of the list to a std::vector and returns it

Parameters
rootThe Node to start at
Returns
A std::vector of the list's values

< Return empty vector

Definition at line 223 of file circular_linked_list.cpp.

223 {
224 std::vector<int64_t> res;
225 if (root == nullptr) {
226 return res;
227 }
228 Node* temp = root;
229 do {
230 res.push_back(temp->data);
231 temp = temp->next;
232 } while (temp != root);
233 return res;
234 }

Member Data Documentation

◆ end

Node* operations_on_datastructures::circular_linked_list::CircularLinkedList::end {}
private

Pointer to the last Node.

Definition at line 60 of file circular_linked_list.cpp.

60{};

◆ root

Node* operations_on_datastructures::circular_linked_list::CircularLinkedList::root
private

Pointer to the root Node.

Definition at line 59 of file circular_linked_list.cpp.


The documentation for this class was generated from the following file: