diff --git a/LinkedList.h b/LinkedList.h index 64321cf..db98c12 100644 --- a/LinkedList.h +++ b/LinkedList.h @@ -1,17 +1,17 @@ /* - LinkedList.h - V1.1 - Generic LinkedList implementation + TrueLinkedList.h - V1.1 - Generic TrueLinkedList implementation Works better with FIFO, because LIFO will need to search the entire List to find the last one; - For instructions, go to https://github.com/ivanseidel/LinkedList + For instructions, go to https://github.com/ivanseidel/TrueLinkedList Created by Ivan Seidel Gomes, March, 2013. Released into the public domain. */ -#ifndef LinkedList_h -#define LinkedList_h +#ifndef TrueLinkedList_h +#define TrueLinkedList_h #include @@ -23,7 +23,7 @@ struct ListNode }; template -class LinkedList{ +class TrueLinkedList{ protected: int _size; @@ -42,27 +42,27 @@ class LinkedList{ ListNode* findEndOfSortedString(ListNode *p, int (*cmp)(T &, T &)); public: - LinkedList(); - LinkedList(int sizeIndex, T _t); //initiate list size and default value - ~LinkedList(); + TrueLinkedList(); + TrueLinkedList(int sizeIndex, T _t); //initiate list size and default value + ~TrueLinkedList(); /* - Returns current size of LinkedList + Returns current size of TrueLinkedList */ virtual int size(); /* Adds a T object in the specified index; - Unlink and link the LinkedList correcly; + Unlink and link the TrueLinkedList correcly; Increment _size */ virtual bool add(int index, T); /* - Adds a T object in the end of the LinkedList; + Adds a T object in the end of the TrueLinkedList; Increment _size; */ virtual bool add(T); /* - Adds a T object in the start of the LinkedList; + Adds a T object in the start of the TrueLinkedList; Increment _size; */ virtual bool unshift(T); @@ -108,9 +108,9 @@ class LinkedList{ }; -// Initialize LinkedList with false values +// Initialize TrueLinkedList with false values template -LinkedList::LinkedList() +TrueLinkedList::TrueLinkedList() { root=NULL; last=NULL; @@ -123,7 +123,7 @@ LinkedList::LinkedList() // Clear Nodes and free Memory template -LinkedList::~LinkedList() +TrueLinkedList::~TrueLinkedList() { ListNode* tmp; while(root!=NULL) @@ -142,7 +142,7 @@ LinkedList::~LinkedList() */ template -ListNode* LinkedList::getNode(int index){ +ListNode* TrueLinkedList::getNode(int index){ int _pos = 0; ListNode* current = root; @@ -173,19 +173,19 @@ ListNode* LinkedList::getNode(int index){ } template -int LinkedList::size(){ +int TrueLinkedList::size(){ return _size; } template -LinkedList::LinkedList(int sizeIndex, T _t){ +TrueLinkedList::TrueLinkedList(int sizeIndex, T _t){ for (int i = 0; i < sizeIndex; i++){ add(_t); } } template -bool LinkedList::add(int index, T _t){ +bool TrueLinkedList::add(int index, T _t){ if(index >= _size) return add(_t); @@ -206,7 +206,7 @@ bool LinkedList::add(int index, T _t){ } template -bool LinkedList::add(T _t){ +bool TrueLinkedList::add(T _t){ ListNode *tmp = new ListNode(); tmp->data = _t; @@ -229,7 +229,7 @@ bool LinkedList::add(T _t){ } template -bool LinkedList::unshift(T _t){ +bool TrueLinkedList::unshift(T _t){ if(_size == 0) return add(_t); @@ -247,12 +247,12 @@ bool LinkedList::unshift(T _t){ template -T& LinkedList::operator[](int index) { +T& TrueLinkedList::operator[](int index) { return getNode(index)->data; } template -bool LinkedList::set(int index, T _t){ +bool TrueLinkedList::set(int index, T _t){ // Check if index position is in bounds if(index < 0 || index >= _size) return false; @@ -262,7 +262,7 @@ bool LinkedList::set(int index, T _t){ } template -T LinkedList::pop(){ +T TrueLinkedList::pop(){ if(_size <= 0) return T(); @@ -288,7 +288,7 @@ T LinkedList::pop(){ } template -T LinkedList::shift(){ +T TrueLinkedList::shift(){ if(_size <= 0) return T(); @@ -309,7 +309,7 @@ T LinkedList::shift(){ } template -T LinkedList::remove(int index){ +T TrueLinkedList::remove(int index){ if (index < 0 || index >= _size) { return T(); @@ -335,20 +335,20 @@ T LinkedList::remove(int index){ template -T LinkedList::get(int index){ +T TrueLinkedList::get(int index){ ListNode *tmp = getNode(index); return (tmp ? tmp->data : T()); } template -void LinkedList::clear(){ +void TrueLinkedList::clear(){ while(size() > 0) shift(); } template -void LinkedList::sort(int (*cmp)(T &, T &)){ +void TrueLinkedList::sort(int (*cmp)(T &, T &)){ if(_size < 2) return; // trivial case; for(;;) { @@ -408,7 +408,7 @@ void LinkedList::sort(int (*cmp)(T &, T &)){ } template -ListNode* LinkedList::findEndOfSortedString(ListNode *p, int (*cmp)(T &, T &)) { +ListNode* TrueLinkedList::findEndOfSortedString(ListNode *p, int (*cmp)(T &, T &)) { while(p->next && cmp(p->data, p->next->data) <= 0) { p = p->next; } diff --git a/README.md b/README.md index 5e3cd5e..32cb75a 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# LinkedList +# TrueLinkedList This library was developed targeting **`Arduino`** applications. However, works just great with any C++. @@ -11,8 +11,8 @@ With a simple but powerful caching algorithm, you can get subsequent objects muc ## Installation -1. [Download](https://github.com/ivanseidel/LinkedList/archive/master.zip) the Latest release from gitHub. -2. Unzip and modify the Folder name to "LinkedList" (Remove the '-version') +1. [Download](https://github.com/ivanseidel/TrueLinkedList/archive/master.zip) the Latest release from gitHub. +2. Unzip and modify the Folder name to "TrueLinkedList" (Remove the '-version') 3. Paste the modified folder on your Library folder (On your `Libraries` folder inside Sketchbooks or Arduino software). 4. Reopen the Arduino software. @@ -26,24 +26,24 @@ With a simple but powerful caching algorithm, you can get subsequent objects muc ## Getting started -### The `LinkedList` class +### The `TrueLinkedList` class -In case you don't know what a LinkedList is and what it's used for, take a quick look at [Wikipedia::LinkedList](https://en.wikipedia.org/wiki/Linked_list) before continuing. +In case you don't know what a TrueLinkedList is and what it's used for, take a quick look at [Wikipedia::TrueLinkedList](https://en.wikipedia.org/wiki/Linked_list) before continuing. -#### To declare a LinkedList object +#### To declare a TrueLinkedList object ```c++ -// Instantiate a LinkedList that will hold 'integer' -LinkedList myLinkedList = LinkedList(); +// Instantiate a TrueLinkedList that will hold 'integer' +TrueLinkedList myTrueLinkedList = TrueLinkedList(); // Or just this -LinkedList myLinkedList; +TrueLinkedList myTrueLinkedList; -// But if you are instantiating a pointer LinkedList... -LinkedList *myLinkedList = new LinkedList(); +// But if you are instantiating a pointer TrueLinkedList... +TrueLinkedList *myTrueLinkedList = new TrueLinkedList(); -// If you want a LinkedList with any other type such as 'MyClass' +// If you want a TrueLinkedList with any other type such as 'MyClass' // Make sure you call delete(MyClass) when you remove! -LinkedList *myLinkedList = new LinkedList(); +TrueLinkedList *myTrueLinkedList = new TrueLinkedList(); ``` #### Getting the size of the linked list @@ -137,47 +137,47 @@ myList.sort(myComparator); - `ListNode` `*next` - Pointer to the next Node -### `LinkedList` class +### `TrueLinkedList` class **`boolean` methods returns if succeeded** -- `LinkedList::LinkedList()` - Constructor. +- `TrueLinkedList::TrueLinkedList()` - Constructor. -- `LinkedList::~LinkedList()` - Destructor. Clear Nodes to minimize memory. Does not free pointer memory. +- `TrueLinkedList::~TrueLinkedList()` - Destructor. Clear Nodes to minimize memory. Does not free pointer memory. -- `int` `LinkedList::size()` - Returns the current size of the list. +- `int` `TrueLinkedList::size()` - Returns the current size of the list. -- `bool` `LinkedList::add(T)` - Add element T at the END of the list. +- `bool` `TrueLinkedList::add(T)` - Add element T at the END of the list. -- `bool` `LinkedList::add(int index, T)` - Add element T at `index` of the list. +- `bool` `TrueLinkedList::add(int index, T)` - Add element T at `index` of the list. -- `bool` `LinkedList::unshift(T)` - Add element T at the BEGINNING of the list. +- `bool` `TrueLinkedList::unshift(T)` - Add element T at the BEGINNING of the list. -- `bool` `LinkedList::set(int index, T)` - Set the element at `index` to T. +- `bool` `TrueLinkedList::set(int index, T)` - Set the element at `index` to T. -- `T` `LinkedList::remove(int index)` - Remove element at `index`. Return the removed element. Does not free pointer memory +- `T` `TrueLinkedList::remove(int index)` - Remove element at `index`. Return the removed element. Does not free pointer memory -- `T` `LinkedList::pop()` - Remove the LAST element. Return the removed element. +- `T` `TrueLinkedList::pop()` - Remove the LAST element. Return the removed element. -- `T` `LinkedList::shift()` - Remove the FIRST element. Return the removed element. +- `T` `TrueLinkedList::shift()` - Remove the FIRST element. Return the removed element. -- `T` `LinkedList::get(int index)` - Return the element at `index`. +- `T` `TrueLinkedList::get(int index)` - Return the element at `index`. -- `void` `LinkedList::clear()` - Removes all elements. Does not free pointer memory. +- `void` `TrueLinkedList::clear()` - Removes all elements. Does not free pointer memory. -- `void` `LinkedList::sort(int (*cmp)(T &, T &))` - Sorts the linked list according to a comparator funcrion. The comparator should return < 0 if the first argument should be sorted before the second, and > 0 if the first argument should be sorted after the first element. (Same as how `strcmp()` works.) +- `void` `TrueLinkedList::sort(int (*cmp)(T &, T &))` - Sorts the linked list according to a comparator funcrion. The comparator should return < 0 if the first argument should be sorted before the second, and > 0 if the first argument should be sorted after the first element. (Same as how `strcmp()` works.) -- **protected** `int` `LinkedList::_size` - Holds the cached size of the list. +- **protected** `int` `TrueLinkedList::_size` - Holds the cached size of the list. -- **protected** `ListNode` `LinkedList::*root` - Holds the root node of the list. +- **protected** `ListNode` `TrueLinkedList::*root` - Holds the root node of the list. -- **protected** `ListNode` `LinkedList::*last` - Holds the last node of the list. +- **protected** `ListNode` `TrueLinkedList::*last` - Holds the last node of the list. -- **protected** `ListNode*` `LinkedList::getNode(int index)` - Returns the `index` node of the list. +- **protected** `ListNode*` `TrueLinkedList::getNode(int index)` - Returns the `index` node of the list. ### Version History * `1.1 (2013-07-20)`: Cache implemented. Getting subsequent objects is now O(N). Before, O(N^2). * `1.0 (2013-07-20)`: Original release -![LinkedList](https://d2weczhvl823v0.cloudfront.net/ivanseidel/LinkedList/trend.png) +![TrueLinkedList](https://d2weczhvl823v0.cloudfront.net/ivanseidel/TrueLinkedList/trend.png) diff --git a/examples/ClassList/ClassList.pde b/examples/ClassList/ClassList.pde index 9a8ea9d..00f2f2f 100644 --- a/examples/ClassList/ClassList.pde +++ b/examples/ClassList/ClassList.pde @@ -1,5 +1,5 @@ /* - LinkedList Example + TrueLinkedList Example Link: http://github.com/ivanseidel/LinkedList Example Created by @@ -9,7 +9,7 @@ Ivan Seidel, github.com/ivanseidel */ -#include +#include // Let's define a new class class Animal { @@ -22,7 +22,7 @@ char catname[]="kitty"; char dogname[]="doggie"; char emuname[]="emu"; -LinkedList myAnimalList = LinkedList(); +TrueLinkedList myAnimalList = TrueLinkedList(); void setup() { diff --git a/examples/SimpleIntegerList/SimpleIntegerList.pde b/examples/SimpleIntegerList/SimpleIntegerList.pde index 1bcbe9c..22dfb33 100644 --- a/examples/SimpleIntegerList/SimpleIntegerList.pde +++ b/examples/SimpleIntegerList/SimpleIntegerList.pde @@ -1,5 +1,5 @@ /* - LinkedList Example + TrueLinkedList Example Link: http://github.com/ivanseidel/LinkedList Example Created by @@ -8,9 +8,9 @@ Edited by: Ivan Seidel, github.com/ivanseidel */ -#include +#include -LinkedList myList = LinkedList(); +TrueLinkedList myList = TrueLinkedList(); void setup() { diff --git a/examples/Sort/Sort.ino b/examples/Sort/Sort.ino index 43df279..bb12d9a 100644 --- a/examples/Sort/Sort.ino +++ b/examples/Sort/Sort.ino @@ -1,5 +1,5 @@ /* - LinkedList Example + TrueLinkedList Example Link: http://github.com/PaulMurrayCbr/LinkedList Forked from: http://github.com/ivanseidel/LinkedList @@ -7,7 +7,7 @@ Paul Murray, github.com/PaulMurrayCbr */ -#include +#include char testString[] = "Lorem ipsum dolor sit amet, \ consectetur adipiscing elit, sed do eiusmod tempor \ @@ -19,7 +19,7 @@ esse cillum dolore eu fugiat nulla pariatur. Excepteur \ sint occaecat cupidatat non proident, sunt in culpa qui \ officia deserunt mollit anim id est laborum."; -LinkedList list; +TrueLinkedList list; void setup() { Serial.begin(9600); diff --git a/keywords.txt b/keywords.txt index 3ae4968..bf839e8 100644 --- a/keywords.txt +++ b/keywords.txt @@ -6,7 +6,7 @@ # Datatypes (KEYWORD1) ####################################### -LinkedList KEYWORD1 +TrueLinkedList KEYWORD1 ListNode KEYWORD1 ####################################### diff --git a/library.json b/library.json index f587cc5..6ce2117 100644 --- a/library.json +++ b/library.json @@ -1,7 +1,7 @@ { - "name": "LinkedList", + "name": "TrueLinkedList", "keywords": "pattern", - "description": "A fully implemented LinkedList (int, float, objects, Lists or Wales) made to work with Arduino projects", + "description": "A fully implemented TrueLinkedList (int, float, objects, Lists or Wales) made to work with Arduino projects", "repository": { "type": "git", @@ -11,7 +11,7 @@ "platforms": "*", "build": { "srcFilter": [ - "+" + "+" ] } } diff --git a/library.properties b/library.properties index 4bdd8ad..99d5633 100644 --- a/library.properties +++ b/library.properties @@ -1,8 +1,8 @@ -name=LinkedList +name=TrueLinkedList version=1.3.1 author=Ivan Seidel maintainer=Ivan Seidel -sentence=A fully implemented LinkedList made to work with Arduino projects +sentence=A fully implemented TrueLinkedList made to work with Arduino projects paragraph=The objective of this library is to create a pattern for projects. If you need to use a List of: int, float, objects, Lists or Wales. This is what you are looking for. category=Data Processing url=https://github.com/ivanseidel/LinkedList diff --git a/tests.cpp b/tests.cpp index 46f4208..170a44e 100644 --- a/tests.cpp +++ b/tests.cpp @@ -1,13 +1,13 @@ //g++ -std=c++14 tests.cpp -o tests && ./tests -#include "LinkedList.h" +#include "TrueLinkedList.h" #include #include void GivenNothingInList_WhenSizeCalled_Returns0() { //Arrange - LinkedList list = LinkedList(); + TrueLinkedList list = TrueLinkedList(); //Act Assert assert(list.size() == 0); @@ -16,7 +16,7 @@ void GivenNothingInList_WhenSizeCalled_Returns0() void GivenNothingInList_WhenAddNodeAtPlace0_ReturnsTrue() { //Arrange - LinkedList list = LinkedList(); + TrueLinkedList list = TrueLinkedList(); //Act Assert assert(list.add(0, 1) == true); @@ -25,7 +25,7 @@ void GivenNothingInList_WhenAddNodeAtPlace0_ReturnsTrue() void GivenOneNodeInList_WhenAddNodeIndexLargerThanListSize_ThenNodeAddedToEndOfList() { //Arrange - LinkedList list = LinkedList(); + TrueLinkedList list = TrueLinkedList(); list.add(1); //Act @@ -41,7 +41,7 @@ void GivenOneNodeInList_WhenAddNodeIndexLargerThanListSize_ThenNodeAddedToEndOfL void GivenThreeNodesInList_WhenSizeCalled_Returns3() { //Arrange - LinkedList list = LinkedList(); + TrueLinkedList list = TrueLinkedList(); list.add(1); list.add(2); list.add(3); @@ -53,7 +53,7 @@ void GivenThreeNodesInList_WhenSizeCalled_Returns3() void GivenNothingInList_WhenUnshiftCalled_ThenListSize1() { //Arrange - LinkedList list = LinkedList(); + TrueLinkedList list = TrueLinkedList(); //Act list.unshift(1); @@ -65,7 +65,7 @@ void GivenNothingInList_WhenUnshiftCalled_ThenListSize1() void GivenOneNodeInList_WhenUnshiftCalled_ThenListSize2() { //Arrange - LinkedList list = LinkedList(); + TrueLinkedList list = TrueLinkedList(); list.add(1); //Act @@ -78,7 +78,7 @@ void GivenOneNodeInList_WhenUnshiftCalled_ThenListSize2() void GivenOneNodeInList_WhenUnshiftCalled_ThenExistingNodeLast() { //Arrange - LinkedList list = LinkedList(); + TrueLinkedList list = TrueLinkedList(); list.add(1); //Act @@ -92,7 +92,7 @@ void GivenOneNodeInList_WhenUnshiftCalled_ThenExistingNodeLast() void GivenNothingInList_WhenSetIsCalled_ThenReturnsFalse() { //Arrange - LinkedList list = LinkedList(); + TrueLinkedList list = TrueLinkedList(); //Act Assert assert(list.set(-1, 1) == false); @@ -103,7 +103,7 @@ void GivenNothingInList_WhenSetIsCalled_ThenReturnsFalse() void GivenThreeNodesInList_WhenSetIsCalledAtPlace1_ThenSecondNodeIsSet() { //Arrange - LinkedList list = LinkedList(); + TrueLinkedList list = TrueLinkedList(); list.add(0); list.add(1); list.add(2); @@ -120,7 +120,7 @@ void GivenThreeNodesInList_WhenSetIsCalledAtPlace1_ThenSecondNodeIsSet() void GivenNothingInList_WhenPopIsCalled_ThenReturnsFalse() { //Arrange - LinkedList list = LinkedList(); + TrueLinkedList list = TrueLinkedList(); //Act Assert assert(list.pop() == false); @@ -129,7 +129,7 @@ void GivenNothingInList_WhenPopIsCalled_ThenReturnsFalse() void GivenTwoNodesInList_WhenPopIsCalled_ThenReturnsLastNode() { //Arrange - LinkedList list = LinkedList(); + TrueLinkedList list = TrueLinkedList(); list.add(0); list.add(1); @@ -140,7 +140,7 @@ void GivenTwoNodesInList_WhenPopIsCalled_ThenReturnsLastNode() void GivenTwoNodesInList_WhenPopIsCalled_ThenListIsShorter() { //Arrange - LinkedList list = LinkedList(); + TrueLinkedList list = TrueLinkedList(); list.add(0); list.add(1); @@ -153,7 +153,7 @@ void GivenTwoNodesInList_WhenPopIsCalled_ThenListIsShorter() void GivenNothingInList_WhenShiftIsCalled_ThenReturnsFalse() { //Arrange - LinkedList list = LinkedList(); + TrueLinkedList list = TrueLinkedList(); //Act Assert assert(list.shift() == false); @@ -162,7 +162,7 @@ void GivenNothingInList_WhenShiftIsCalled_ThenReturnsFalse() void GivenOneNodeInList_WhenShiftIsCalled_ThenReturnsData() { //Arrange - LinkedList list = LinkedList(); + TrueLinkedList list = TrueLinkedList(); list.add(5); //Act Assert @@ -172,7 +172,7 @@ void GivenOneNodeInList_WhenShiftIsCalled_ThenReturnsData() void GivenOneNodeInList_WhenShiftIsCalled_ThenListEmpty() { //Arrange - LinkedList list = LinkedList(); + TrueLinkedList list = TrueLinkedList(); list.add(5); //Act Assert @@ -184,7 +184,7 @@ void GivenOneNodeInList_WhenShiftIsCalled_ThenListEmpty() void GivenThreeNodesInList_WhenShiftIsCalled_ThenReturnsFirstData() { //Arrange - LinkedList list = LinkedList(); + TrueLinkedList list = TrueLinkedList(); list.add(0); list.add(1); list.add(2); @@ -196,7 +196,7 @@ void GivenThreeNodesInList_WhenShiftIsCalled_ThenReturnsFirstData() void GivenThreeNodesInList_WhenShiftIsCalled_ThenListIsShorter() { //Arrange - LinkedList list = LinkedList(); + TrueLinkedList list = TrueLinkedList(); list.add(0); list.add(1); list.add(2); @@ -210,7 +210,7 @@ void GivenThreeNodesInList_WhenShiftIsCalled_ThenListIsShorter() void GivenNothingInList_WhenRemoveIsCalled_ThenFalseIsReturned() { //Arrange - LinkedList list = LinkedList(); + TrueLinkedList list = TrueLinkedList(); //Act Assert assert(list.remove(0) == false); @@ -219,7 +219,7 @@ void GivenNothingInList_WhenRemoveIsCalled_ThenFalseIsReturned() void GivenThreeNodesInList_WhenRemoveIsCalledAtPlace0_ThenFirstNodeDataIsReturned() { //Arrange - LinkedList list = LinkedList(); + TrueLinkedList list = TrueLinkedList(); list.add(0); list.add(1); list.add(2); @@ -231,7 +231,7 @@ void GivenThreeNodesInList_WhenRemoveIsCalledAtPlace0_ThenFirstNodeDataIsReturne void GivenThreeNodesInList_WhenRemoveIsCalledAtPlace2_ThenLastNodeDataIsReturned() { //Arrange - LinkedList list = LinkedList(); + TrueLinkedList list = TrueLinkedList(); list.add(0); list.add(1); list.add(2); @@ -243,7 +243,7 @@ void GivenThreeNodesInList_WhenRemoveIsCalledAtPlace2_ThenLastNodeDataIsReturned void GivenThreeNodesInList_WhenRemoveIsCalled_ThenListIsShorter() { //Arrange - LinkedList list = LinkedList(); + TrueLinkedList list = TrueLinkedList(); list.add(0); list.add(1); list.add(2); @@ -257,7 +257,7 @@ void GivenThreeNodesInList_WhenRemoveIsCalled_ThenListIsShorter() void GivenNothingInList_WhenGetIsCalled_ThenReturnsFalse() { //Arrange - LinkedList list = LinkedList(); + TrueLinkedList list = TrueLinkedList(); //Act Assert assert(list.get(0) == false); @@ -266,7 +266,7 @@ void GivenNothingInList_WhenGetIsCalled_ThenReturnsFalse() void GivenThreeNodesInList_WhenGetIsCalled_ThenReturnsData() { //Arrange - LinkedList list = LinkedList(); + TrueLinkedList list = TrueLinkedList(); list.add(0); list.add(1); list.add(2); @@ -278,7 +278,7 @@ void GivenThreeNodesInList_WhenGetIsCalled_ThenReturnsData() void GivenNothingInList_WhenClearIsCalled_ThenSizeUnchanged() { //Arrange - LinkedList list = LinkedList(); + TrueLinkedList list = TrueLinkedList(); //Act Assert assert(list.size() == 0); @@ -289,7 +289,7 @@ void GivenNothingInList_WhenClearIsCalled_ThenSizeUnchanged() void GivenThreeInList_WhenClearIsCalled_ThenListEmpty() { //Arrange - LinkedList list = LinkedList(); + TrueLinkedList list = TrueLinkedList(); list.add(0); list.add(1); list.add(2);