Results 1 to 6 of 6
  1. #1
    Dr Gonzo is offline Member
    Join Date
    Dec 2008
    Posts
    3
    Rep Power
    0

    Default Help with Doubly linked list

    need some help writing an add method for a node class that will transverse the list from front to back and from back to front, inserting objects containing integers assending or decending I can go forward.

    public void add(int x)
    {
    NumNode node = new NumNode(x);
    NumNode current;

    if(list == null)
    list = node;
    else
    {
    current = list;
    while (current.next != null)
    current = current.next;
    current.next = node;
    }
    }

  2. #2
    carlodelmundo is offline Member
    Join Date
    Aug 2008
    Posts
    31
    Rep Power
    0

    Default

    Hi Dr Gonzo,

    You're off to a great start! It looks like you still need a little more work though.

    You need a total of three classes:

    1.) DoublyListNode Class
    2.) DoublyLinkedList Class
    3.) DoublyLinkedList Tester

    In the ListNode class you need the following instance variables:

    private object value;
    private DoublyListNode next;
    private DoublyListNode prev;

    The instance variables 'next' and 'prev' are what makes your linked list able to traverse back and fourth.

    The LinkedList Class contains the following variables:

    private DoublyListNode headerNode;
    private DoublyListNode trailerNode;

    These "trailer nodes" essentially are at the extremes of your list and from these nodes you are able to traverse your linked list.

    Finally, your tester class just creates a Doubly Linked List and assigns it values.


    A good resource that has helped me with Linked Lists is "Barron's AP Computer Science Levels A and AB 2008" It is detailed enough to give you a broad understanding of linked lists.

    Good luck.

  3. #3
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    Just a quick question. What is the difference twixt a LinkedList and an ArrayList?

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    An arraylist is backed by an array, a linkedlist means that each node contains a reference to the next node (and in a doubly linked list, the previous node).

  5. #5
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    6

    Default

    Quote Originally Posted by carderne View Post
    Just a quick question. What is the difference twixt a LinkedList and an ArrayList?
    With an array (pure array, ArrayList, etc.) when you add something to the middle, you have to first move everything else down a slot and then insert. With a Linked list, you just adjust the links and are done. And when you delete from the middle of an array, you have to copy stuff to cover the hole.

    Arrays are faster and smaller for lists that grow only at the end.
    LinkedLists are much better if you insert or delete in the middle.

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    The actual insert or delete is quicker, yes, but finding the element takes longer as it has to travers the list. ArrayLists also have to create new arrays in order to grow (although they create them in chunks).

Similar Threads

  1. Linked List removeAll Help
    By unc123w in forum New To Java
    Replies: 13
    Last Post: 09-30-2008, 02:41 PM
  2. Doubly-linked list with data structure
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:30 PM
  3. Linked List help
    By neobie in forum New To Java
    Replies: 8
    Last Post: 12-22-2007, 03:15 AM
  4. Linked List
    By rnavarro9 in forum New To Java
    Replies: 0
    Last Post: 11-29-2007, 03:42 AM
  5. Help with linked list
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •