Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 30 additions & 30 deletions LinkedList.h
Original file line number Diff line number Diff line change
@@ -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 <stddef.h>

Expand All @@ -23,7 +23,7 @@ struct ListNode
};

template <typename T>
class LinkedList{
class TrueLinkedList{

protected:
int _size;
Expand All @@ -42,27 +42,27 @@ class LinkedList{
ListNode<T>* findEndOfSortedString(ListNode<T> *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);
Expand Down Expand Up @@ -108,9 +108,9 @@ class LinkedList{

};

// Initialize LinkedList with false values
// Initialize TrueLinkedList with false values
template<typename T>
LinkedList<T>::LinkedList()
TrueLinkedList<T>::TrueLinkedList()
{
root=NULL;
last=NULL;
Expand All @@ -123,7 +123,7 @@ LinkedList<T>::LinkedList()

// Clear Nodes and free Memory
template<typename T>
LinkedList<T>::~LinkedList()
TrueLinkedList<T>::~TrueLinkedList()
{
ListNode<T>* tmp;
while(root!=NULL)
Expand All @@ -142,7 +142,7 @@ LinkedList<T>::~LinkedList()
*/

template<typename T>
ListNode<T>* LinkedList<T>::getNode(int index){
ListNode<T>* TrueLinkedList<T>::getNode(int index){

int _pos = 0;
ListNode<T>* current = root;
Expand Down Expand Up @@ -173,19 +173,19 @@ ListNode<T>* LinkedList<T>::getNode(int index){
}

template<typename T>
int LinkedList<T>::size(){
int TrueLinkedList<T>::size(){
return _size;
}

template<typename T>
LinkedList<T>::LinkedList(int sizeIndex, T _t){
TrueLinkedList<T>::TrueLinkedList(int sizeIndex, T _t){
for (int i = 0; i < sizeIndex; i++){
add(_t);
}
}

template<typename T>
bool LinkedList<T>::add(int index, T _t){
bool TrueLinkedList<T>::add(int index, T _t){

if(index >= _size)
return add(_t);
Expand All @@ -206,7 +206,7 @@ bool LinkedList<T>::add(int index, T _t){
}

template<typename T>
bool LinkedList<T>::add(T _t){
bool TrueLinkedList<T>::add(T _t){

ListNode<T> *tmp = new ListNode<T>();
tmp->data = _t;
Expand All @@ -229,7 +229,7 @@ bool LinkedList<T>::add(T _t){
}

template<typename T>
bool LinkedList<T>::unshift(T _t){
bool TrueLinkedList<T>::unshift(T _t){

if(_size == 0)
return add(_t);
Expand All @@ -247,12 +247,12 @@ bool LinkedList<T>::unshift(T _t){


template<typename T>
T& LinkedList<T>::operator[](int index) {
T& TrueLinkedList<T>::operator[](int index) {
return getNode(index)->data;
}

template<typename T>
bool LinkedList<T>::set(int index, T _t){
bool TrueLinkedList<T>::set(int index, T _t){
// Check if index position is in bounds
if(index < 0 || index >= _size)
return false;
Expand All @@ -262,7 +262,7 @@ bool LinkedList<T>::set(int index, T _t){
}

template<typename T>
T LinkedList<T>::pop(){
T TrueLinkedList<T>::pop(){
if(_size <= 0)
return T();

Expand All @@ -288,7 +288,7 @@ T LinkedList<T>::pop(){
}

template<typename T>
T LinkedList<T>::shift(){
T TrueLinkedList<T>::shift(){
if(_size <= 0)
return T();

Expand All @@ -309,7 +309,7 @@ T LinkedList<T>::shift(){
}

template<typename T>
T LinkedList<T>::remove(int index){
T TrueLinkedList<T>::remove(int index){
if (index < 0 || index >= _size)
{
return T();
Expand All @@ -335,20 +335,20 @@ T LinkedList<T>::remove(int index){


template<typename T>
T LinkedList<T>::get(int index){
T TrueLinkedList<T>::get(int index){
ListNode<T> *tmp = getNode(index);

return (tmp ? tmp->data : T());
}

template<typename T>
void LinkedList<T>::clear(){
void TrueLinkedList<T>::clear(){
while(size() > 0)
shift();
}

template<typename T>
void LinkedList<T>::sort(int (*cmp)(T &, T &)){
void TrueLinkedList<T>::sort(int (*cmp)(T &, T &)){
if(_size < 2) return; // trivial case;

for(;;) {
Expand Down Expand Up @@ -408,7 +408,7 @@ void LinkedList<T>::sort(int (*cmp)(T &, T &)){
}

template<typename T>
ListNode<T>* LinkedList<T>::findEndOfSortedString(ListNode<T> *p, int (*cmp)(T &, T &)) {
ListNode<T>* TrueLinkedList<T>::findEndOfSortedString(ListNode<T> *p, int (*cmp)(T &, T &)) {
while(p->next && cmp(p->data, p->next->data) <= 0) {
p = p->next;
}
Expand Down
64 changes: 32 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# LinkedList
# TrueLinkedList

This library was developed targeting **`Arduino`** applications. However, works just great with any C++.

Expand All @@ -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.

Expand All @@ -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<int> myLinkedList = LinkedList<int>();
// Instantiate a TrueLinkedList that will hold 'integer'
TrueLinkedList<int> myTrueLinkedList = TrueLinkedList<int>();

// Or just this
LinkedList<int> myLinkedList;
TrueLinkedList<int> myTrueLinkedList;

// But if you are instantiating a pointer LinkedList...
LinkedList<int> *myLinkedList = new LinkedList<int>();
// But if you are instantiating a pointer TrueLinkedList...
TrueLinkedList<int> *myTrueLinkedList = new TrueLinkedList<int>();

// 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<MyClass> *myLinkedList = new LinkedList<MyClass>();
TrueLinkedList<MyClass> *myTrueLinkedList = new TrueLinkedList<MyClass>();
```

#### Getting the size of the linked list
Expand Down Expand Up @@ -137,47 +137,47 @@ myList.sort(myComparator);

- `ListNode<T>` `*next` - Pointer to the next Node

### `LinkedList` class
### `TrueLinkedList` class

**`boolean` methods returns if succeeded**

- `LinkedList<T>::LinkedList()` - Constructor.
- `TrueLinkedList<T>::TrueLinkedList()` - Constructor.

- `LinkedList<T>::~LinkedList()` - Destructor. Clear Nodes to minimize memory. Does not free pointer memory.
- `TrueLinkedList<T>::~TrueLinkedList()` - Destructor. Clear Nodes to minimize memory. Does not free pointer memory.

- `int` `LinkedList<T>::size()` - Returns the current size of the list.
- `int` `TrueLinkedList<T>::size()` - Returns the current size of the list.

- `bool` `LinkedList<T>::add(T)` - Add element T at the END of the list.
- `bool` `TrueLinkedList<T>::add(T)` - Add element T at the END of the list.

- `bool` `LinkedList<T>::add(int index, T)` - Add element T at `index` of the list.
- `bool` `TrueLinkedList<T>::add(int index, T)` - Add element T at `index` of the list.

- `bool` `LinkedList<T>::unshift(T)` - Add element T at the BEGINNING of the list.
- `bool` `TrueLinkedList<T>::unshift(T)` - Add element T at the BEGINNING of the list.

- `bool` `LinkedList<T>::set(int index, T)` - Set the element at `index` to T.
- `bool` `TrueLinkedList<T>::set(int index, T)` - Set the element at `index` to T.

- `T` `LinkedList<T>::remove(int index)` - Remove element at `index`. Return the removed element. Does not free pointer memory
- `T` `TrueLinkedList<T>::remove(int index)` - Remove element at `index`. Return the removed element. Does not free pointer memory

- `T` `LinkedList<T>::pop()` - Remove the LAST element. Return the removed element.
- `T` `TrueLinkedList<T>::pop()` - Remove the LAST element. Return the removed element.

- `T` `LinkedList<T>::shift()` - Remove the FIRST element. Return the removed element.
- `T` `TrueLinkedList<T>::shift()` - Remove the FIRST element. Return the removed element.

- `T` `LinkedList<T>::get(int index)` - Return the element at `index`.
- `T` `TrueLinkedList<T>::get(int index)` - Return the element at `index`.

- `void` `LinkedList<T>::clear()` - Removes all elements. Does not free pointer memory.
- `void` `TrueLinkedList<T>::clear()` - Removes all elements. Does not free pointer memory.

- `void` `LinkedList<T>::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<T>::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<T>::_size` - Holds the cached size of the list.
- **protected** `int` `TrueLinkedList<T>::_size` - Holds the cached size of the list.

- **protected** `ListNode<T>` `LinkedList<T>::*root` - Holds the root node of the list.
- **protected** `ListNode<T>` `TrueLinkedList<T>::*root` - Holds the root node of the list.

- **protected** `ListNode<T>` `LinkedList<T>::*last` - Holds the last node of the list.
- **protected** `ListNode<T>` `TrueLinkedList<T>::*last` - Holds the last node of the list.

- **protected** `ListNode<T>*` `LinkedList<T>::getNode(int index)` - Returns the `index` node of the list.
- **protected** `ListNode<T>*` `TrueLinkedList<T>::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)
6 changes: 3 additions & 3 deletions examples/ClassList/ClassList.pde
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
LinkedList Example
TrueLinkedList Example
Link: http://github.com/ivanseidel/LinkedList

Example Created by
Expand All @@ -9,7 +9,7 @@
Ivan Seidel, github.com/ivanseidel
*/

#include <LinkedList.h>
#include <TrueLinkedList.h>

// Let's define a new class
class Animal {
Expand All @@ -22,7 +22,7 @@ char catname[]="kitty";
char dogname[]="doggie";
char emuname[]="emu";

LinkedList<Animal*> myAnimalList = LinkedList<Animal*>();
TrueLinkedList<Animal*> myAnimalList = TrueLinkedList<Animal*>();

void setup()
{
Expand Down
6 changes: 3 additions & 3 deletions examples/SimpleIntegerList/SimpleIntegerList.pde
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
LinkedList Example
TrueLinkedList Example
Link: http://github.com/ivanseidel/LinkedList

Example Created by
Expand All @@ -8,9 +8,9 @@
Edited by:
Ivan Seidel, github.com/ivanseidel
*/
#include <LinkedList.h>
#include <TrueLinkedList.h>

LinkedList<int> myList = LinkedList<int>();
TrueLinkedList<int> myList = TrueLinkedList<int>();

void setup()
{
Expand Down
6 changes: 3 additions & 3 deletions examples/Sort/Sort.ino
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/*
LinkedList Example
TrueLinkedList Example
Link: http://github.com/PaulMurrayCbr/LinkedList
Forked from: http://github.com/ivanseidel/LinkedList

Example Created by
Paul Murray, github.com/PaulMurrayCbr
*/

#include <LinkedList.h>
#include <TrueLinkedList.h>

char testString[] = "Lorem ipsum dolor sit amet, \
consectetur adipiscing elit, sed do eiusmod tempor \
Expand All @@ -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<char *> list;
TrueLinkedList<char *> list;

void setup() {
Serial.begin(9600);
Expand Down
Loading