Arduino List Library  3.0.1
The Ultimate Collection of Lists
SingleLinkedList.hpp
Go to the documentation of this file.
1 
26 #ifndef LIST_SINGLE_LINKED_LIST_HPP
27 #define LIST_SINGLE_LINKED_LIST_HPP
28 
29 #include "AbstractList.hpp"
30 
36 template<typename T>
37 class SingleLinkedList : public AbstractList<T> {
41  class Entry : public AbstractList<T>::AbstractEntry {
42  Entry *next = nullptr;
43 
44  public:
48  ~Entry() { next = nullptr; }
49 
55  Entry *getNext() const { return next; };
56 
62  void setNext(Entry *nextEntry) { next = nextEntry; }
63  };
64 
65  Entry *head = nullptr;
66  Entry *tail = nullptr;
67 
68  protected:
72  T *getPointer(int index) override {
73  if (this->isIndexOutOfBounds(index)) {
74  return nullptr;
75  }
76 
77  Entry *current = head;
78  int i = 0;
79  while (i != index) {
80  current = current->getNext();
81  i++;
82  }
83  return current->getValue(this->isMutable());
84  }
85 
86  public:
93  explicit SingleLinkedList<T>(bool mutableList = false)
94  : AbstractList<T>(mutableList) {}
95 
99  ~SingleLinkedList() { this->clear(); }
100 
102 
108  void addAtIndex(int index, T &value) override {
109  // it is allowed, that index == this->getSize() to insert it behind the last
110  // entry
111  if (extendedIsIndexOutOfBounds(index)) {
112  return;
113  }
114 
115  Entry *entry;
116 
117  entry = new Entry();
118  entry->setValue(value, this->isMutable());
119 
120  if (index == 0) {
121  if (this->getSize() == 0) {
122  // Add entry to an empty list
123  tail = entry;
124  } else {
125  // Add entry to a not empty list
126  entry->setNext(head);
127  }
128  head = entry;
129  } else if (index == this->getSize()) {
130  // Add entry at not empty list but at last position
131  tail->setNext(entry);
132  tail = entry;
133  } else {
134  // Add entry to not empty list, somewhere in the middle
135  Entry *current = head;
136  for (int i = 0; i < index - 1; ++i) {
137  current = current->getNext();
138  }
139  entry->setNext(current->getNext());
140  current->setNext(entry);
141  }
142 
143  this->increaseSize();
144  };
145 
149  void clear() override {
150  if (this->getSize() == 0) {
151  return;
152  }
153 
154  Entry *current = head;
155  Entry *next;
156  for (int i = 0; i < this->getSize(); ++i) {
157  next = current->getNext();
158 
159  delete current;
160  current = next;
161  }
162 
163  this->resetSize();
164  head = nullptr;
165  tail = nullptr;
166  }
167 
171  void remove(int index) override {
172  if (this->isIndexOutOfBounds(index)) {
173  return;
174  }
175 
176  Entry *current = head;
177  // current is either the element to delete if index == 0, or the previous
178  // element
179  if (index != 0) {
180  int i = 0;
181  while (i < index - 1) {
182  current = current->getNext();
183  i++;
184  }
185  }
186 
187  if (index == this->getSize() - 1) {
188  tail = current;
189  }
190 
191  Entry *toDelete;
192  if (index == 0) {
193  toDelete = current;
194  head = current->getNext();
195  } else {
196  toDelete = current->getNext();
197  current->setNext(current->getNext()->getNext());
198  }
199 
200  delete toDelete;
201 
202  this->decreaseSize();
203 
204  if (this->getSize() == 0) {
205  head = nullptr;
206  tail = nullptr;
207  }
208  }
209 };
210 
211 #endif// LIST_SINGLE_LINKED_LIST_HPP
void resetSize()
Reset the size to zero.
Definition: AbstractList.hpp:120
int getSize() const
Get the number how many elements are saved in the list.
Definition: AbstractList.hpp:334
~SingleLinkedList()
Destructor of a SingleLinkedList Object.
Definition: SingleLinkedList.hpp:99
void clear() override
Remove all elements from the List.
Definition: SingleLinkedList.hpp:149
#define extendedIsIndexOutOfBounds(index)
Is the list mutable or immutable.
Definition: AbstractList.hpp:44
bool isIndexOutOfBounds(const int index) const
Method to verify if the given index is out of the range of the list size.
Definition: AbstractList.hpp:130
void increaseSize()
Increase the size of the list by one. Should only be called after an insertion!
Definition: AbstractList.hpp:109
T * getPointer(int index) override
The last entry of the list.
Definition: SingleLinkedList.hpp:72
void addAtIndex(int index, T &value) override
Add the value to the list at the given index. The original entry at this index, and followings...
Definition: SingleLinkedList.hpp:108
Definition: AbstractList.hpp:50
Implementation of a single-linked list.
Definition: SingleLinkedList.hpp:37
void decreaseSize()
Decrease the size of the list by one. Should only be called after an deletion!
Definition: AbstractList.hpp:115
bool isMutable() const
Check if the list is mutable.
Definition: AbstractList.hpp:341
Abstract class from which all lists can be derived.
Definition: AbstractList.hpp:37