a

Dependencies:   mbed

Committer:
Jagang
Date:
Sun Dec 14 17:49:01 2014 +0000
Revision:
0:85567bbcebdb
New

Who changed what in which revision?

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