Code Monkey home page Code Monkey logo

linkedlist's Introduction

LinkedList

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

Implementing a buffer for objects takes time. If we are not in the mood, we just create an array[1000] with enough size.

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.

With a simple but powerful caching algorithm, you can get subsequent objects much faster than usual. Tested without any problems with Lists bigger than 2000 members.

Installation

  1. Download the Latest release from gitHub.
  2. Unzip and modify the Folder name to "LinkedList" (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.

If you are here, because another Library requires this class, just don't waste time reading bellow. Install and ready.

Tests

cd to this directory and run g++ -std=c++14 extras/test/tests.cpp -o tests && ./tests


Getting started

The LinkedList class

In case you don't know what a LinkedList is and what it's used for, take a quick look at Wikipedia::LinkedList before continuing.

To declare a LinkedList object

// Instantiate a LinkedList that will hold 'integer'
LinkedList<int> myLinkedList = LinkedList<int>();

// Or just this
LinkedList<int> myLinkedList;

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

// If you want a LinkedList with any other type such as 'MyClass'
// Make sure you call delete(MyClass) when you remove!
LinkedList<MyClass> *myLinkedList = new LinkedList<MyClass>();

Getting the size of the linked list

// To get the size of a linked list, make use of the size() method
int theSize = myList.size();

// Notice that if it's pointer to the linked list, you should use -> instead
int theSize = myList->size();

Adding elements

// add(obj) method will insert at the END of the list
myList.add(myObject);

// add(index, obj) method will try to insert the object at the specified index
myList.add(0, myObject); // Add at the beginning
myList.add(3, myObject); // Add at index 3

// unshift(obj) method will insert the object at the beginning
myList.unshift(myObject);

Getting elements

// get(index) will return the element at index
// (notice that the start element is 0, not 1)

// Get the FIRST element
myObject = myList.get(0);

// Get the third element
myObject = myList.get(2);

// Get the LAST element
myObject = myList.get(myList.size() - 1);

Changing elements

// set(index, obj) method will change the object at index to obj

// Change the first element to myObject
myList.set(0, myObject);

// Change the third element to myObject
myList.set(2, myObject);

// Change the LAST element of the list
myList.set(myList.size() - 1, myObject);

Deleting elements

// remove(index) will remove and return the element at index

// Remove the first object
myList.remove(0);

// Get and Delete the third element
myDeletedObject = myList.remove(2);

// pop() will remove and return the LAST element
myDeletedObject = myList.pop();

// shift() will remove and return the FIRST element
myDeletedObject = myList.shift();

// clear() will erase the entire list, leaving it with 0 elements
// NOTE: Clear wont DELETE/FREE memory from Pointers, if you
// are using Classes/Poiners, manualy delete and free those.
myList.clear();

Sorting elements

// Sort using a comparator function
myList.sort(myComparator);

Library Reference

ListNode struct

  • T ListNode::data - The object data

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

LinkedList class

boolean methods returns if succeeded

  • LinkedList<T>::LinkedList() - Constructor.

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

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

  • bool LinkedList<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 LinkedList<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.

  • T LinkedList<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 LinkedList<T>::shift() - Remove the FIRST element. Return the removed element.

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

  • void LinkedList<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.)

  • protected int LinkedList<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> LinkedList<T>::*last - Holds the last node of the list.

  • protected ListNode<T>* LinkedList<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

linkedlist's People

Contributors

cmooney3 avatar iisfaq avatar ivankravets avatar ivanseidel avatar jasonthomasdata avatar jwd83 avatar nitrofmtl avatar ogatatsu avatar paulmurraycbr avatar per1234 avatar petermitrano avatar rajeeves avatar ruigaspar avatar sandeepmistry avatar sidoh avatar siemanko avatar thirtythreeforty avatar vijay-jha avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

linkedlist's Issues

2 of 3 examples do not open in Arduino IDE 2.0

Just FYI, two of the examples do not even open an IDE window when you try to select them:
ClassList
SimpleList

The sort example works fine.
in the latest 1.8.15 version they all open.

Add unit tests and continuous integration

Hello,

I'm considering this library as a dependency for an application.
Althought I'd prefer to rely on a library which have unit tests.

