This repository has been archived by the owner on Feb 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RingList.h
90 lines (70 loc) · 2.73 KB
/
RingList.h
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
#pragma once
#include <cassert> // To emergency escape programm.
// Doubly connected ring list class.
class RingList
{
using size_t = unsigned long long;
using value_t = double;
// List node structure.
struct Node
{
// Constructor
Node(const value_t value = 0);
value_t data; // Node data.
Node *next; // Next node pointer.
Node *previous; // Previous node pointer.
};
public:
using Iterator = Node *;
struct Node_Iterator
{
Node_Iterator(Node *node);
Node *current; // Current Node pointer.
// Overloading operators n++, n--, n == o and n != o.
Node_Iterator operator++(int);
Node_Iterator operator--(int);
bool operator==(const Node_Iterator &other);
bool operator!=(const Node_Iterator &other);
};
RingList();
RingList(const size_t size, const value_t value = 0);
explicit RingList(const RingList &other);
~RingList();
// Overloading operator = for lists.
// Assigns new contents to the list, replacing its current
// contents, and modifying its size accordingly.
RingList &operator=(const RingList &other);
Iterator push_back(const value_t value); // Adds a new element at the end.
Iterator push_front(const value_t value); // Inserts a new element at the beginning.
// Inserts new element(-s) before the element at the specified position.
Iterator insert(const size_t position,
const value_t value);
Iterator insert(const size_t position,
const RingList &list);
// Resizes the list so that it contains n elements.
size_t resize(const size_t newSize,
const value_t default_value = 0);
// Removes element(-s) at the specified position (or gap).
void remove(const size_t position);
void remove(const size_t start_pos,
const size_t end_pos);
// Returns the data of the element at position n.
value_t at(const size_t position) const;
// Removes all elements from the list (which are destroyed),
// leaving it with a size of 0.
size_t clear();
size_t size() const; // Returns the number of elements in the list.
Iterator begin() const; // Returns an iterator pointing to the first element in the list.
Iterator end() const; // Returns an iterator pointing to the last element in the list.
private:
size_t m_size; // The number of elements in the list.
Node *head; // List first element pointer aka "head".
// Creates first list element aka "head".
Iterator createHead(const value_t value);
// Returns the reference to the element at position n.
Iterator find(const size_t position) const;
// Clears the memory from the node(-s).
void clearMemory(const Iterator removable);
void clearMemory(const Iterator begin,
const Iterator end);
};