Added ability to maintain ordered linked list based on "insertAsc" function. The function takes a comparator function that allows for specific order behavior. If values collide, then FIFO or LIFO order can be maintained based on comparator function implementation.
Fork of LinkedList by
LinkedList.h@8:918b196b0ac4, 2017-07-07 (annotated)
- Committer:
- sgnezdov
- Date:
- Fri Jul 07 23:29:29 2017 +0000
- Revision:
- 8:918b196b0ac4
- Parent:
- 7:4ed66162aaa8
- Child:
- 9:b173aed98988
added insertAsc function that maintains order of insertion and sort order.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sam_grove | 0:3f64a15357ac | 1 | /** |
sam_grove | 0:3f64a15357ac | 2 | * @file LinkedList.h |
sam_grove | 0:3f64a15357ac | 3 | * @brief Core Utility - Templated Linked List class |
sam_grove | 0:3f64a15357ac | 4 | * @author sam grove |
sam_grove | 0:3f64a15357ac | 5 | * @version 1.0 |
sam_grove | 0:3f64a15357ac | 6 | * @see |
sam_grove | 0:3f64a15357ac | 7 | * |
sam_grove | 0:3f64a15357ac | 8 | * Copyright (c) 2013 |
sam_grove | 0:3f64a15357ac | 9 | * |
sam_grove | 0:3f64a15357ac | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sam_grove | 0:3f64a15357ac | 11 | * you may not use this file except in compliance with the License. |
sam_grove | 0:3f64a15357ac | 12 | * You may obtain a copy of the License at |
sam_grove | 0:3f64a15357ac | 13 | * |
sam_grove | 0:3f64a15357ac | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
sam_grove | 0:3f64a15357ac | 15 | * |
sam_grove | 0:3f64a15357ac | 16 | * Unless required by applicable law or agreed to in writing, software |
sam_grove | 0:3f64a15357ac | 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
sam_grove | 0:3f64a15357ac | 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sam_grove | 0:3f64a15357ac | 19 | * See the License for the specific language governing permissions and |
sam_grove | 0:3f64a15357ac | 20 | * limitations under the License. |
sam_grove | 0:3f64a15357ac | 21 | */ |
sam_grove | 0:3f64a15357ac | 22 | |
sam_grove | 0:3f64a15357ac | 23 | #ifndef LINKEDLIST_H_ |
sam_grove | 0:3f64a15357ac | 24 | #define LINKEDLIST_H_ |
sam_grove | 0:3f64a15357ac | 25 | |
sam_grove | 0:3f64a15357ac | 26 | #include <stdint.h> |
Sissors | 7:4ed66162aaa8 | 27 | #include "mbed.h" |
sam_grove | 0:3f64a15357ac | 28 | |
sam_grove | 0:3f64a15357ac | 29 | /** |
sam_grove | 1:a032c0392ba1 | 30 | * @struct node |
sam_grove | 0:3f64a15357ac | 31 | * @brief The Linked List structure |
sam_grove | 0:3f64a15357ac | 32 | */ |
sam_grove | 0:3f64a15357ac | 33 | struct node |
sam_grove | 0:3f64a15357ac | 34 | { |
sam_grove | 0:3f64a15357ac | 35 | void *data; /*!< pointer to list member data */ |
sam_grove | 0:3f64a15357ac | 36 | struct node *next; /*!< pointer to the next list member */ |
sam_grove | 0:3f64a15357ac | 37 | }; |
sam_grove | 0:3f64a15357ac | 38 | |
sgnezdov | 8:918b196b0ac4 | 39 | ///** Returns true if data1 shall be inserted before data 2. |
sgnezdov | 8:918b196b0ac4 | 40 | //*/ |
sgnezdov | 8:918b196b0ac4 | 41 | //typedef bool insertAtFunc(void *data1, void *data2); |
sgnezdov | 8:918b196b0ac4 | 42 | |
sam_grove | 0:3f64a15357ac | 43 | /** Example using the LinkedList Class |
sam_grove | 0:3f64a15357ac | 44 | * @code |
sam_grove | 1:a032c0392ba1 | 45 | * #include "mbed.h" |
sam_grove | 1:a032c0392ba1 | 46 | * #include "LinkedList.h" |
sam_grove | 1:a032c0392ba1 | 47 | * |
sam_grove | 1:a032c0392ba1 | 48 | * LinkedList<node>list; |
sam_grove | 1:a032c0392ba1 | 49 | * |
sam_grove | 1:a032c0392ba1 | 50 | * int main() |
sam_grove | 1:a032c0392ba1 | 51 | * { |
sam_grove | 1:a032c0392ba1 | 52 | * node *tmp; |
sam_grove | 1:a032c0392ba1 | 53 | * |
sam_grove | 1:a032c0392ba1 | 54 | * list.push((char *)"Two\n"); |
sam_grove | 1:a032c0392ba1 | 55 | * list.append((char *)"Three\n"); |
sam_grove | 1:a032c0392ba1 | 56 | * list.append((char *)"Four\n"); |
sam_grove | 1:a032c0392ba1 | 57 | * list.push((char*)"One\n"); |
sam_grove | 1:a032c0392ba1 | 58 | * list.append((char*)"Five\n"); |
sam_grove | 1:a032c0392ba1 | 59 | * |
sam_grove | 3:c14e7a918e21 | 60 | * for(int i=1; i<=list.length(); i++) |
sam_grove | 1:a032c0392ba1 | 61 | * { |
sam_grove | 1:a032c0392ba1 | 62 | * tmp = list.pop(i); |
sam_grove | 1:a032c0392ba1 | 63 | * printf("%s", (char *)tmp->data); |
sam_grove | 1:a032c0392ba1 | 64 | * } |
sam_grove | 1:a032c0392ba1 | 65 | * |
sam_grove | 1:a032c0392ba1 | 66 | * error("done\n"); |
sam_grove | 1:a032c0392ba1 | 67 | * } |
sam_grove | 0:3f64a15357ac | 68 | * @endcode |
sam_grove | 0:3f64a15357ac | 69 | */ |
sam_grove | 0:3f64a15357ac | 70 | |
sam_grove | 0:3f64a15357ac | 71 | /** |
sam_grove | 0:3f64a15357ac | 72 | * @class LinkedList |
sam_grove | 0:3f64a15357ac | 73 | * @brief API abstraction for a Linked List |
sam_grove | 0:3f64a15357ac | 74 | */ |
sam_grove | 0:3f64a15357ac | 75 | template<class retT> |
sam_grove | 0:3f64a15357ac | 76 | class LinkedList |
sam_grove | 0:3f64a15357ac | 77 | { |
sam_grove | 0:3f64a15357ac | 78 | protected: |
sam_grove | 0:3f64a15357ac | 79 | retT *_head; |
sam_grove | 0:3f64a15357ac | 80 | |
sam_grove | 0:3f64a15357ac | 81 | public: |
sam_grove | 0:3f64a15357ac | 82 | /** Create the LinkedList object |
sam_grove | 0:3f64a15357ac | 83 | */ |
sam_grove | 0:3f64a15357ac | 84 | LinkedList(); |
sam_grove | 0:3f64a15357ac | 85 | |
sam_grove | 0:3f64a15357ac | 86 | /** Deconstructor for the LinkedList object |
sam_grove | 0:3f64a15357ac | 87 | * Removes any members |
sam_grove | 0:3f64a15357ac | 88 | */ |
sam_grove | 0:3f64a15357ac | 89 | ~LinkedList(); |
sam_grove | 0:3f64a15357ac | 90 | |
sam_grove | 0:3f64a15357ac | 91 | /** Add a member to the begining of the list |
sam_grove | 0:3f64a15357ac | 92 | * @param data - Some data type that is added to the list |
sam_grove | 0:3f64a15357ac | 93 | * @return The member that was just inserted (NULL if empty) |
sam_grove | 0:3f64a15357ac | 94 | */ |
sam_grove | 0:3f64a15357ac | 95 | retT *push(void *data); |
sam_grove | 0:3f64a15357ac | 96 | |
sgnezdov | 8:918b196b0ac4 | 97 | /** Add a member to some position based on sort condition. |
sgnezdov | 8:918b196b0ac4 | 98 | * The list will be iterated from _head to end and the moment inserted |
sgnezdov | 8:918b196b0ac4 | 99 | * data is NOT smaller than current node's data, then |
sgnezdov | 8:918b196b0ac4 | 100 | * data will be inserted in front of current node. |
sgnezdov | 8:918b196b0ac4 | 101 | * This will preserve ascending order of data. |
sgnezdov | 8:918b196b0ac4 | 102 | * @param data - some data type that is added to the list |
sgnezdov | 8:918b196b0ac4 | 103 | * @param isBigger - comparator function returns true if data1 is bigger |
sgnezdov | 8:918b196b0ac4 | 104 | * than data2 and false otherwise. If data1 equals data2, then ">" comparision |
sgnezdov | 8:918b196b0ac4 | 105 | * will result in LIFO order. Using ">=" operator in the function |
sgnezdov | 8:918b196b0ac4 | 106 | * results in "FIFO" order and thus it preserves order in which items |
sgnezdov | 8:918b196b0ac4 | 107 | * were added to the list. |
sgnezdov | 8:918b196b0ac4 | 108 | * @return The member that was just inserted (NULL if not inserted). |
sgnezdov | 8:918b196b0ac4 | 109 | */ |
sgnezdov | 8:918b196b0ac4 | 110 | retT *insertAsc(void *data, bool (isBigger)(void* data1, void *data2)); |
sam_grove | 0:3f64a15357ac | 111 | |
sam_grove | 0:3f64a15357ac | 112 | /** Add a member to the end of the list |
sam_grove | 0:3f64a15357ac | 113 | * @param data - Some data type that is added to the list |
sam_grove | 0:3f64a15357ac | 114 | * @return The member that was just inserted (NULL if empty) |
sam_grove | 0:3f64a15357ac | 115 | */ |
sam_grove | 0:3f64a15357ac | 116 | retT *append(void *data); |
sam_grove | 0:3f64a15357ac | 117 | |
sam_grove | 0:3f64a15357ac | 118 | /** Remove a member from the list |
sam_grove | 0:3f64a15357ac | 119 | * @param loc - The location of the member to remove |
sam_grove | 0:3f64a15357ac | 120 | * @return The head of the list |
sam_grove | 0:3f64a15357ac | 121 | */ |
sam_grove | 0:3f64a15357ac | 122 | retT *remove(uint32_t loc); |
sam_grove | 0:3f64a15357ac | 123 | |
sam_grove | 0:3f64a15357ac | 124 | /** Get access to a member from the list |
sam_grove | 0:3f64a15357ac | 125 | * @param loc - The location of the member to access |
sam_grove | 0:3f64a15357ac | 126 | * @return The member that was just requested (NULL if empty or out of bounds) |
sam_grove | 0:3f64a15357ac | 127 | */ |
sam_grove | 0:3f64a15357ac | 128 | retT *pop(uint32_t loc); |
sam_grove | 0:3f64a15357ac | 129 | |
sam_grove | 0:3f64a15357ac | 130 | /** Get the length of the list |
sam_grove | 0:3f64a15357ac | 131 | * @return The number of members in the list |
sam_grove | 0:3f64a15357ac | 132 | */ |
sam_grove | 0:3f64a15357ac | 133 | uint32_t length(void); |
sam_grove | 0:3f64a15357ac | 134 | }; |
sam_grove | 0:3f64a15357ac | 135 | |
sam_grove | 0:3f64a15357ac | 136 | #endif /* LINKEDLIST_H_ */ |