TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
circular_linked_list.cpp
Go to the documentation of this file.
1
12#include <cassert>
13#include <iostream>
14#include <vector>
15
21
27namespace circular_linked_list {
28
32struct Node {
33 int64_t data;
39 explicit Node(int64_t _data) {
40 data = _data;
41 next = nullptr;
42 }
48 explicit Node(int64_t _data, Node* _next) {
49 data = _data;
50 next = _next;
51 }
52};
53
58 private:
60 Node* end{};
61
62 public:
67 root = nullptr;
68 end = nullptr;
69 }
74 erase();
75 root = nullptr;
76 Node* node = copy.root;
77 while (node != nullptr) {
78 insert(node->data);
79 node = node->next;
80 }
81 }
87 root = source.root;
88 end = source.end;
89 source.root = nullptr;
90 source.end = nullptr;
91 }
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 }
113 root = other.root;
114 end = other.end;
115 other.root = nullptr;
116 other.end = nullptr;
117 return *this;
118 }
126 void erase() {
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 }
146 void insert(const std::vector<int64_t>& values) {
147 for (int64_t value : values) {
148 insert(value);
149 }
150 }
158 void insert(int64_t data) {
159 Node* node = new Node(data, root);
160 insert(node);
161 }
169 void insert(Node* node) {
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 }
187 void print() { print(root); }
196 void print(Node* root) {
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 }
214 std::vector<int64_t> values() { return values(root); }
223 std::vector<int64_t> values(Node* root) {
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 }
235};
236
237} // namespace circular_linked_list
238
239} // namespace operations_on_datastructures
240
245namespace tests {
252void test1() {
253 std::cout << "TEST CASE 1\n";
254 std::cout << "Intialized a = {2}\n";
255 std::cout << "Expected result: {2}\n";
256 CircularLinkedList a;
257 std::vector<int64_t> res = {2};
258 a.insert(2);
259 assert(a.values() == res);
260 a.print();
261 std::cout << "TEST PASSED!\n\n";
262}
267void test2() {
268 std::cout << "TEST CASE 2\n";
269 std::cout << "Intialized a = {2, 5, 6}\n";
270 std::cout << "Expected result: {2, 5, 6}\n";
271 CircularLinkedList a;
272 std::vector<int64_t> res = {2, 5, 6};
273 a.insert(2);
274 a.insert(5);
275 a.insert(6);
276 assert(a.values() == res);
277 a.print();
278 std::cout << "TEST PASSED!\n\n";
279}
284void test3() {
285 std::cout << "TEST CASE 3\n";
286 std::cout << "Intialized a = {2, 7, 8, 3, 2, 6}\n";
287 std::cout << "Expected result: {2, 7, 8, 3, 2, 6}\n";
288 CircularLinkedList a;
289 std::vector<int64_t> res = {2, 7, 8, 3, 2, 6};
290 a.insert({2, 7, 8, 3, 2, 6});
291 a.print();
292 assert(a.values() == res);
293 std::cout << "TEST PASSED!\n\n";
294}
299void test4() {
300 std::cout << "TEST CASE 4\n";
301 std::cout << "Intialized a = {2, 5}\n";
302 std::cout << "Expected result: {5, 2}\n";
303 CircularLinkedList a;
304 std::vector<int64_t> res = {5, 2};
305 a.insert(2);
306 Node* start = new Node(5);
307 a.insert(start);
308 assert(a.values(start) == res);
309 a.print(start);
310 std::cout << "TEST PASSED!\n\n";
311}
312
317void test5() {
318 std::cout << "TEST CASE 5\n";
319 std::cout << "Intialized a = {}\n";
320 std::cout << "Expected result: Empty List!\n";
321 CircularLinkedList a;
322 std::vector<int64_t> res = {};
323 assert(a.values() == res);
324 a.print();
325 std::cout << "TEST PASSED!\n\n";
326}
327} // namespace tests
328
333static void test() {
334 tests::test1();
335 tests::test2();
336 tests::test3();
337 tests::test4();
338 tests::test5();
339}
340
345int main() {
346 test(); // run self-test implementations
347 return 0;
348}
struct node { int data; int height; struct node *left; struct node *right;} node
for std::queue
Definition avltree.cpp:13
static void test()
Function to test the correctness of the Circular Linked List.
int main()
main function
std::vector< int64_t > values(Node *root)
Returns a std::vector of the values of the Circular Linked List, beginning from a given Node.
CircularLinkedList(const CircularLinkedList &copy)
Copy constructor for CircularLinkedList.
void insert(Node *node)
Inserts a given Node into the Circular Linked List.
void insert(int64_t data)
Inserts a single value into the Circular Linked List.
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.
CircularLinkedList & operator=(CircularLinkedList &&other) noexcept
Move assignment operator.
CircularLinkedList & operator=(const CircularLinkedList &other)
Copy assignment operator.
CircularLinkedList(CircularLinkedList &&source) noexcept
Move constructor for CircularLinkedList.
void print()
Prints the values of the Circular Linked List, beginning from the root Node.
void insert(const std::vector< int64_t > &values)
Inserts all the values from a vector into the Circular Linked List.
int data[MAX]
test data
Functions for the Circular Linked List implementation.
Testcases to check Union of Two Arrays.
void test1()
A Test to check an simple case.
void test4()
A Test to check a very large input.
void test3()
A Test to check an invalid shift value.
void test2()
A Test to check an empty vector.
void test5()
A Test to check a shift of zero.
A Node struct that represents a single Node in a Binary Tree.
Node(int64_t _data)
Creates a new Node with some initial data.
Node(int64_t _data, Node *_next)
Creates a new Node with initial data and a successor.