Skip to content

Commit 7379402

Browse files
authored
add removeFirst() and removeLast() methods (#18)
* add removeFirst() and removeLast() methods * clang
1 parent 93b5a26 commit 7379402

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

examples/List/ManageElements/ManageElements.ino

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,24 @@ void setup() {
3636
Serial.print(list.getSize());
3737
Serial.println(" element(s)");
3838

39+
Serial.println("Adding 10 more elements");
40+
// Add 10 more elements
41+
int y = 0;
42+
for (int i = 0; i < 10; ++i) {
43+
list.add(y);
44+
y++;
45+
}
46+
3947
// Remove element from list
40-
list.remove(0); // With this, you will remove the first element of the list.
41-
Serial.print("After the deletion, the list has: ");
48+
list.remove(3); // With this, you will remove the third element of the list.
49+
Serial.print("After the deletion of the third element, the list has: ");
4250
Serial.print(list.getSize());
4351
Serial.println(" element(s)");
4452

45-
// Add 10 more elements
46-
int e = 0;
47-
for (int i = 0; i < 10; ++i) {
48-
list.add(e);
49-
}
50-
Serial.print("After the insertion, the list has: ");
53+
// Remove the first and the last element
54+
list.removeFirst();
55+
list.removeLast();
56+
Serial.print("After the deletion of the first and the last element, the list has: ");
5157
Serial.print(list.getSize());
5258
Serial.println(" element(s)");
5359

keywords.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ clear KEYWORD2
2424
getValue KEYWORD2
2525
getPointer KEYWORD2
2626
remove KEYWORD2
27+
removeFirst KEYWORD2
28+
removeLast KEYWORD2
2729
removeAll KEYWORD2
2830
getSize KEYWORD2
2931
isMutable KEYWORD2

src/AbstractList.hpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ template <typename T> class AbstractList {
127127
/*!
128128
* @copybrief AbstractList::addLast()
129129
* @note Alias of addLast().
130-
* @see addLast()
130+
* @see addLast()
131131
*
132132
* @param value Value to add.
133133
*/
@@ -167,7 +167,6 @@ template <typename T> class AbstractList {
167167

168168
/*!
169169
* @brief Add all entries from the given list at the end of the list.
170-
* @see addAll()
171170
*
172171
* @param list Other list to copy from.
173172
*/
@@ -182,16 +181,13 @@ template <typename T> class AbstractList {
182181

183182
/*!
184183
* @brief Add a new entry at the end of the list.
185-
* @see add()
186184
*
187185
* @param value Value to add.
188186
*/
189187
void addLast(T &value) { addAtIndex(getSize(), value); }
190188

191189
/*!
192190
* @copydoc AbstractList::get()
193-
* @note Alias of get().
194-
* @see get()
195191
*/
196192
T *getPointer(int index) { return get(index); }
197193

@@ -224,10 +220,20 @@ template <typename T> class AbstractList {
224220
*/
225221
virtual void remove(int index) = 0;
226222

223+
/*!
224+
* @brief Remove the first entry from the list.
225+
*/
226+
virtual void removeFirst() { remove(0); }
227+
228+
/*!
229+
* @brief Remove the las entry from the list.
230+
*/
231+
virtual void removeLast() { remove(getSize() - 1); }
232+
227233
/*!
228234
* @copybrief AbstractList::clear()
229235
* @note Alias of clear().
230-
* @see clear().
236+
* @see clear()
231237
*/
232238
void removeAll() { clear(); }
233239

@@ -250,7 +256,7 @@ template <typename T> class AbstractList {
250256
*
251257
* @return true if the list is empty; false otherwise
252258
*/
253-
bool isEmpty() { return size == 0; }
259+
bool isEmpty() { return getSize() == 0; }
254260

255261
/*!
256262
* @brief Get an array which represent the list.

0 commit comments

Comments
 (0)