Library for MAX31850 thermocouple temperature sensor
LinkedList.cpp@0:5530bf2d0e94, 2019-01-23 (annotated)
- Committer:
- croberts21
- Date:
- Wed Jan 23 23:44:57 2019 +0000
- Revision:
- 0:5530bf2d0e94
All files added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
croberts21 | 0:5530bf2d0e94 | 1 | /** |
croberts21 | 0:5530bf2d0e94 | 2 | * @file LinkedList.cpp |
croberts21 | 0:5530bf2d0e94 | 3 | * @brief Core Utility - Templated Linked List class |
croberts21 | 0:5530bf2d0e94 | 4 | * @author sam grove |
croberts21 | 0:5530bf2d0e94 | 5 | * @version 1.0 |
croberts21 | 0:5530bf2d0e94 | 6 | * @see |
croberts21 | 0:5530bf2d0e94 | 7 | * |
croberts21 | 0:5530bf2d0e94 | 8 | * Copyright (c) 2013 |
croberts21 | 0:5530bf2d0e94 | 9 | * |
croberts21 | 0:5530bf2d0e94 | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); |
croberts21 | 0:5530bf2d0e94 | 11 | * you may not use this file except in compliance with the License. |
croberts21 | 0:5530bf2d0e94 | 12 | * You may obtain a copy of the License at |
croberts21 | 0:5530bf2d0e94 | 13 | * |
croberts21 | 0:5530bf2d0e94 | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
croberts21 | 0:5530bf2d0e94 | 15 | * |
croberts21 | 0:5530bf2d0e94 | 16 | * Unless required by applicable law or agreed to in writing, software |
croberts21 | 0:5530bf2d0e94 | 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
croberts21 | 0:5530bf2d0e94 | 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
croberts21 | 0:5530bf2d0e94 | 19 | * See the License for the specific language governing permissions and |
croberts21 | 0:5530bf2d0e94 | 20 | * limitations under the License. |
croberts21 | 0:5530bf2d0e94 | 21 | */ |
croberts21 | 0:5530bf2d0e94 | 22 | |
croberts21 | 0:5530bf2d0e94 | 23 | #include "LinkedList.h" // api wrapper |
croberts21 | 0:5530bf2d0e94 | 24 | |
croberts21 | 0:5530bf2d0e94 | 25 | template<class retT> |
croberts21 | 0:5530bf2d0e94 | 26 | LinkedList<retT>::LinkedList() |
croberts21 | 0:5530bf2d0e94 | 27 | { |
croberts21 | 0:5530bf2d0e94 | 28 | // clear the member |
croberts21 | 0:5530bf2d0e94 | 29 | _head = 0; |
croberts21 | 0:5530bf2d0e94 | 30 | |
croberts21 | 0:5530bf2d0e94 | 31 | return; |
croberts21 | 0:5530bf2d0e94 | 32 | } |
croberts21 | 0:5530bf2d0e94 | 33 | |
croberts21 | 0:5530bf2d0e94 | 34 | template<class retT> |
croberts21 | 0:5530bf2d0e94 | 35 | LinkedList<retT>::~LinkedList() |
croberts21 | 0:5530bf2d0e94 | 36 | { |
croberts21 | 0:5530bf2d0e94 | 37 | // free any memory that is on the heap |
croberts21 | 0:5530bf2d0e94 | 38 | while(remove(1) != NULL); |
croberts21 | 0:5530bf2d0e94 | 39 | |
croberts21 | 0:5530bf2d0e94 | 40 | return; |
croberts21 | 0:5530bf2d0e94 | 41 | } |
croberts21 | 0:5530bf2d0e94 | 42 | |
croberts21 | 0:5530bf2d0e94 | 43 | template<class retT> |
croberts21 | 0:5530bf2d0e94 | 44 | retT *LinkedList<retT>::push(void *data) |
croberts21 | 0:5530bf2d0e94 | 45 | { |
croberts21 | 0:5530bf2d0e94 | 46 | retT *new_node = new retT [1]; |
croberts21 | 0:5530bf2d0e94 | 47 | // make sure the new object was allocated |
croberts21 | 0:5530bf2d0e94 | 48 | if (0 == new_node) |
croberts21 | 0:5530bf2d0e94 | 49 | { |
croberts21 | 0:5530bf2d0e94 | 50 | error("Memory allocation failed\n"); |
croberts21 | 0:5530bf2d0e94 | 51 | } |
croberts21 | 0:5530bf2d0e94 | 52 | // update the next item in the list to the current head |
croberts21 | 0:5530bf2d0e94 | 53 | new_node->next = _head; |
croberts21 | 0:5530bf2d0e94 | 54 | // store the address to the linked datatype |
croberts21 | 0:5530bf2d0e94 | 55 | new_node->data = data; |
croberts21 | 0:5530bf2d0e94 | 56 | _head = new_node; |
croberts21 | 0:5530bf2d0e94 | 57 | |
croberts21 | 0:5530bf2d0e94 | 58 | return _head; |
croberts21 | 0:5530bf2d0e94 | 59 | } |
croberts21 | 0:5530bf2d0e94 | 60 | |
croberts21 | 0:5530bf2d0e94 | 61 | //template<class retT> |
croberts21 | 0:5530bf2d0e94 | 62 | //retT *LinkedList<retT>::insert(void *data, uint32_t loc) |
croberts21 | 0:5530bf2d0e94 | 63 | //{ |
croberts21 | 0:5530bf2d0e94 | 64 | // retT *new_node = new retT [1]; |
croberts21 | 0:5530bf2d0e94 | 65 | // // make sure the new object was allocated |
croberts21 | 0:5530bf2d0e94 | 66 | // if (0 == new_node) |
croberts21 | 0:5530bf2d0e94 | 67 | // { |
croberts21 | 0:5530bf2d0e94 | 68 | // error("Memory allocation failed\n"); |
croberts21 | 0:5530bf2d0e94 | 69 | // } |
croberts21 | 0:5530bf2d0e94 | 70 | // retT *current = _head->next; |
croberts21 | 0:5530bf2d0e94 | 71 | // retT *prev = _head; |
croberts21 | 0:5530bf2d0e94 | 72 | // // move to the item we want to insert |
croberts21 | 0:5530bf2d0e94 | 73 | // for (uint32_t i=1; i!=(loc-1); i++) |
croberts21 | 0:5530bf2d0e94 | 74 | // { |
croberts21 | 0:5530bf2d0e94 | 75 | // prev = current; |
croberts21 | 0:5530bf2d0e94 | 76 | // current = current->next; |
croberts21 | 0:5530bf2d0e94 | 77 | // } |
croberts21 | 0:5530bf2d0e94 | 78 | // // store the address to the linked datatype |
croberts21 | 0:5530bf2d0e94 | 79 | // new_node->data = data; |
croberts21 | 0:5530bf2d0e94 | 80 | // // clear the next pointer |
croberts21 | 0:5530bf2d0e94 | 81 | // new_node->next = 0; |
croberts21 | 0:5530bf2d0e94 | 82 | // // update the list and store the new stuff |
croberts21 | 0:5530bf2d0e94 | 83 | // prev->next = new_node; |
croberts21 | 0:5530bf2d0e94 | 84 | // new_node->next = current; |
croberts21 | 0:5530bf2d0e94 | 85 | // // store the address to the linked datatype |
croberts21 | 0:5530bf2d0e94 | 86 | // _head->data = data; |
croberts21 | 0:5530bf2d0e94 | 87 | // |
croberts21 | 0:5530bf2d0e94 | 88 | // return prev->next; |
croberts21 | 0:5530bf2d0e94 | 89 | //} |
croberts21 | 0:5530bf2d0e94 | 90 | |
croberts21 | 0:5530bf2d0e94 | 91 | template<class retT> |
croberts21 | 0:5530bf2d0e94 | 92 | retT *LinkedList<retT>::append(void *data) |
croberts21 | 0:5530bf2d0e94 | 93 | { |
croberts21 | 0:5530bf2d0e94 | 94 | retT *current = _head; |
croberts21 | 0:5530bf2d0e94 | 95 | retT *new_node = new retT [1]; |
croberts21 | 0:5530bf2d0e94 | 96 | // make sure the new object was allocated |
croberts21 | 0:5530bf2d0e94 | 97 | if (0 == new_node) |
croberts21 | 0:5530bf2d0e94 | 98 | { |
croberts21 | 0:5530bf2d0e94 | 99 | error("Memory allocation failed\n"); |
croberts21 | 0:5530bf2d0e94 | 100 | } |
croberts21 | 0:5530bf2d0e94 | 101 | // store the address to the linked datatype |
croberts21 | 0:5530bf2d0e94 | 102 | new_node->data = data; |
croberts21 | 0:5530bf2d0e94 | 103 | // clear the next pointer |
croberts21 | 0:5530bf2d0e94 | 104 | new_node->next = 0; |
croberts21 | 0:5530bf2d0e94 | 105 | // check for an empty list |
croberts21 | 0:5530bf2d0e94 | 106 | if (0 == current) |
croberts21 | 0:5530bf2d0e94 | 107 | { |
croberts21 | 0:5530bf2d0e94 | 108 | _head = new_node; |
croberts21 | 0:5530bf2d0e94 | 109 | return _head; |
croberts21 | 0:5530bf2d0e94 | 110 | } |
croberts21 | 0:5530bf2d0e94 | 111 | else |
croberts21 | 0:5530bf2d0e94 | 112 | { |
croberts21 | 0:5530bf2d0e94 | 113 | // look for the end of the list |
croberts21 | 0:5530bf2d0e94 | 114 | while (current->next != 0) |
croberts21 | 0:5530bf2d0e94 | 115 | { |
croberts21 | 0:5530bf2d0e94 | 116 | current = current->next; |
croberts21 | 0:5530bf2d0e94 | 117 | } |
croberts21 | 0:5530bf2d0e94 | 118 | // and then add the new item to the list |
croberts21 | 0:5530bf2d0e94 | 119 | current->next = new_node; |
croberts21 | 0:5530bf2d0e94 | 120 | } |
croberts21 | 0:5530bf2d0e94 | 121 | |
croberts21 | 0:5530bf2d0e94 | 122 | return current->next; |
croberts21 | 0:5530bf2d0e94 | 123 | } |
croberts21 | 0:5530bf2d0e94 | 124 | |
croberts21 | 0:5530bf2d0e94 | 125 | template<class retT> |
croberts21 | 0:5530bf2d0e94 | 126 | retT *LinkedList<retT>::remove(uint32_t loc) |
croberts21 | 0:5530bf2d0e94 | 127 | { |
croberts21 | 0:5530bf2d0e94 | 128 | retT *current = _head; |
croberts21 | 0:5530bf2d0e94 | 129 | retT *prev = 0; |
croberts21 | 0:5530bf2d0e94 | 130 | // make sure we have an item to remove |
croberts21 | 0:5530bf2d0e94 | 131 | if ((loc <= length()) && (loc > 0)) |
croberts21 | 0:5530bf2d0e94 | 132 | { |
croberts21 | 0:5530bf2d0e94 | 133 | // move to the item we want to delete |
croberts21 | 0:5530bf2d0e94 | 134 | if (1 == loc) |
croberts21 | 0:5530bf2d0e94 | 135 | { |
croberts21 | 0:5530bf2d0e94 | 136 | _head = current->next; |
croberts21 | 0:5530bf2d0e94 | 137 | delete [] current; |
croberts21 | 0:5530bf2d0e94 | 138 | } |
croberts21 | 0:5530bf2d0e94 | 139 | else |
croberts21 | 0:5530bf2d0e94 | 140 | { |
croberts21 | 0:5530bf2d0e94 | 141 | for (uint32_t i=2; i<=loc; ++i) |
croberts21 | 0:5530bf2d0e94 | 142 | { |
croberts21 | 0:5530bf2d0e94 | 143 | prev = current; |
croberts21 | 0:5530bf2d0e94 | 144 | current = current->next; |
croberts21 | 0:5530bf2d0e94 | 145 | } |
croberts21 | 0:5530bf2d0e94 | 146 | // store the item + 1 to replace what we are deleting |
croberts21 | 0:5530bf2d0e94 | 147 | prev->next = current->next; |
croberts21 | 0:5530bf2d0e94 | 148 | delete [] current; |
croberts21 | 0:5530bf2d0e94 | 149 | } |
croberts21 | 0:5530bf2d0e94 | 150 | } |
croberts21 | 0:5530bf2d0e94 | 151 | |
croberts21 | 0:5530bf2d0e94 | 152 | return _head; |
croberts21 | 0:5530bf2d0e94 | 153 | } |
croberts21 | 0:5530bf2d0e94 | 154 | |
croberts21 | 0:5530bf2d0e94 | 155 | template<class retT> |
croberts21 | 0:5530bf2d0e94 | 156 | retT *LinkedList<retT>::pop(uint32_t loc) |
croberts21 | 0:5530bf2d0e94 | 157 | { |
croberts21 | 0:5530bf2d0e94 | 158 | retT *current = _head; |
croberts21 | 0:5530bf2d0e94 | 159 | // make sure we have something in the location |
croberts21 | 0:5530bf2d0e94 | 160 | if ((loc > length()) || (loc == 0)) |
croberts21 | 0:5530bf2d0e94 | 161 | { |
croberts21 | 0:5530bf2d0e94 | 162 | return 0; |
croberts21 | 0:5530bf2d0e94 | 163 | } |
croberts21 | 0:5530bf2d0e94 | 164 | // and if so jump down the list |
croberts21 | 0:5530bf2d0e94 | 165 | for (uint32_t i=2; i<=loc; ++i) |
croberts21 | 0:5530bf2d0e94 | 166 | { |
croberts21 | 0:5530bf2d0e94 | 167 | current = current->next; |
croberts21 | 0:5530bf2d0e94 | 168 | } |
croberts21 | 0:5530bf2d0e94 | 169 | |
croberts21 | 0:5530bf2d0e94 | 170 | return current; |
croberts21 | 0:5530bf2d0e94 | 171 | } |
croberts21 | 0:5530bf2d0e94 | 172 | |
croberts21 | 0:5530bf2d0e94 | 173 | template<class retT> |
croberts21 | 0:5530bf2d0e94 | 174 | uint32_t LinkedList<retT>::length(void) |
croberts21 | 0:5530bf2d0e94 | 175 | { |
croberts21 | 0:5530bf2d0e94 | 176 | int32_t count = 0; |
croberts21 | 0:5530bf2d0e94 | 177 | retT *current = _head; |
croberts21 | 0:5530bf2d0e94 | 178 | //loop until the end of the list is found |
croberts21 | 0:5530bf2d0e94 | 179 | while (current != 0) |
croberts21 | 0:5530bf2d0e94 | 180 | { |
croberts21 | 0:5530bf2d0e94 | 181 | ++count; |
croberts21 | 0:5530bf2d0e94 | 182 | current = current->next; |
croberts21 | 0:5530bf2d0e94 | 183 | } |
croberts21 | 0:5530bf2d0e94 | 184 | |
croberts21 | 0:5530bf2d0e94 | 185 | return count; |
croberts21 | 0:5530bf2d0e94 | 186 | } |
croberts21 | 0:5530bf2d0e94 | 187 | |
croberts21 | 0:5530bf2d0e94 | 188 | // pre-define the type for the linker |
croberts21 | 0:5530bf2d0e94 | 189 | template class LinkedList<node>; |
croberts21 | 0:5530bf2d0e94 | 190 | |
croberts21 | 0:5530bf2d0e94 | 191 | |
croberts21 | 0:5530bf2d0e94 | 192 | |
croberts21 | 0:5530bf2d0e94 | 193 | |
croberts21 | 0:5530bf2d0e94 | 194 |