linked list advice needed

hi

I was hoping someone might be able to advise me please

I would like to create a dynamic list of numbers
I need to be able to add numbers to the list, remove a number from the list and report the size of the list
I tried this library http://forum.arduino.cc/index.php?topic=178546.0

but i don’t think it does all what i want. but i may be wrong (as usual :wink: ).

in the example code below i have set to read from the serial monitor

so my scenario might be

if i press b it add 98 to the list (the sketch does that )
if i press n it adds 110 to the list (the sketch does that )
i might then press b again which adds another 98 to the list (the sketch does that )
if i press m it adds 109 to the list (the sketch does that )

so i now have a list containing four numbers 98 110 98 and 109

the problem comes with removing them i was gonna set up another 3 keys to remove them

so for example i might press k which would remove 109 from the list leaving 98 110 98
i might then press h which would remove one of the 98’s from the list leaving 98 and 110
I don’t think this library does this with out specifying an index or remove first or last
i know i can search the list for a number but then i can’t report the index to delete it

has anyone know what i should be using to achieve this,
I presume i could go down the array route ??? but it would be nice to use a list library

thanking you in advance

#include <LinkedList.h>
int listSize = 0;
int inByte = 0;   // for incoming serial data
LinkedList<int> myList = LinkedList<int>();

void setup() {
  Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
  Serial.println ("use key b n and m to add to stack");
}

void loop() {
  if (Serial.available() > 0) {
    inByte = Serial.read();

    if (inByte != 10)
    {//is anything but return
      Serial.println(" ");
      Serial.print("I received: ");
      Serial.println(inByte, DEC);

      if (inByte == 98 || inByte == 110 || inByte == 109 )
      {//is b n m

        if (inByte == 98 || inByte == 110 || inByte == 109)
        { 
          myList.add(inByte);
          listSize = myList.size();
          Serial.print("list size ");
          Serial.println(listSize);
        }
        printList();
      }//end of bnm
    }// end of if (inByte != 10)
  }//end of if available
}//end of loop



void printList(){
  for (int h = 0; h < listSize; h++) {
    int val = myList.get(h);
    Serial.print("val = ");
    Serial.println(val);
  }//eo for
}// eo print list

I don't know that library. I would be very sceptical of any "black box" that purports to manage memory dynamically on a device as limited as an Arduino.

I would much prefer to pre allocate an array (or equivalent) with enough space for the maximum number of elements you expect.

Can you explain what you want your list to do? Maybe there is another way?

...R

Search the list for the element to remove, then call the remove() method.

  for (int h = 0; h < listSize; h++) {
    int val = myList.get(h);
    if (val == 98) {
      myList.remove(h);
      break;
    }
  }//eo for

BTW linked lists are commonly implemented using pointers and dynamic memory, but arrays can be used also.

(inByte == 98 || inByte == 110 || inByte == 109 )

I had to look those values up

I didn't have think about this.

(inByte == 'b' || inByte == 'n' || inByte == 'm' )
      if (inByte == 98 || inByte == 110 || inByte == 109 )
      {//is b n m

        if (inByte == 98 || inByte == 110 || inByte == 109)
        {

Might be a bit overkill to check twice…

Regards,

Brad
KF7FER

hi everyone
thank you all so much for your replies

to david i would like to quote my post

"but i don’t think it does all what i want. but i may be wrong (as usual :wink: "

but i may be wrong (as usual :wink: this is how my life is

thanks again

The problem is, that library probably includes a lot of stuff you don't need, and assumes you have unlimited amounts of memory, which you don't.

I'm containing it to about 6 numbers i presume the limited memory will be ok for this as i overwrite the list numbers

michaelmartin2758: i presume the limited memory will be ok for this as i overwrite the list numbers

I understand that the library logically overwrites the numbers but do you KNOW that it physically overwrites the numbers? Does it use the same memory locations or does it just ignore the old location and find some other unused memory location with a view to tidying up the garbage sometime later - if you haven't run out of memory first.

Is there any advantage in using a linked-list library for 6 numbers?

...R