ESP3D  3.0
Firmware for ESP boards connected to 3D Printer
genLinkedList.h
Go to the documentation of this file.
1 /*
2  genlinkedlist.h - ESP3D simple generic linked list class
3  * inspired by great https://github.com/ivanseidel/LinkedList
4 
5  Copyright (c) 2018 Luc Lebosse. All rights reserved.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 2.1 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21 #ifndef _GENLINKEDLIST_H
22 #define _GENLINKEDLIST_H
23 
24 template<class T> struct GenNode {
25  T data;
28 };
29 
30 template <typename T> class GenLinkedList
31 {
32 public:
33  GenLinkedList();
35  //Clear list
36  void clear();
37  //number of GenNodes
38  size_t count();
39  //add data at the end of list
40  bool push(T data);
41  //get data at end of list and remove from list
42  T pop();
43  //add data at the begining of list
44  bool shift(T data);
45  //get data from begining of list and remove from list
46  T unshift();
47  //get data from index position
48  T get(size_t index);
49  //get head data
50  T getFirst();
51  //get tail data
52  T getLast();
53 
54 private:
55  //number of GenNode
56  size_t _nb;
57  //First GenNode
58  GenNode<T> *_head;
59  //Last GenNode
60  GenNode<T> *_tail;
61 };
62 
63 //Constructor
64 template <typename T>GenLinkedList<T>::GenLinkedList()
65 {
66  _nb = 0;
67  _head = nullptr;
68  _tail = nullptr;
69 }
70 
71 //Destructor
72 template <typename T>GenLinkedList<T>::~GenLinkedList()
73 {
74  clear();
75 }
76 
77 //clear list of all GenNodes
78 template<typename T>void GenLinkedList<T>::clear()
79 {
80  GenNode<T> *current;
81  //delete from first to last
82  while (_head != nullptr) {
83  current = _head;
84  _head = _head->next;
85  current->data = T();
86  delete current;
87  }
88  _tail = _head;
89  _nb = 0;
90 }
91 
92 //Number of GenNodes
93 template<typename T>size_t GenLinkedList<T>::count()
94 {
95  return _nb;
96 }
97 
98 //add to end of list
99 template<typename T> bool GenLinkedList<T>::push (T data)
100 {
101  GenNode<T> *ptr = new GenNode<T>();
102  if (!ptr) {
103  return false;
104  }
105  ptr->data = data;
106  ptr->next = nullptr;
107  ptr->prev = nullptr;
108  _nb++;
109  //Check if already have element in list
110  if (_head) {
111  _tail->next = ptr;
112  ptr->prev = _tail;
113  _tail = ptr;
114  } else { // no element _head = _tail
115  _head = ptr;
116  _tail = _head;
117  }
118  return true;
119 }
120 //take out from end of list
121 template<typename T>T GenLinkedList<T>::pop()
122 {
123  if (_head == nullptr) {
124  return T();
125  }
126  T data = _tail->data;
127 
128  _nb--;
129  if (_head == _tail) {
130  _head->data = T();
131  delete (_head);
132  _head = nullptr;
133  _tail = _head;
134  } else {
135  GenNode<T> *ptr = _tail;
136  _tail = _tail->prev;
137  _tail->next = nullptr;
138  ptr->data = T();
139  delete (ptr);
140  }
141  return data;
142 }
143 
144 //add to head of list
145 template<typename T> bool GenLinkedList<T>::shift (T data)
146 {
147 
148  GenNode<T> *ptr = new GenNode<T>();
149  if (!ptr) {
150  return false;
151  }
152  ptr->data = data;
153  ptr->next = nullptr;
154  ptr->prev = nullptr;
155  _nb++;
156  //Check if already have element in list
157  if (_head) {
158  ptr->next = _head;
159  _head->prev = ptr;
160  _head = ptr;
161  } else { // no element _head = _tail
162  _head = ptr;
163  _tail = _head;
164  }
165  return true;
166 }
167 
168 //take out from Head
169 template<typename T>T GenLinkedList<T>::unshift()
170 {
171  if (_head == nullptr) {
172  return T();
173  }
174  T data = _head->data;
175  _nb--;
176  if (_head == _tail) {
177  _head->data = T();
178  delete (_head);
179  _head = nullptr;
180  _tail = _head;
181  } else {
182  GenNode<T> *ptr = _head;
183  _head = _head->next;
184  _head->prev = nullptr;
185  ptr->data = T();
186  delete (ptr);
187  }
188  return data;
189 }
190 
191 //get data from position (do not remove from list)
192 template<typename T>T GenLinkedList<T>::get(size_t index)
193 {
194  if ((_head == nullptr) || (index > _nb)) {
195  return T();
196  }
197  GenNode<T> *ptr = _head;
198  for (size_t pos = 0; pos < index; pos++) {
199  ptr = ptr->next;
200  }
201  return ptr->data;
202 }
203 
204 
205 //get head data (do not remove from list)
206 template<typename T>T GenLinkedList<T>::getFirst()
207 {
208  if (_head == nullptr) {
209  return T();
210  }
211  return _head->data;
212 }
213 
214 //get tail data (do not remove from list)
215 template<typename T>T GenLinkedList<T>::getLast()
216 {
217  if (_head == nullptr) {
218  return T();
219  }
220  return _tail->data;
221 }
222 
223 #endif //_GENLINKEDLIST_H
GenLinkedList
Definition: genLinkedList.h:30
GenNode::data
T data
Definition: genLinkedList.h:25
GenLinkedList::clear
void clear()
Definition: genLinkedList.h:78
GenLinkedList::getLast
T getLast()
Definition: genLinkedList.h:215
GenNode::next
GenNode< T > * next
Definition: genLinkedList.h:26
GenLinkedList::push
bool push(T data)
Definition: genLinkedList.h:99
GenLinkedList::getFirst
T getFirst()
Definition: genLinkedList.h:206
GenNode
Definition: genLinkedList.h:24
GenLinkedList::GenLinkedList
GenLinkedList()
Definition: genLinkedList.h:64
GenLinkedList::pop
T pop()
Definition: genLinkedList.h:121
GenLinkedList::~GenLinkedList
~GenLinkedList()
Definition: genLinkedList.h:72
GenLinkedList::get
T get(size_t index)
Definition: genLinkedList.h:192
GenLinkedList::count
size_t count()
Definition: genLinkedList.h:93
GenLinkedList::unshift
T unshift()
Definition: genLinkedList.h:169
GenLinkedList::shift
bool shift(T data)
Definition: genLinkedList.h:145
GenNode::prev
GenNode< T > * prev
Definition: genLinkedList.h:27