For homework I am supposed to create a circularly linked list using nodes and pointers.
This is my node classclass Node implement开发者_JS百科s Serializable {
public String theName; //the wrapped name
public Node next; //the next node in the sequence
public Node prev; //the previous node in the sequence
public Node(Node p, String s, Node n){
prev = p;
theName = s;
next = n;
}
}
I am trying to insert strings (names) at the front of the list and then print them out using the traverse method.
So far these are my traverse and insert methods...public class ListImpl /*implements List*/ {
Node list;
public ListImpl() {
list = new Node(list, null, list);
}
public void insert(String s) {
if (list == null) {
list = new Node(list, s, list);
} else {
list = new Node(list.prev, s, list);
list.next.prev = list;
list.next.next = list;
}
}
public void traverse(ASCIIDisplayer out) {
Node p = new Node(null, null, null);
p = list;
if (list != null) {
while(true) {
out.writeString(p.theName);
p = p.next;
}
} else {
throw new EmptyListException();
}
}
}
I understand that my traverse method will me a continuous loop and I have to figure out how to make it stop when it gets back to the beginning of the list, but that is not my problem.
I believe my problem lies in the insert method because my output is not as expected when I run this code (the main):public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ASCIIDisplayer a = new ASCIIDisplayer();
ListImpl List;
List = new ListImpl();
List.insert("Steve");
List.insert("Kuba");
List.insert("Taylor");
List.insert("Jane");
List.traverse(a);
}
}
The output I get is: Taylor Jane Taylor Jane Taylor Jane Taylor Jane... repeated.
I expected the output to be: Steve Kuba Taylor Jane Steve Kuba Taylor Jane...repeated.This is why I think that the problem is in the insert method, my pointer must be pointing to the wrong nodes but I just can't figure out what I did wrong.
Sorry for the long question, hopefully there is enough information for you to help me! Thanks in advance!
Replace this:
list = new Node(list.prev, s, list);
list.next.prev = list;
list.next.next = list;
with this:
Node tmpList = new Node(list.prev, s, list);
list.next.prev = tmpList;
list.prev.next = tmpList;
list = tmpList;
As for figuring out when you repeat a node, you can do that by saving the first node and comparing if it's the same as the next node, when that happens you're reached the end of the list.
Also, when you insert the first node you do this:
if (list == null) {
list = new Node(list, s, list);
In that case both of the list
parameters being passed on to your constructor are null
, you should fix that either by making a new constructor that only takes the String
value and then references itself or by setting prev
and next
manually.
Here's the constructor solution:
class Node implements Serializable {
public String theName; //the wrapped name
public Node next; //the next node in the sequence
public Node prev; //the previous node in the sequence
public Node(Node p, String s, Node n){
prev = p;
theName = s;
next = n;
}
public Node(String s){
prev = this;
theName = s;
next = this;
}}
You do have an issue in your insert method:
list.next.prev = list;
list.next.next = list;
Anything seem asymmetic there?
Unrelated, but think about the questions: can list
ever be null
? Should it be?
This part of your insert looks incorrect
} else {
list = new Node(list.prev, s, list);
list.next.prev = list;
list.next.next = list;
The last line should be:
list.prev.next = list
Look closely at the else
branch inside the insert
function, and consider whether you really want to set list.next.next=list
.
精彩评论