Mbed OS Reference
Loading...
Searching...
No Matches
CellularList.h
1/*
2 * Copyright (c) 2017, Arm Limited and affiliates.
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef CELLULAR_LIST_H_
19#define CELLULAR_LIST_H_
20
21#include <stddef.h>
22
23namespace mbed {
24
25/** Class CellularList
26 *
27 * Templated linked list class for common usage.
28 *
29 */
30template <class T> class CellularList {
31private:
32 T *_head, *_tail;
33public:
35 {
36 _head = NULL;
37 _tail = NULL;
38 }
39
41 {
42 T *temp = _head;
43 while (temp) {
44 _head = _head->next;
45 delete temp;
46 temp = _head;
47 }
48 }
49
50 T *add_new()
51 {
52 T *temp = new T;
53 temp->next = NULL;
54 if (_head == NULL) {
55 _head = temp;
56 } else {
57 _tail->next = temp;
58 }
59 _tail = temp;
60
61 return _tail;
62 }
63
64 void delete_last()
65 {
66 T *previous = NULL;
67 T *current = _head;
68
69 if (!current) {
70 return;
71 }
72
73 while (current->next != NULL) {
74 previous = current;
75 current = current->next;
76 }
77
78 if (previous) {
79 _tail = previous;
80 previous->next = NULL;
81 } else {
82 _head = NULL;
83 _tail = NULL;
84 }
85
86 delete current;
87 }
88
89 int count()
90 {
91 T *item = _head;
92 int n = 0;
93 while (item) {
94 item = item->next;
95 n++;
96 }
97 return n;
98 }
99
100 T *dequeue()
101 {
102 if (!_head) {
103 return NULL;
104 }
105 T *temp = _head;
106 _head = _head->next;
107 return temp;
108 }
109
110 void delete_all()
111 {
112 T *temp = _head;
113 while (temp) {
114 _head = _head->next;
115 delete temp;
116 temp = _head;
117 }
118 _tail = NULL;
119 }
120
121
122 T *get_head()
123 {
124 return _head;
125 }
126};
127
128} // namespace mbed
129
130#endif // CELLULAR_LIST_H_
Class CellularList.
Definition: CellularList.h:30