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:
Mon Apr 08 22:23:25 2013 +0000
Revision:
3:c14e7a918e21
Parent:
2:704e1c9057c1
Child:
7:4ed66162aaa8
Updated class to offset such that 0 is empty or not a valid location. The start of the list logic is 1. Added documentation

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