Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of SimpleGUI by
LinkedList.h
00001 #ifndef SIMPLEGUI_LINKED_LIST_H 00002 #define SIMPLEGUI_LINKED_LIST_H 00003 00004 #include "mbed.h" 00005 00006 template<class T> 00007 class LinkedListNode 00008 { 00009 00010 public: 00011 00012 LinkedListNode(T* nodeData) : data(nodeData), next(NULL) {} 00013 ~LinkedListNode() {} 00014 00015 T* data; 00016 LinkedListNode<T>* next; 00017 }; 00018 00019 template<class T> 00020 class LinkedListIterator { 00021 public: 00022 LinkedListIterator(LinkedListNode<T> *first) { 00023 _current = first; 00024 } 00025 00026 ~LinkedListIterator() {} 00027 00028 T* next() { 00029 00030 LinkedListNode<T>* p = _current; 00031 if(p != NULL) { 00032 _current = _current->next; 00033 return p->data; 00034 } 00035 00036 return NULL; 00037 } 00038 00039 private: 00040 LinkedListNode<T>* _current; 00041 }; 00042 00043 template<class T> 00044 class LinkedList 00045 { 00046 00047 public: 00048 00049 LinkedList() : _first(NULL), _next(NULL), _current(NULL), _size(0) {} 00050 ~LinkedList() {} 00051 00052 LinkedListIterator<T> getIterator() { 00053 LinkedListIterator<T> iterator(_first); 00054 return iterator; 00055 } 00056 00057 void append(T* data) { 00058 00059 if(_first == NULL) { 00060 00061 _first = new LinkedListNode<T>(data); 00062 00063 } else { 00064 00065 LinkedListNode<T>* p = _first; 00066 LinkedListNode<T>* d = new LinkedListNode<T>(data); 00067 00068 while(p->next != NULL) { 00069 p = p->next; 00070 } 00071 00072 p->next = d; 00073 } 00074 _size++; 00075 } 00076 00077 void appendOnce(T* data) { 00078 00079 if(_first == NULL) { 00080 00081 _first = new LinkedListNode<T>(data); 00082 _size++; 00083 00084 } else { 00085 00086 LinkedListNode<T>* p = _first; 00087 LinkedListNode<T>* d = new LinkedListNode<T>(data); 00088 00089 while(p->next != NULL) { 00090 00091 if(p->data == data) { 00092 return; 00093 } 00094 00095 p = p->next; 00096 } 00097 00098 p->next = d; 00099 _size++; 00100 } 00101 } 00102 void remove(T* data) { 00103 00104 if(_first == NULL) { 00105 return; 00106 } 00107 00108 LinkedListNode<T>* next = _first; 00109 LinkedListNode<T>* prev = _first; 00110 00111 if(_first->data == data) { 00112 _first = _first->next; 00113 delete next; 00114 _size--; 00115 } else { 00116 next = _first->next; 00117 while(next) { 00118 if(next->data == data) { 00119 prev->next = next->next; 00120 delete next; 00121 _size--; 00122 return; 00123 } 00124 prev = next; 00125 next = next->next; 00126 } 00127 } 00128 } 00129 00130 void clear() { 00131 LinkedListNode<T>* here = _first; 00132 LinkedListNode<T>* next; 00133 while(here != NULL) { 00134 next = here->next; 00135 delete here; 00136 here = next; 00137 } 00138 _first = NULL; 00139 _size = 0; 00140 } 00141 00142 void reset() { 00143 _current = _first; 00144 } 00145 00146 T* next() { 00147 00148 LinkedListNode<T>* p = _current; 00149 if(p != NULL) { 00150 _current = _current->next; 00151 return p->data; 00152 } 00153 00154 return NULL; 00155 } 00156 00157 bool contains(T *thing) { 00158 LinkedListNode<T>* here = _first; 00159 while(here != NULL) { 00160 if(here->data == thing) { 00161 return true; 00162 } 00163 here = here->next; 00164 } 00165 return false; 00166 } 00167 00168 int size() { 00169 return _size; 00170 } 00171 00172 00173 protected: 00174 00175 LinkedListNode<T>* _first; 00176 LinkedListNode<T>* _next; 00177 LinkedListNode<T>* _current; 00178 int _size; 00179 00180 }; 00181 00182 #endif
Generated on Tue Jul 12 2022 22:27:14 by
