Francis CHATAIN / Mbed 2 deprecated MSNV2-Terminal_V1-6

Dependencies:   DmTftLibrary eeprom SX1280Lib filesystem mbed

Fork of MSNV2-Terminal_V1-5 by Francis CHATAIN

Committer:
lo
Date:
Tue Sep 18 19:41:38 2018 +0000
Revision:
26:271d2d510f6c
added components implementations

Who changed what in which revision?

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