Shop OBEX P1 Docs P2 Docs Learn Events
Javelin question with linkedlist — Parallax Forums

Javelin question with linkedlist

MightyBeastMightyBeast Posts: 6
edited 2006-10-29 07:43 in General Discussion
i want to use linkedlist and i want the value of each node to be a array of char.
can anybody tell me how would i do this?

do i have to write a linkedlist class with add, remove functions, or can i just import it from library

thanks

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-10-28 07:57
    The stamp.util package has a List object and accompanied methods.
    See page 157 and further in the manual.

    As an alternative there is my implementation of a heap,·list, stack and queue.
    http://tech.groups.yahoo.com/group/JavelinCode/files/Javelin%20Stamp%20IDE/lib/stamp/peripheral/memory/
    If your goal is to store your list in an external memory like eeprom, then my
    implementation is more suitable because it allows you to add and remove from the list
    without copying it back to memory first (which must be when using object references).

    regards peter
  • MightyBeastMightyBeast Posts: 6
    edited 2006-10-28 23:23
    Thank you for you help. I need to save the value in the ram, javelin needs to insert and remove constantly.
    I have one more question, the add method in the list object take parameter of an object, what can i do to make it takes a string or an array of char?

    Thanks
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-10-28 23:34
    You need to extend the LinkedListItem class (which is an abstract class),
    for a String you could define

    package stamp.util;
    public class StringLinkedListItem extends LinkedListItem {
    · private String s = new String();
    · public StringLinkedListItem() {
    ··}

    ··//example of method for reading a char
    · public char charAt(int i) {
    ··· return s.charAt(i);
    · }

    }

    In your main application program,use
    static StringLinkedListItem myItem = new StringLinkedListItem();
    You can of course define any datastructure plus methods that you like.
    The key is to extend the LinkedListItem class, becaus these objects
    can be inserted/removed/added to the LinkedList object.

    regards peter
  • MightyBeastMightyBeast Posts: 6
    edited 2006-10-29 00:16
    import stamp.core.*;
    import stamp.util.*;

    public class ListTest {
    static StringLinkedListItem myItem = new StringLinkedListItem();
    static LinkedList myList = new LinkedList();
    public static void main() {
    myItem.addString("Hello World");
    myList.addItem(myItem);
    System.out.println((myList.getFirst()).getString());
    myList.removeItem(myItem);
    }
    }
    class StringLinkedListItem extends LinkedListItem {
    private String s = new String();
    public StringLinkedListItem() {
    }
    public void addString(String a)
    {
    s = a;
    }
    public String getString()
    {
    return s;
    }
    }


    In the main method, why it doesnt print the value of the first item? and also if i want to add more strings to the linkedlist, i have to create more StringLinkedListItem objects, and just add to the same list right?
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-10-29 06:05
    I put your example code in files and it didn't compile.
    So I changed it a little.

    Here is the application file

    import stamp.core.*;
    import stamp.util.*;
    public class LinkedListTest {
    · static StringLinkedListItem myItem = new StringLinkedListItem();
    · static LinkedList myList = new LinkedList();
    · static StringLinkedListItem k;
    · public static void main() {
    ··· myItem.addString("Hello World");
    ··· myList.addItem(myItem);
    ··· k = (StringLinkedListItem)myList.getFirst();
    ··· System.out.println(k.getString());
    ··· myList.removeItem(myItem);
    · }
    }

    Notice that I cast LinkedListItem to StringLinkedListItem.
    That is required because LinkedListItem does not have a getString() method.
    To add more items to the list, you must indeed first create more StringLinkedListItem objects.

    regards peter
  • MightyBeastMightyBeast Posts: 6
    edited 2006-10-29 07:43
    Peter:

    Thanks a lot for your help. because of that, i was able to make a progress on my project.

    thanks again
Sign In or Register to comment.