You can find some links dealing with continuous integration and Arduino
https://github.com/adafruit/travis-ci-arduino
https://www.pololu.com/blog/654/continuous-testing-for-arduino-libraries-using-platformio-and-travis-ci
(they are using http://platformio.org/ )

Kind regards

PS : see also https://docs.travis-ci.com/user/integration/platformio/

https://github.com/platformio/platformio-remote-unit-testing-example

See also https://github.com/mmurdoch/arduinounit/

See discussion mmurdoch/arduinounit#63

A strange problem.

LinkedList<int> mMagBiasX = LinkedList<int>();
void magcalMPU9250_2() {
	int sample_count;
	Serial.println("Mag Calibration ");
	mMagBiasX.clear();
	for(;;){

//		Serial.println(mag_temp[0]);
		int t = 0;
		//memcpy(&t,&mag_temp[0],sizeof(int16_t));		#2
		t = -1;	//t =mag_temp[0];                                          #1
		mMagBiasX.add(t);
// 		delay(135);  // at 8 Hz ODR, new mag data is available every 125 ms
		delay(16);
 		//if(calibBut.uniquePress())
 		//	break;
 		Serial.println(sample_count);
 		Serial.println(mMagBiasX.size());
 	}
}

Just copy this function to SimpleIntegerList.pde . and call it from setup .Then the loop will stuck at about the 176th call (Arduino uno).
and comment # 1 line out ,u will see the loop work again . I don't know why .Maybe relative to the memory ,so maybe it is arduino false . I don't know and give up .Just hope you notice this.

2 bugs in the remove()

There are 2 bugs in your remove method

template<typename T>
T LinkedList<T>::remove(int index){
    if(index < 0 || index >= _size)
        return T();

    if(index == 0)
        return shift();

    if(index - 1 == _size)
        return pop();

    ListNode<T> *tmp = getNode(index - 1);
    ListNode<T> *toDelete = tmp->next;
    tmp->next = tmp->next->next;
    delete(toDelete);
    _size--;
    isCached = false;

    return T();
}

Bug 1

    if(index - 1 == _size)
        return pop();

This can never work!

For example you have 5 items in a list, and you want to remove the last item.

index =4 for the 5th element in the list.

Your code will never work
4-1 = 3, and comparing 3 to 5 don't work

So you need to change that to

    if(index == _size-1)
        return pop();

Bug 2

At the end of the method you have this statement

return T();

This creates a new instance of T instead of returning the current T for the node you are deleting. Here is my corrected code.

        ListNode<T> *tmp = getNode(index - 1);
    ListNode<T> *toDelete = tmp->next;
-->        T ret = toDelete->data;
    tmp->next = tmp->next->next;
    delete(toDelete);
    _size--;
    isCached = false;
--> return ret;

The new corrected code

template<typename T>
T LinkedList<T>::remove(int index){
    if (index < 0 || index >= _size)
    {
        return T();
    }

    if(index == 0)
        return shift();

    if (index == _size-1)
    {
        return pop();
    }

    ListNode<T> *tmp = getNode(index - 1);
    ListNode<T> *toDelete = tmp->next;
    T ret = toDelete->data;
    tmp->next = tmp->next->next;
    delete(toDelete);
    _size--;
    isCached = false;
    return ret;
    //return T();
}

Chris

iostream compilation error on Arduino Mega

/home/luana/Arduino/libraries/LinkedList/tests.cpp:5:10: fatal error: iostream: No such file or directory
 #include <iostream>
          ^~~~~~~~~~
compilation terminated.
exit status 1

Error compiling for Arduino Mega or Mega 2560 board. It just works removing this file.

Compile error for Arduino Nano

Hello,
I'm trying to use your library on Arduino IDE 1.8.10 with an Arduino Nano Board but I get the following error message :

`Arduino: 1.8.10 (Windows 7), Board: "Arduino Nano, ATmega328P (Old Bootloader)"

C:\Program Files (x86)\Arduino\arduino-builder -dump-prefs -logger=machine -hardware C:\Program Files (x86)\Arduino\hardware -hardware C:\Users\EESM\AppData\Local\Arduino15\packages -tools C:\Program Files (x86)\Arduino\tools-builder -tools C:\Program Files (x86)\Arduino\hardware\tools\avr -tools C:\Users\EESM\AppData\Local\Arduino15\packages -built-in-libraries C:\Program Files (x86)\Arduino\libraries -libraries C:\Users\EESM\Documents\Arduino\libraries -fqbn=arduino:avr:nano:cpu=atmega328old -vid-pid=1A86_7523 -ide-version=10810 -build-path C:\Users\EESM\AppData\Local\Temp\arduino_build_776334 -warnings=none -build-cache C:\Users\EESM\AppData\Local\Temp\arduino_cache_884885 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.arduinoOTA.path=C:\Users\EESM\AppData\Local\Arduino15\packages\arduino\tools\arduinoOTA\1.3.0 -prefs=runtime.tools.arduinoOTA-1.3.0.path=C:\Users\EESM\AppData\Local\Arduino15\packages\arduino\tools\arduinoOTA\1.3.0 -prefs=runtime.tools.avrdude.path=C:\Users\EESM\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17 -prefs=runtime.tools.avrdude-6.3.0-arduino17.path=C:\Users\EESM\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17 -prefs=runtime.tools.avr-gcc.path=C:\Users\EESM\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino5 -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino5.path=C:\Users\EESM\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino5 -verbose C:\Users\EESM\Documents\Arduino\test_arg_list_linked\test_arg_list_linked.ino
C:\Program Files (x86)\Arduino\arduino-builder -compile -logger=machine -hardware C:\Program Files (x86)\Arduino\hardware -hardware C:\Users\EESM\AppData\Local\Arduino15\packages -tools C:\Program Files (x86)\Arduino\tools-builder -tools C:\Program Files (x86)\Arduino\hardware\tools\avr -tools C:\Users\EESM\AppData\Local\Arduino15\packages -built-in-libraries C:\Program Files (x86)\Arduino\libraries -libraries C:\Users\EESM\Documents\Arduino\libraries -fqbn=arduino:avr:nano:cpu=atmega328old -vid-pid=1A86_7523 -ide-version=10810 -build-path C:\Users\EESM\AppData\Local\Temp\arduino_build_776334 -warnings=none -build-cache C:\Users\EESM\AppData\Local\Temp\arduino_cache_884885 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.arduinoOTA.path=C:\Users\EESM\AppData\Local\Arduino15\packages\arduino\tools\arduinoOTA\1.3.0 -prefs=runtime.tools.arduinoOTA-1.3.0.path=C:\Users\EESM\AppData\Local\Arduino15\packages\arduino\tools\arduinoOTA\1.3.0 -prefs=runtime.tools.avrdude.path=C:\Users\EESM\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17 -prefs=runtime.tools.avrdude-6.3.0-arduino17.path=C:\Users\EESM\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17 -prefs=runtime.tools.avr-gcc.path=C:\Users\EESM\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino5 -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino5.path=C:\Users\EESM\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino5 -verbose C:\Users\EESM\Documents\Arduino\test_arg_list_linked\test_arg_list_linked.ino
Using board 'nano' from platform in folder: C:\Users\EESM\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.2
Using core 'arduino' from platform in folder: C:\Users\EESM\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.2
Detecting libraries used...
"C:\Users\EESM\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino5/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10810 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR "-IC:\Users\EESM\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.2\cores\arduino" "-IC:\Users\EESM\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.2\variants\eightanaloginputs" "C:\Users\EESM\AppData\Local\Temp\arduino_build_776334\sketch\test_arg_list_linked.ino.cpp" -o nul
Alternatives for LinkedList.h: [[email protected]]
ResolveLibrary(LinkedList.h)
-> candidates: [[email protected]]
"C:\Users\EESM\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino5/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10810 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR "-IC:\Users\EESM\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.2\cores\arduino" "-IC:\Users\EESM\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.2\variants\eightanaloginputs" "-IC:\Users\EESM\Documents\Arduino\libraries\LinkedList" "C:\Users\EESM\AppData\Local\Temp\arduino_build_776334\sketch\test_arg_list_linked.ino.cpp" -o nul
"C:\Users\EESM\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino5/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10810 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR "-IC:\Users\EESM\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.2\cores\arduino" "-IC:\Users\EESM\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.2\variants\eightanaloginputs" "-IC:\Users\EESM\Documents\Arduino\libraries\LinkedList" "C:\Users\EESM\Documents\Arduino\libraries\LinkedList\tests.cpp" -o nul
Alternatives for iostream: []
ResolveLibrary(iostream)
-> candidates: []
C:\Users\EESM\Documents\Arduino\libraries\LinkedList\tests.cpp:5:10: fatal error: iostream: No such file or directory

#include

      ^~~~~~~~~~

compilation terminated.

Multiple libraries were found for "LinkedList.h"
Used: C:\Users\EESM\Documents\Arduino\libraries\LinkedList
Using library LinkedList at version 1.3.1 in folder: C:\Users\EESM\Documents\Arduino\libraries\LinkedList
exit status 1
Error compiling for board Arduino Nano.`

Type requires no arg constructor

Nowhere in the documentation it mentions that the type T must have a no-arg constructor for this to work.

Please specify in the documentation explicitly, otherwise figuring it out may cost a lot of time to the developer like it did for me.

Type mismatch error

Arduino\libraries\LinkedList/LinkedList.h:160:9: error: cannot convert 'bool' to 'ListNode*' in return

return false;

But return type must be ListNode not boolean.

Compile errors with 1.8.10 arduino ide

Hello,
I get these compilation errors using your lib in Arduino 1.8.10 ide:

home/user/Arduino/libraries/LinkedList/LinkedList.h: In instantiation of 'ListNode* LinkedList::getNode(int) [with T = Transition*]':
/home/user/Arduino/libraries/LinkedList/LinkedList.h:314:28: required from 'T LinkedList::get(int) [with T = Transition*]'
/home/user/Arduino/libraries/StateMachine/src/State.h:83:32: required from here
/home/user/Arduino/libraries/LinkedList/LinkedList.h:160:9: error: cannot convert 'bool' to 'ListNode<Transition*>' in return
return false;
^~~~~
/home/user/Arduino/libraries/LinkedList/LinkedList.h: In instantiation of 'ListNode
LinkedList::getNode(int) [with T = State*]':
/home/user/Arduino/libraries/LinkedList/LinkedList.h:314:28: required from 'T LinkedList::get(int) [with T = State*]'
/home/user/Arduino/libraries/StateMachine/src/StateMachine.h:54:41: required from here
/home/user/Arduino/libraries/LinkedList/LinkedList.h:160:9: error: cannot convert 'bool' to 'ListNode<State*>*' in return

With Arduino ide 1.8.9 these errors are warnings and I can compile but with the new version 1.8.10 I can't
Thank you very much and best regards

'class LinkedList<int>' has no member named 'add'

When trying to compile your example (SimpleIntegerList.pde) in Arduino IDE 1.8.16 with a Nano RP2040 as target I get the following error:

/Users/(...)/Dev/arduino/sketch_sep22b/sketch_sep22b.ino: In function 'void setup()':
sketch_sep22b:26:10: error: 'class LinkedList' has no member named 'add'
myList.add(n);
^~~
sketch_sep22b:27:10: error: 'class LinkedList' has no member named 'add'
myList.add(0);
^~~
sketch_sep22b:28:10: error: 'class LinkedList' has no member named 'add'
myList.add(l);
^~~
sketch_sep22b:29:10: error: 'class LinkedList' has no member named 'add'
myList.add(17);
^~~
sketch_sep22b:30:10: error: 'class LinkedList' has no member named 'add'
myList.add(k);
^~~
sketch_sep22b:31:10: error: 'class LinkedList' has no member named 'add'
myList.add(m);
^~~
/Users/adias/Dev/arduino/sketch_sep22b/sketch_sep22b.ino: In function 'void loop()':
sketch_sep22b:36:25: error: 'class LinkedList' has no member named 'size'
int listSize = myList.size();
^~~~
sketch_sep22b:46:22: error: 'class LinkedList' has no member named 'get'
int val = myList.get(h);
^~~
exit status 1
'class LinkedList' has no member named 'add'

Any ideas of what I am doing wrong?

Clear() vs Remove

Thanks for the library - it is great.

A quick tip though (maybe useful to put into the readme) is that using clear() can be dangerous to use if you are using a class as the list data compared to a int for example.

The reason is that if you dynamically create your class such as in your ClassList example

Animal *dog = new Animal();
dog->name = dogname;
dog->isMammal = true;

When you use the clear() method the 'dog' object is not freed up and consumes memory.

A better approach when using a class for the list data is like this

while (myAnimalList.size() > 0)
{
delete myAnimalList.remove(0);
}

This then frees up the dynamic object as it is removed from the list.

This may help someone who is adding/deleting objects at runtime.

Chris

Arduino auto-reboot 😄

Simple code below doing auto-reboot of Arduino:

#include <Arduino.h>
#include <DEVICE.h>
#include <elapsedMillis.h>
elapsedMillis testTimer;

void setup()
{
  
  Serial.begin(115200);
  Serial.println("Working again");

}

void loop()
{

  if(testTimer > 50) 
  {
    LinkedList<byte> * packet = new LinkedList<byte>();
    packet->add(0xF1);
    packet->add(0x00);
    packet->add(0x01);
    packet->add(0x00);
    packet->add(0x0D);
    packet->clear();
    testTimer = 0;
  }
}
--- Available filters and text transformations: colorize, debug, default, direct, hexlify, log2file, nocontrol, printable, send_on_enter, time
--- More details at https://bit.ly/pio-monitor-filters
--- Miniterm on COM3  115200,8,N,1 ---
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
Working again
Working again
Working again
Working again
Working again
Working again
Working again
Working again

has no member named 'length'

When trying to compile an esp32 wroom with arduino uno with the library AsyncTCP I have this error:

Documents\Arduino\libraries\ESPAsyncWebServer-master\src/AsyncEventSource.h:89:59: error: 'const class LinkedList<AsyncEventSourceMessage*>' has no member named 'length'
89 | size_t packetsWaiting() const { return _messageQueue.length(); }
| ^~~~~~

thanks for your help

it does not work

LinkedList list = LinkedList();
list.add(1);
list.add(2);
list.add(3);
list.get(0); //will be 3
list.get(1); //will be 2
list.get(2); //will be 0

WHAT IS THE FUCK???

Added swap(index, index1) method

Added swap method as follow:

	/*
		Swap objects at index and index1;
	*/
	virtual bool swap(int index, int index1);

template<typename T>
bool LinkedList<T>::swap(int index, int index1){
	// Check if index position is in bounds
	if(index < 0 || index >= _size)
		return false;
	if(index1 < 0 || index1 >= _size)
		return false;

	T _t = getNode(index)->data;
	getNode(index)->data = getNode(index1)->data;
	getNode(index1)->data = _t;
	return true;
}

Install Instructions Are Incorrect

The current install instructions need to have an additional step included
when used with Arduino (at least 1.8.13). Without this step, the test.cpp
file will be included when compiling a script that includes LinkedList.
The result will be a second "main()" function, from test.cpp, included in
the compile which will override the correct "main()" function for the chosen
board / MCU. This means the normal "setup()" and "loop()" functions will NOT
run making it appear like LinkedList has trashed the program or the program
cannot work.

The solution is quite simple. After the third step of pasting the modified
folder to the Library folder, add:

3a - In the LinkedList Library folder, rename test.cpp to test.cpp_hidden

So the resulting steps are:

  1. Download the Latest release from gitHub.
  2. Unzip and modify the Folder name to "LinkedList" (Remove the '-version')
  3. Paste the modified folder on your Library folder (On your Libraries folder inside Sketchbooks or Arduino software).
    3a. In the LinkedList Library folder, rename test.cpp to test.cpp_hidden
  4. Reopen the Arduino software.

Linked_listC++

class linked_list{
struct node{//عملنا العقدة حقنا
int data;
node* next;};
node* head=NULL;
public:
// دالة اضافة عنصر الى اخر القائمة
void apend_to_last(int val){
node* newnode=new node;
newnode->data=val;
newnode->next=NULL;
if(head==NULL)
head=newnode;
else{
node* temp=head;
while(temp->next!=NULL)
temp=temp->next;
temp->next=newnode;}}
//دالة البحث عن القيمة التي ادخلها المستخدم وحذفها
void delete_by_val(int val){
if(head==NULL)
{cout<<"no elements in linked list...\n";
return;}

nodetemp;
temp=head;
if(temp->data==val){
head=head->next;
delete temp;}
else{
node
prev,temp;
prev=temp=head;
while(temp!=NULL&&temp->data!=val){
prev=temp;
temp=temp->next;}
if(temp==NULL)
{cout<<"not found \n";
return;}
else{
prev->next=temp->next;
delete temp;}}}
//دالة عرض عناصر القائمة المتصلة
void display(){
if(head==NULL){
cout<<"no element in linked list...\n";
return;}
node
temp=head;
while(temp!=NULL){
cout<data<<"\t";
temp=temp->next;}}
//دالة اضافة قيمة معينة الى موقع معين
void insrt_by_pos(int pos,int val){
node* newnode=new node;
newnode->data=val;
newnode->next=NULL;
if(pos==0){
newnode->next=head;
head=newnode;}
else{
nodetemp=head;
for(int i=0;i<pos-1&&temp->next!=NULL;i++ )
temp=temp->next;
newnode->next=temp->next;
temp->next=newnode;}}
//دالة حذف قيمة في موقع معين
void delete_by_pos(int pos){
if(head==NULL){
cout<<"no element in linked list..\n";
return;}
if(pos==0){
node
temp=head;
head=head->next;
delete temp;}
else {
nodetemp=head;
for(int i=0;i<pos-1&&temp->next!=NULL;i++)
temp=temp->next;
if(temp->next==NULL){
cout<<"ERORR enter corect position...\n";
return;}
node
temp2=temp->next;
temp->next=temp->next->next;
delete temp2;}}
//دالة عرض القائمة بالعكس
void revers(){
if(head==NULL){
cout<<"no element in linked list...\n";
return;}
node* prev=NULL;
nodecurr=head;
node
next=NULL;
while(curr!=NULL){
next=curr->next;
curr->next=prev;
prev=curr;
curr=next;}
head=prev;}};

No output to Serial Output

Hi,

I have commented #include iostream and last line std::cout of test.cpp. Example programs with the library are compiling without error but when running the program, there is no output in serial monitor.

I am using Arduino Mega, 1.8.13

Help pls!

This library overlaps with ESPAsyncWebServer

Unfortunately.
That library is much more popular, so it's needed to make changes here.

src\esp32_AP.cpp:36:52: error: no matching function for call to 'LinkedList<Error*>::LinkedList()'
LinkedList<Error*> ErrorsList = LinkedList<Error*>();
^
In file included from C:/Users/Nelly/.platformio/lib/ESPAsyncWebServer-esphome/src/ESPAsyncWebServer.h:29:0,
from src\esp32_AP.cpp:4:

And so on ...

Is LinkedList usable outside of the Arduino platform (e.g. in a regular desktop application)?

Hello! I love LinkedList on my Arduino Zero. I am using it for some sort of "messaging queue" and even if the LinkedList might not be the smartest way to implement such a queue, it currently works really well.

However: in order to be able to create a "desktop application", that can simulate my Arduino-based device, I decoupled all my hardware adapters and I am working on doing so for the Serial port access, too. I also figured, that the LinkedList is meant for use on Arduino.

Hence my question is: would this library compile if I were to e.g. build a desktop application with it?

Sure, it might have some disadvantages over C++'s own implementation, but at least it works on the Arduino device. Would I have to "replace" it in a desktop application (say for MacOS or Windows or Linux)? Or could my application just keep using the LinkedList you provided?

Including LinkedList.h breaks Nano 33 IoT setup method

I wrote a simple sketch that essentially lets the buildin LED blink through the loop method. At the moment I include LinkedList.h evering breaks and the Arduino does not exit setup-method (visible via the not flashing LED).
My test code:

#include<LinkedList.h>

void setup() {
// put your setup code here, to run once:
pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
}

feature request: clear + free objects

LinkedList<obj*>* list = new LL():

list->clear(true); does a normal clear but also calls delete() on each item in the list.

An enum of CLEAR_TYPE { DEFAULT = 0, DELETE, FREE } would be nice so if obj it calls delete, if malloc'd char* it calls free.

non-virtual deconstructor

-wall using this class gives me a message about calling delete on a linkedlist<obj*>* about a non-virtual deconstructor which leads to a undetermined state

it is hard to judge get() success or not?

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

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

}
no matter success or not, an instance of T is returned, so how to judge?

