Class to manage a linked list. Utility that can be built on or used alone

Dependents:   Waldo_Embed_V2 elevator_with_queue RaheeNew DS1820 ... more

Good information on linked list basics here.

Information

Dependencies not included with library:

#include "mbed.h"
#include "LL.h"
  
LL<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");
}
Committer:
sam_grove
Date:
Sat Dec 28 19:06:07 2019 +0000
Revision:
8:a7956e9c2bf5
mbed-os has files and a class named LinkedList. Rename the file and class name to match the files.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sam_grove 8:a7956e9c2bf5 1 /**
sam_grove 8:a7956e9c2bf5 2 * @file LL.h
sam_grove 8:a7956e9c2bf5 3 * @brief Core Utility - Templated Linked List class
sam_grove 8:a7956e9c2bf5 4 * @author sam grove
sam_grove 8:a7956e9c2bf5 5 * @version 1.0
sam_grove 8:a7956e9c2bf5 6 * @see
sam_grove 8:a7956e9c2bf5 7 *
sam_grove 8:a7956e9c2bf5 8 * Copyright (c) 2013
sam_grove 8:a7956e9c2bf5 9 *
sam_grove 8:a7956e9c2bf5 10 * Licensed under the Apache License, Version 2.0 (the "License");
sam_grove 8:a7956e9c2bf5 11 * you may not use this file except in compliance with the License.
sam_grove 8:a7956e9c2bf5 12 * You may obtain a copy of the License at
sam_grove 8:a7956e9c2bf5 13 *
sam_grove 8:a7956e9c2bf5 14 * http://www.apache.org/licenses/LICENSE-2.0
sam_grove 8:a7956e9c2bf5 15 *
sam_grove 8:a7956e9c2bf5 16 * Unless required by applicable law or agreed to in writing, software
sam_grove 8:a7956e9c2bf5 17 * distributed under the License is distributed on an "AS IS" BASIS,
sam_grove 8:a7956e9c2bf5 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sam_grove 8:a7956e9c2bf5 19 * See the License for the specific language governing permissions and
sam_grove 8:a7956e9c2bf5 20 * limitations under the License.
sam_grove 8:a7956e9c2bf5 21 */
sam_grove 8:a7956e9c2bf5 22
sam_grove 8:a7956e9c2bf5 23 #ifndef LL_H_
sam_grove 8:a7956e9c2bf5 24 #define LL_H_
sam_grove 8:a7956e9c2bf5 25
sam_grove 8:a7956e9c2bf5 26 #include <stdint.h>
sam_grove 8:a7956e9c2bf5 27 #include <cstddef>
sam_grove 8:a7956e9c2bf5 28
sam_grove 8:a7956e9c2bf5 29 /**
sam_grove 8:a7956e9c2bf5 30 * @struct node
sam_grove 8:a7956e9c2bf5 31 * @brief The Linked List structure
sam_grove 8:a7956e9c2bf5 32 */
sam_grove 8:a7956e9c2bf5 33 struct node
sam_grove 8:a7956e9c2bf5 34 {
sam_grove 8:a7956e9c2bf5 35 void *data; /*!< pointer to list member data */
sam_grove 8:a7956e9c2bf5 36 struct node *next; /*!< pointer to the next list member */
sam_grove 8:a7956e9c2bf5 37 };
sam_grove 8:a7956e9c2bf5 38
sam_grove 8:a7956e9c2bf5 39 /** Example using the LL Class
sam_grove 8:a7956e9c2bf5 40 * @code
sam_grove 8:a7956e9c2bf5 41 * #include "mbed.h"
sam_grove 8:a7956e9c2bf5 42 * #include "LL.h"
sam_grove 8:a7956e9c2bf5 43 *
sam_grove 8:a7956e9c2bf5 44 * LL<node>list;
sam_grove 8:a7956e9c2bf5 45 *
sam_grove 8:a7956e9c2bf5 46 * int main()
sam_grove 8:a7956e9c2bf5 47 * {
sam_grove 8:a7956e9c2bf5 48 * node *tmp;
sam_grove 8:a7956e9c2bf5 49 *
sam_grove 8:a7956e9c2bf5 50 * list.push((char *)"Two\n");
sam_grove 8:a7956e9c2bf5 51 * list.append((char *)"Three\n");
sam_grove 8:a7956e9c2bf5 52 * list.append((char *)"Four\n");
sam_grove 8:a7956e9c2bf5 53 * list.push((char*)"One\n");
sam_grove 8:a7956e9c2bf5 54 * list.append((char*)"Five\n");
sam_grove 8:a7956e9c2bf5 55 *
sam_grove 8:a7956e9c2bf5 56 * for(int i=1; i<=list.length(); i++)
sam_grove 8:a7956e9c2bf5 57 * {
sam_grove 8:a7956e9c2bf5 58 * tmp = list.pop(i);
sam_grove 8:a7956e9c2bf5 59 * printf("%s", (char *)tmp->data);
sam_grove 8:a7956e9c2bf5 60 * }
sam_grove 8:a7956e9c2bf5 61 *
sam_grove 8:a7956e9c2bf5 62 * error("done\n");
sam_grove 8:a7956e9c2bf5 63 * }
sam_grove 8:a7956e9c2bf5 64 * @endcode
sam_grove 8:a7956e9c2bf5 65 */
sam_grove 8:a7956e9c2bf5 66
sam_grove 8:a7956e9c2bf5 67 /**
sam_grove 8:a7956e9c2bf5 68 * @class LL
sam_grove 8:a7956e9c2bf5 69 * @brief API abstraction for a Linked List
sam_grove 8:a7956e9c2bf5 70 */
sam_grove 8:a7956e9c2bf5 71 template<class retT>
sam_grove 8:a7956e9c2bf5 72 class LL
sam_grove 8:a7956e9c2bf5 73 {
sam_grove 8:a7956e9c2bf5 74 protected:
sam_grove 8:a7956e9c2bf5 75 retT *_head;
sam_grove 8:a7956e9c2bf5 76
sam_grove 8:a7956e9c2bf5 77 public:
sam_grove 8:a7956e9c2bf5 78 /** Create the LL object
sam_grove 8:a7956e9c2bf5 79 */
sam_grove 8:a7956e9c2bf5 80 LL();
sam_grove 8:a7956e9c2bf5 81
sam_grove 8:a7956e9c2bf5 82 /** Deconstructor for the LL object
sam_grove 8:a7956e9c2bf5 83 * Removes any members
sam_grove 8:a7956e9c2bf5 84 */
sam_grove 8:a7956e9c2bf5 85 ~LL();
sam_grove 8:a7956e9c2bf5 86
sam_grove 8:a7956e9c2bf5 87 /** Add a member to the begining of the list
sam_grove 8:a7956e9c2bf5 88 * @param data - Some data type that is added to the list
sam_grove 8:a7956e9c2bf5 89 * @return The member that was just inserted (NULL if empty)
sam_grove 8:a7956e9c2bf5 90 */
sam_grove 8:a7956e9c2bf5 91 retT *push(void *data);
sam_grove 8:a7956e9c2bf5 92
sam_grove 8:a7956e9c2bf5 93 // /** Add a member to some position in the list
sam_grove 8:a7956e9c2bf5 94 // * @param data - Some data type that is added to the list
sam_grove 8:a7956e9c2bf5 95 // * @param loc - Place in the list to put the data
sam_grove 8:a7956e9c2bf5 96 // * @return The member that was just inserted (NULL if empty)
sam_grove 8:a7956e9c2bf5 97 // */
sam_grove 8:a7956e9c2bf5 98 // retT *insert(void *data, uint32_t loc);
sam_grove 8:a7956e9c2bf5 99
sam_grove 8:a7956e9c2bf5 100 /** Add a member to the end of the list
sam_grove 8:a7956e9c2bf5 101 * @param data - Some data type that is added to the list
sam_grove 8:a7956e9c2bf5 102 * @return The member that was just inserted (NULL if empty)
sam_grove 8:a7956e9c2bf5 103 */
sam_grove 8:a7956e9c2bf5 104 retT *append(void *data);
sam_grove 8:a7956e9c2bf5 105
sam_grove 8:a7956e9c2bf5 106 /** Remove a member from the list
sam_grove 8:a7956e9c2bf5 107 * @param loc - The location of the member to remove
sam_grove 8:a7956e9c2bf5 108 * @return The head of the list
sam_grove 8:a7956e9c2bf5 109 */
sam_grove 8:a7956e9c2bf5 110 retT *remove(uint32_t loc);
sam_grove 8:a7956e9c2bf5 111
sam_grove 8:a7956e9c2bf5 112 /** Get access to a member from the list
sam_grove 8:a7956e9c2bf5 113 * @param loc - The location of the member to access
sam_grove 8:a7956e9c2bf5 114 * @return The member that was just requested (NULL if empty or out of bounds)
sam_grove 8:a7956e9c2bf5 115 */
sam_grove 8:a7956e9c2bf5 116 retT *pop(uint32_t loc);
sam_grove 8:a7956e9c2bf5 117
sam_grove 8:a7956e9c2bf5 118 /** Get the length of the list
sam_grove 8:a7956e9c2bf5 119 * @return The number of members in the list
sam_grove 8:a7956e9c2bf5 120 */
sam_grove 8:a7956e9c2bf5 121 uint32_t length(void);
sam_grove 8:a7956e9c2bf5 122 };
sam_grove 8:a7956e9c2bf5 123
sam_grove 8:a7956e9c2bf5 124 #endif /* LINKEDLIST_H_ */