Doubly-Link List?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • D. Beckham

    Doubly-Link List?

    I wrote the following code to create a short doubly-link list of three
    strings: "one", "two", and "three". I would like to know if I set them up
    correctly, so that they point to one another.

    DLLNode class has already been created. Basically, "one" is the head node,
    "two" is the mid node, and "three" is the tail or the last node. I would
    like to know if my algorithm for setting up doubly-linked node are correct
    before I proceed to printing them. Can anybody give me comments regarding
    my algorithm?

    public class TestNode
    {
    private static DLLNode head = null;
    private static DLLNode tail = null;
    private static DLLNode index = null;

    public static void main(String[] args)
    {
    DLLNode node = new DLLNode("One", null, head);
    if (head == null) //list empty
    {
    head = tail = index = node;
    return;
    }
    head.setPrev(no de);
    head = node;

    DLLNode node1 = new DLLNode ("Two");
    index.setNext(n ode1);
    index.setPrev(h ead);
    index = node1;

    DLLNode node2 = new DLLNode("Three" , tail, null);
    tail.setNext(no de2);
    tail.setPrev(no de1);
    tail = node2;

    }
    }


  • Liz

    #2
    Re: Doubly-Link List?

    I hate to bring this up, but did you try it?
    If you are going to use it you are going to have to try it eventually.
    Also, you should expect that you should be doing some testing of your code
    too.
    After all, how many bosses would put up with "I wrote the code and some
    geek on usenet said it works for him ok, so it must be right."

    "D. Beckham" <lekhanh88@eart hlink.net> wrote in message
    news:vvsuc.1593 0$Tn6.9773@news read1.news.pas. earthlink.net.. .[color=blue]
    > I wrote the following code to create a short doubly-link list of three
    > strings: "one", "two", and "three". I would like to know if I set them up
    > correctly, so that they point to one another.
    >
    > DLLNode class has already been created. Basically, "one" is the head node,
    > "two" is the mid node, and "three" is the tail or the last node. I would
    > like to know if my algorithm for setting up doubly-linked node are correct
    > before I proceed to printing them. Can anybody give me comments regarding
    > my algorithm?
    >
    > public class TestNode
    > {
    > private static DLLNode head = null;
    > private static DLLNode tail = null;
    > private static DLLNode index = null;
    >
    > public static void main(String[] args)
    > {
    > DLLNode node = new DLLNode("One", null, head);
    > if (head == null) //list empty
    > {
    > head = tail = index = node;
    > return;
    > }
    > head.setPrev(no de);
    > head = node;
    >
    > DLLNode node1 = new DLLNode ("Two");
    > index.setNext(n ode1);
    > index.setPrev(h ead);
    > index = node1;
    >
    > DLLNode node2 = new DLLNode("Three" , tail, null);
    > tail.setNext(no de2);
    > tail.setPrev(no de1);
    > tail = node2;
    >
    > }
    > }
    >
    >[/color]


    Comment

    Working...