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
Diff: Core/LinkedList.h
- Revision:
- 12:63db16fea709
- Child:
- 15:e69fd74d42e4
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Core/LinkedList.h Sun May 08 14:42:08 2016 +0000
@@ -0,0 +1,143 @@
+#ifndef SIMPLEGUI_LINKED_LIST_H
+#define SIMPLEGUI_LINKED_LIST_H
+
+#include "mbed.h"
+
+template<class T>
+class LinkedListNode
+{
+
+public:
+
+ LinkedListNode(T* nodeData) : data(nodeData), next(NULL) {}
+ ~LinkedListNode() {}
+
+ T* data;
+ LinkedListNode<T>* next;
+};
+
+template<class T>
+class LinkedList
+{
+
+public:
+
+ LinkedList() : _first(NULL), _next(NULL), _current(NULL) {}
+ ~LinkedList() {}
+
+
+ void append(T* data) {
+
+ if(_first == NULL) {
+
+ _first = new LinkedListNode<T>(data);
+
+ } else {
+
+ LinkedListNode<T>* p = _first;
+ LinkedListNode<T>* d = new LinkedListNode<T>(data);
+
+ while(p->next != NULL) {
+ p = p->next;
+ }
+
+ p->next = d;
+ }
+ }
+
+ void appendOnce(T* data) {
+
+ if(_first == NULL) {
+
+ _first = new LinkedListNode<T>(data);
+
+ } else {
+
+ LinkedListNode<T>* p = _first;
+ LinkedListNode<T>* d = new LinkedListNode<T>(data);
+
+ while(p->next != NULL) {
+
+ if(p->data == data) {
+ return;
+ }
+
+ p = p->next;
+ }
+
+ p->next = d;
+ }
+ }
+ void remove(T* data) {
+
+ if(_first == NULL) {
+ return;
+ }
+
+ LinkedListNode<T>* next = _first;
+ LinkedListNode<T>* prev = _first;
+
+ if(_first->data == data) {
+ _first = _first->next;
+ delete next;
+ } else {
+ next = _first->next;
+ while(next) {
+ if(next->data == data) {
+ prev->next = next->next;
+ delete next;
+ return;
+ }
+ prev = next;
+ next = next->next;
+ }
+ }
+ }
+
+ void clear() {
+ LinkedListNode<T>* here = _first;
+ LinkedListNode<T>* next;
+ while(here != NULL) {
+ next = here->next;
+ delete here;
+ here = next;
+ }
+ _first = NULL;
+ }
+
+ void reset() {
+ _current = _first;
+ }
+
+ T* next() {
+
+ LinkedListNode<T>* p = _current;
+ if(p != NULL) {
+ _current = _current->next;
+ return p->data;
+ }
+
+ return NULL;
+ }
+
+ bool contains(T *thing) {
+ LinkedListNode<T>* here = _first;
+ while(here != NULL) {
+ if(here->data == thing) {
+ return true;
+ }
+ here = here->next;
+ }
+ return false;
+ }
+
+
+protected:
+
+ LinkedListNode<T>* _first;
+ LinkedListNode<T>* _next;
+ LinkedListNode<T>* _current;
+
+};
+
+#endif
\ No newline at end of file
