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 LinkedList by
LinkedList< T > Class Template Reference
Returns true if data1 shall be inserted before data 2. More...
#include <LinkedList.h>
Public Member Functions | |
| LinkedList () | |
| Create the LinkedList object. | |
| ~LinkedList () | |
| Deconstructor for the LinkedList object Removes any members. | |
| node< T > * | push (T *data) |
| Add a member to the begining of the list. | |
| node< T > * | insertOrdered (T *data, bool(isBigger)(T *data1, T *data2)) |
| Add a member to some position based on sort condition. | |
| node< T > * | append (T *data) |
| Add a member to the end of the list. | |
| node< T > * | remove (uint32_t loc) |
| Remove a member from the list. | |
| node< T > * | pop (uint32_t loc) |
| Get access to a member from the list. | |
| uint32_t | length (void) |
| Get the length of the list. | |
Detailed Description
template<typename T>
class LinkedList< T >
Returns true if data1 shall be inserted before data 2.
Example using the LinkedList Class
#include "mbed.h" #include "LinkedList.h" LinkedList<node>list; int main() { node *tmp; list.push((char *)"Two\n"); list.append((char *)"Three\n"); list.append((char *)"Four\n"); list.push((char*)"One\n"); list.append((char*)"Five\n"); for(int i=1; i<=list.length(); i++) { tmp = list.pop(i); printf("%s", (char *)tmp->data ); } error("done\n"); }
Example using new insertOrdered function:
#include "mbed.h" #include "LinkedList.h" void printList(LinkedList<node> &list, const char* msg) { printf("%s: ", msg); for(int i=1; i<=list.length(); i++) { node *tmp = list.pop(i); printf("%d ", *(int*)tmp->data ); } printf("\n"); } bool ascending(int *n1, int *n2) { int *d1 = (int*)n1; int *d2 = (int*)n2; bool rv = *d1 <= *d2; printf("(%d %d:%d)", *d1, *d2, rv); return rv; } void testAscending() { node<int> *tmp; int n0 = 0; int n1 = 1; int n1B = 1; int n2 = 2; int n3 = 3; int n4 = 4; LinkedList<int>list; tmp = list.insertOrdered(&n2, ascending); if (NULL == tmp) { error("insertOrdered did not insert a node"); } printList(list, "exp 2"); tmp = list.insertOrdered(&n1, ascending); if (NULL == tmp) { error("insertOrdered did not insert a node"); } printList(list, "exp 1,2"); tmp = list.insertOrdered(&n4, ascending); if (NULL == tmp) { error("insertOrdered did not insert a node"); } printList(list, "exp 1,2,4"); tmp = list.insertOrdered(&n3, ascending); if (NULL == tmp) { error("insertOrdered did not insert a node"); } printList(list, "exp 1,2,3,4"); tmp = list.insertOrdered(&n0, ascending); if (NULL == tmp) { error("insertOrdered did not insert a node"); } printList(list, "exp 0,1,2,3,4"); tmp = list.insertOrdered(&n1B, ascending); if (NULL == tmp) { error("insertOrdered did not insert a node"); } printList(list, "exp 0,1,1,2,3,4"); tmp = list.pop(2); if (tmp->data != &n1) { error("pos 2 is not n1\n"); } printf("n1 is good\n"); tmp = list.pop(3); if (tmp->data != &n1B) { error("pos3 is not n1B"); } printf("n1B is good\n"); } bool descending(int *n1, int *n2) { int *d1 = (int*)n1; int *d2 = (int*)n2; bool rv = *d1 <= *d2; printf("(%d %d:%d)", *d1, *d2, rv); return rv; } void testDescending() { node<int> *tmp; int n0 = 0; int n1 = 1; int n1B = 1; int n2 = 2; int n3 = 3; int n4 = 4; LinkedList<int>l; tmp = l.insertOrdered(&n2, descending); if (NULL == tmp) { error("insertOrdered did not insert a node"); } printList(l, "exp 2"); tmp = l.insertOrdered(&n1, descending); if (NULL == tmp) { error("insertOrdered did not insert a node"); } printList(l, "exp 2,1"); tmp = l.insertOrdered(&n4, descending); if (NULL == tmp) { error("insertOrdered did not insert a node"); } printList(l, "exp 4,2,1"); tmp = l.insertOrdered(&n3, descending); if (NULL == tmp) { error("insertOrdered did not insert a node"); } printList(l, "exp 4,3,2,1"); tmp = l.insertOrdered(&n0, descending); if (NULL == tmp) { error("insertOrdered did not insert a node"); } printList(l, "exp 4,3,2,1,0"); tmp = l.insertOrdered(&n1B, descending); if (NULL == tmp) { error("insertOrdered did not insert a node"); } printList(l, "exp 4,3,2,1,1,0"); tmp = l.pop(4); if (tmp->data != &n1) { error("pos 2 is not n1\n"); } printf("n1 is good\n"); tmp = l.pop(5); if (tmp->data != &n1B) { error("pos3 is not n1B"); } printf("n1B is good\n"); } int main() { printf("\nJob Scheduler Demo\n"); testDescending(); error("done\n"); exit(0); }
API abstraction for a Linked List
Definition at line 262 of file LinkedList.h.
Constructor & Destructor Documentation
| LinkedList | ( | ) |
Create the LinkedList object.
Definition at line 270 of file LinkedList.h.
| ~LinkedList | ( | ) |
Deconstructor for the LinkedList object Removes any members.
Definition at line 279 of file LinkedList.h.
Member Function Documentation
| node<T>* append | ( | T * | data ) |
Add a member to the end of the list.
- Parameters:
-
data - Some data type that is added to the list
- Returns:
- The member that was just inserted (NULL if empty)
Definition at line 377 of file LinkedList.h.
| node<T>* insertOrdered | ( | T * | data, |
| bool(isBigger)(T *data1, T *data2) | |||
| ) |
Add a member to some position based on sort condition.
The list will be iterated from _head to end and the moment inserted data is NOT smaller than current node's data, then data will be inserted in front of current node. This will preserve ascending order of data.
- Parameters:
-
data - some data type that is added to the list isBigger - comparator function returns true if data1 is bigger than data2 and false otherwise. If data1 equals data2, then ">" comparision will result in LIFO order. Using ">=" operator in the function results in "FIFO" order and thus it preserves order in which items were added to the list.
If isBigger function is implemented with data1 < data2, then result insert will be in descending order. If data1 equals data2, then LIFO order is established. If data1 <= data2, then FIFO order is preserved when data1 equals data2.
- Returns:
- The member that was just inserted (NULL if not inserted).
Definition at line 325 of file LinkedList.h.
| uint32_t length | ( | void | ) |
Get the length of the list.
- Returns:
- The number of members in the list
Definition at line 489 of file LinkedList.h.
| node<T>* pop | ( | uint32_t | loc ) |
Get access to a member from the list.
- Parameters:
-
loc - The location of the member to access
- Returns:
- The member that was just requested (NULL if empty or out of bounds)
Definition at line 447 of file LinkedList.h.
| node<T>* push | ( | T * | data ) |
Add a member to the begining of the list.
- Parameters:
-
data - Some data type that is added to the list
- Returns:
- The member that was just inserted (NULL if empty)
Definition at line 288 of file LinkedList.h.
| node<T>* remove | ( | uint32_t | loc ) |
Remove a member from the list.
- Parameters:
-
loc - The location of the member to remove
- Returns:
- The head of the list
Definition at line 414 of file LinkedList.h.
Generated on Wed Jul 13 2022 14:39:12 by
1.7.2
