By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,034 Members | 819 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,034 IT Pros & Developers. It's quick & easy.

Doubly-Link List?

P: n/a
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(node);
head = node;

DLLNode node1 = new DLLNode ("Two");
index.setNext(node1);
index.setPrev(head);
index = node1;

DLLNode node2 = new DLLNode("Three", tail, null);
tail.setNext(node2);
tail.setPrev(node1);
tail = node2;

}
}
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Liz
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" <le*******@earthlink.net> wrote in message
news:vv******************@newsread1.news.pas.earth link.net...
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(node);
head = node;

DLLNode node1 = new DLLNode ("Two");
index.setNext(node1);
index.setPrev(head);
index = node1;

DLLNode node2 = new DLLNode("Three", tail, null);
tail.setNext(node2);
tail.setPrev(node1);
tail = node2;

}
}

Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.