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:
Thu Apr 04 20:34:38 2013 +0000
Revision:
0:3f64a15357ac
Child:
1:a032c0392ba1
Upload from different project and add 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 0:3f64a15357ac 29 * @enum 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 *
sam_grove 0:3f64a15357ac 40 * Example:
sam_grove 0:3f64a15357ac 41 * @code
sam_grove 0:3f64a15357ac 42 * int main(void)
sam_grove 0:3f64a15357ac 43 * {
sam_grove 0:3f64a15357ac 44 * }
sam_grove 0:3f64a15357ac 45 * @endcode
sam_grove 0:3f64a15357ac 46 */
sam_grove 0:3f64a15357ac 47
sam_grove 0:3f64a15357ac 48 /**
sam_grove 0:3f64a15357ac 49 * @class LinkedList
sam_grove 0:3f64a15357ac 50 * @brief API abstraction for a Linked List
sam_grove 0:3f64a15357ac 51 */
sam_grove 0:3f64a15357ac 52 template<class retT>
sam_grove 0:3f64a15357ac 53 class LinkedList
sam_grove 0:3f64a15357ac 54 {
sam_grove 0:3f64a15357ac 55 protected:
sam_grove 0:3f64a15357ac 56 retT *_head;
sam_grove 0:3f64a15357ac 57
sam_grove 0:3f64a15357ac 58 public:
sam_grove 0:3f64a15357ac 59 /** Create the LinkedList object
sam_grove 0:3f64a15357ac 60 */
sam_grove 0:3f64a15357ac 61 LinkedList();
sam_grove 0:3f64a15357ac 62
sam_grove 0:3f64a15357ac 63 /** Deconstructor for the LinkedList object
sam_grove 0:3f64a15357ac 64 * Removes any members
sam_grove 0:3f64a15357ac 65 */
sam_grove 0:3f64a15357ac 66 ~LinkedList();
sam_grove 0:3f64a15357ac 67
sam_grove 0:3f64a15357ac 68 /** Add a member to the begining of the list
sam_grove 0:3f64a15357ac 69 * @param data - Some data type that is added to the list
sam_grove 0:3f64a15357ac 70 * @return The member that was just inserted (NULL if empty)
sam_grove 0:3f64a15357ac 71 */
sam_grove 0:3f64a15357ac 72 retT *push(void *data);
sam_grove 0:3f64a15357ac 73
sam_grove 0:3f64a15357ac 74 /** Add a member to some position in the list
sam_grove 0:3f64a15357ac 75 * @param data - Some data type that is added to the list
sam_grove 0:3f64a15357ac 76 * @param loc - Place in the list to put the data
sam_grove 0:3f64a15357ac 77 * @return The member that was just inserted (NULL if empty)
sam_grove 0:3f64a15357ac 78 */
sam_grove 0:3f64a15357ac 79 retT *insert(void *data, uint32_t loc);
sam_grove 0:3f64a15357ac 80
sam_grove 0:3f64a15357ac 81 /** Add a member to the end of the list
sam_grove 0:3f64a15357ac 82 * @param data - Some data type that is added to the list
sam_grove 0:3f64a15357ac 83 * @return The member that was just inserted (NULL if empty)
sam_grove 0:3f64a15357ac 84 */
sam_grove 0:3f64a15357ac 85 retT *append(void *data);
sam_grove 0:3f64a15357ac 86
sam_grove 0:3f64a15357ac 87 /** Remove a member from the list
sam_grove 0:3f64a15357ac 88 * @param loc - The location of the member to remove
sam_grove 0:3f64a15357ac 89 * @return The head of the list
sam_grove 0:3f64a15357ac 90 */
sam_grove 0:3f64a15357ac 91 retT *remove(uint32_t loc);
sam_grove 0:3f64a15357ac 92
sam_grove 0:3f64a15357ac 93 /** Get access to a member from the list
sam_grove 0:3f64a15357ac 94 * @param loc - The location of the member to access
sam_grove 0:3f64a15357ac 95 * @return The member that was just requested (NULL if empty or out of bounds)
sam_grove 0:3f64a15357ac 96 */
sam_grove 0:3f64a15357ac 97 retT *pop(uint32_t loc);
sam_grove 0:3f64a15357ac 98
sam_grove 0:3f64a15357ac 99 /** Get the length of the list
sam_grove 0:3f64a15357ac 100 * @return The number of members in the list
sam_grove 0:3f64a15357ac 101 */
sam_grove 0:3f64a15357ac 102 uint32_t length(void);
sam_grove 0:3f64a15357ac 103 };
sam_grove 0:3f64a15357ac 104
sam_grove 0:3f64a15357ac 105 #endif /* LINKEDLIST_H_ */