挺好的. 如果只是为了练习,这是可以的,否则用java.util.LinkedList也行. 我把一些代码重写了一下,只是不同的思路,供参考: public class Node { private char data = '\0'; public Node next;
public Node () { } public Node (char data) { this.data = data; }
// methods public char getData () { return data; } public boolean isTheData(char data) { return this.data == data; } public void replaceData (char data) { this.data = data; } }public class List { private Node head; // private Node movCur; /* don't let temporary variable to be a data member public List () { // don't assign a new Node() to head, otherwise there will always be a dummy head }
// methods public void addNode (char data) { if (!head) { head = new Node (data); // head.next = null; } else { Node movCur = head; while(movCur.next != null) movCur = movCur.next; movCur.next = new Node(data); /* ; newNode = new Node (data); newNode.next = null; movCur.next = newNode; */ } }
//
虽然运行结果是我想要的,
但是我想知道别人会怎么想呢?
还有什么好的设计方法吗?
//
这个程序有什么欠缺的地方吗??
//
如果只是为了练习,这是可以的,否则用java.util.LinkedList也行.
我把一些代码重写了一下,只是不同的思路,供参考:
public class Node {
private char data = '\0';
public Node next;
public Node () { }
public Node (char data) {
this.data = data;
}
// methods
public char getData () {
return data;
}
public boolean isTheData(char data) {
return this.data == data;
}
public void replaceData (char data) {
this.data = data;
}
}public class List {
private Node head;
// private Node movCur; /* don't let temporary variable to be a data member public List () {
// don't assign a new Node() to head, otherwise there will always be a dummy head
}
// methods
public void addNode (char data) {
if (!head) {
head = new Node (data);
// head.next = null;
}
else {
Node movCur = head;
while(movCur.next != null)
movCur = movCur.next;
movCur.next = new Node(data);
/* ;
newNode = new Node (data);
newNode.next = null;
movCur.next = newNode;
*/
}
}
public void printList () {
for (Node movCur = head;
movCur != null;
movCur = movCur.next)
{
System.out.println ("data : " + movCur.getData ());
}
}
}