Arduino Nano 33 Incompatibility

Library compiles properly with Arduino Uno but does not compile with Arduino Nano 33 BLE. The following error message is given for the example sketch SimpleIntegerList:

SimpleIntegerList:26:9: error: 'class LinkedList' has no member named 'add'

Arduino: 1.8.12 (Windows 10), Board: "Arduino Nano 33 BLE"

Is it thread safe ?

I am thinking of using this as display controller where each item is a page , and other tasks will update the items and add it remove them ...

Is this a good lib to use

Thread Protection

I have been using a modified version of this library from another developer for some time now. The modified version seems to have the same usage syntax as this library (as expected) so I am about to transition from using the modified version to using this library. The other version I am referencing is heavily inspired by this library you have written so...thanks for making an awesome library.

The main reason for my wanting to leave the modified version and come to this one is that the modified version does not have thread protection. In one of my projects, there is a method that causes my device to crash because that method is running in parallel with another method that is accessing the same instance of the list. If both methods attempt to add and/or remove items at the same time, instant crash.

Does this library provide protection against issues like that?

Example won't compile for Arduino Portenta

Compile command: arduino-cli compile ~/Arduino/libraries/LinkedList/examples/ClassList/ClassList.pde -b arduino:mbed_portenta:envie_m7 --libraries ~/Arduino/libraries/
Errors:
"
/home/cellula/Arduino/libraries/LinkedList/examples/ClassList/ClassList.ino: In function 'void setup()':
/home/cellula/Arduino/libraries/LinkedList/examples/ClassList/ClassList.ino:49:15: error: 'class LinkedList<Animal*>' has no member named 'add'
myAnimalList.add(cat);
^~~
/home/cellula/Arduino/libraries/LinkedList/examples/ClassList/ClassList.ino:50:15: error: 'class LinkedList<Animal*>' has no member named 'add'
myAnimalList.add(emu);
^~~
/home/cellula/Arduino/libraries/LinkedList/examples/ClassList/ClassList.ino:51:15: error: 'class LinkedList<Animal*>' has no member named 'add'
myAnimalList.add(dog);
^~~
/home/cellula/Arduino/libraries/LinkedList/examples/ClassList/ClassList.ino: In function 'void loop()':
/home/cellula/Arduino/libraries/LinkedList/examples/ClassList/ClassList.ino:57:28: error: 'class LinkedList<Animal*>' has no member named 'size'
Serial.print(myAnimalList.size());
^~~~
/home/cellula/Arduino/libraries/LinkedList/examples/ClassList/ClassList.ino:62:34: error: 'class LinkedList<Animal*>' has no member named 'size'
for(int i = 0; i < myAnimalList.size(); i++){
^~~~
/home/cellula/Arduino/libraries/LinkedList/examples/ClassList/ClassList.ino:65:25: error: 'class LinkedList<Animal*>' has no member named 'get'
animal = myAnimalList.get(i);
^~~

Error during build: exit status 1
"

Arduino CLI version 0.20.2
Arduino Portenta core version 2.6.1
LinkedList library version 1.3.2

Compiles for Arduino Mega after deleting tests.cpp.

Arduino IDE 1.6.9 reports a warning : converting 'false' to pointer type 'ListNode<MyObject*>*' [-Wconversion-null]

MyObject being whatever I try to store.

I believe I have found the culprit and would like to propose a commit, but I have never done that on GitHub and don't even know if I'm allowed to contribute to this project.

Anyway, the proposed fix is :
file LinkedList.h, method template ListNode* LinkedList::getNode(int index)
line 160 :

- return false; 
+ return 0;

Shall someone take this in account or is there a way I can propose formally this change ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.