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:
Sissors
Date:
Sun Feb 23 14:09:48 2014 +0000
Revision:
7:4ed66162aaa8
Parent:
3:c14e7a918e21
Removed LogUtil dependency

Who changed what in which revision?

UserRevisionLine numberNew 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
sam_grove 0:3f64a15357ac 39 /** Example using the LinkedList Class
sam_grove 0:3f64a15357ac 40 * @code
sam_grove 1:a032c0392ba1 41 * #include "mbed.h"
sam_grove 1:a032c0392ba1 42 * #include "LinkedList.h"
sam_grove 1:a032c0392ba1 43 *
sam_grove 1:a032c0392ba1 44 * LinkedList<node>list;
sam_grove 1:a032c0392ba1 45 *
sam_grove 1:a032c0392ba1 46 * int main()
sam_grove 1:a032c0392ba1 47 * {
sam_grove 1:a032c0392ba1 48 * node *tmp;
sam_grove 1:a032c0392ba1 49 *
sam_grove 1:a032c0392ba1 50 * list.push((char *)"Two\n");
sam_grove 1:a032c0392ba1 51 * list.append((char *)"Three\n");
sam_grove 1:a032c0392ba1 52 * list.append((char *)"Four\n");
sam_grove 1:a032c0392ba1 53 * list.push((char*)"One\n");
sam_grove 1:a032c0392ba1 54 * list.append((char*)"Five\n");
sam_grove 1:a032c0392ba1 55 *
sam_grove 3:c14e7a918e21 56 * for(int i=1; i<=list.length(); i++)
sam_grove 1:a032c0392ba1 57 * {
sam_grove 1:a032c0392ba1 58 * tmp = list.pop(i);
sam_grove 1:a032c0392ba1 59 * printf("%s", (char *)tmp->data);
sam_grove 1:a032c0392ba1 60 * }
sam_grove 1:a032c0392ba1 61 *
sam_grove 1:a032c0392ba1 62 * error("done\n");
sam_grove 1:a032c0392ba1 63 * }
sam_grove 0:3f64a15357ac 64 * @endcode
sam_grove 0:3f64a15357ac 65 */
sam_grove 0:3f64a15357ac 66
sam_grove 0:3f64a15357ac 67 /**
sam_grove 0:3f64a15357ac 68 * @class LinkedList
sam_grove 0:3f64a15357ac 69 * @brief API abstraction for a Linked List
sam_grove 0:3f64a15357ac 70 */
sam_grove 0:3f64a15357ac 71 template<class retT>
sam_grove 0:3f64a15357ac 72 class LinkedList
sam_grove 0:3f64a15357ac 73 {
sam_grove 0:3f64a15357ac 74 protected:
sam_grove 0:3f64a15357ac 75 retT *_head;
sam_grove 0:3f64a15357ac 76
sam_grove 0:3f64a15357ac 77 public:
sam_grove 0:3f64a15357ac 78 /** Create the LinkedList object
sam_grove 0:3f64a15357ac 79 */
sam_grove 0:3f64a15357ac 80 LinkedList();
sam_grove 0:3f64a15357ac 81
sam_grove 0:3f64a15357ac 82 /** Deconstructor for the LinkedList object
sam_grove 0:3f64a15357ac 83 * Removes any members
sam_grove 0:3f64a15357ac 84 */
sam_grove 0:3f64a15357ac 85 ~LinkedList();
sam_grove 0:3f64a15357ac 86
sam_grove 0:3f64a15357ac 87 /** Add a member to the begining of the list
sam_grove 0:3f64a15357ac 88 * @param data - Some data type that is added to the list
sam_grove 0:3f64a15357ac 89 * @return The member that was just inserted (NULL if empty)
sam_grove 0:3f64a15357ac 90 */
sam_grove 0:3f64a15357ac 91 retT *push(void *data);
sam_grove 0:3f64a15357ac 92
sam_grove 1:a032c0392ba1 93 // /** Add a member to some position in the list
sam_grove 1:a032c0392ba1 94 // * @param data - Some data type that is added to the list
sam_grove 1:a032c0392ba1 95 // * @param loc - Place in the list to put the data
sam_grove 1:a032c0392ba1 96 // * @return The member that was just inserted (NULL if empty)
sam_grove 1:a032c0392ba1 97 // */
sam_grove 1:a032c0392ba1 98 // retT *insert(void *data, uint32_t loc);
sam_grove 0:3f64a15357ac 99
sam_grove 0:3f64a15357ac 100 /** Add a member to the end of the list
sam_grove 0:3f64a15357ac 101 * @param data - Some data type that is added to the list
sam_grove 0:3f64a15357ac 102 * @return The member that was just inserted (NULL if empty)
sam_grove 0:3f64a15357ac 103 */
sam_grove 0:3f64a15357ac 104 retT *append(void *data);
sam_grove 0:3f64a15357ac 105
sam_grove 0:3f64a15357ac 106 /** Remove a member from the list
sam_grove 0:3f64a15357ac 107 * @param loc - The location of the member to remove
sam_grove 0:3f64a15357ac 108 * @return The head of the list
sam_grove 0:3f64a15357ac 109 */
sam_grove 0:3f64a15357ac 110 retT *remove(uint32_t loc);
sam_grove 0:3f64a15357ac 111
sam_grove 0:3f64a15357ac 112 /** Get access to a member from the list
sam_grove 0:3f64a15357ac 113 * @param loc - The location of the member to access
sam_grove 0:3f64a15357ac 114 * @return The member that was just requested (NULL if empty or out of bounds)
sam_grove 0:3f64a15357ac 115 */
sam_grove 0:3f64a15357ac 116 retT *pop(uint32_t loc);
sam_grove 0:3f64a15357ac 117
sam_grove 0:3f64a15357ac 118 /** Get the length of the list
sam_grove 0:3f64a15357ac 119 * @return The number of members in the list
sam_grove 0:3f64a15357ac 120 */
sam_grove 0:3f64a15357ac 121 uint32_t length(void);
sam_grove 0:3f64a15357ac 122 };
sam_grove 0:3f64a15357ac 123
sam_grove 0:3f64a15357ac 124 #endif /* LINKEDLIST_H_ */