java关于链表的输出
我这是怎么了?输出无限循环。。。由于我是记事本写的 不好改错啊 --------------------编程问答-------------------- class Link{class Node{
private String data;
private Node next;
public Node(String data){
this.data=data;
}
public void print(Node root){
System.out.print(this.data+"\t");
if(this.next!=null) print(this.next);
}
public void add(Node newnode){
if(this.next==null) this.next=newnode;
else this.next.add(newnode);
}
}
private Node root;
public void printNode(){
if(this.root!=null) this.root.print(this.root);
}
public void addNode(String data){
Node newnode=new Node(data);
if(this.root==null) this.root=newnode;
else this.root.add(newnode);
}
}
public class LinkDemo{
public static void main(String args[]){
Link l=new Link();
l.addNode("A");
l.addNode("B");
l.addNode("C");
l.addNode("D");
l.addNode("E");
l.printNode();
}
} --------------------编程问答--------------------
package a;--------------------编程问答--------------------
class Node {
private String data;
private Node next;
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public Node(String data) {
this.data = data;
this.next = null ;
}
public void print(Node root) {
//这里写错啦 ,你应该打印的是root的数据,而不是this,递归打印,你应该递归root.next
// System.out.print(this.data + "\t");
// if (this.next != null)
// print(this.next);
System.out.print(root.data + "\t");
if (root.next != null)
print(root.next);
}
// public void add(Node newnode) {
// if (this.next == null)
// this.next = newnode;
// else
// this.next.add(newnode);
// }
}
class Link {
private Node root;
public void printNode() {
if (this.root != null)
this.root.print(this.root);
}
public void addNode(String data) {
Node tmp = null ;
Node newnode = new Node(data);
if (this.root == null)
this.root = newnode;
else{
//这里修改掉 ,你每次增加节点都是在最后加,所以每次先找到最后一个节点然后在加上去
tmp = root ;
while(tmp.getNext() != null ) {
tmp = tmp.getNext() ;
}
tmp.setNext(newnode);
//打印结果 :A B B B C D E
//以下是在前面增加节点的代码
//newnode.setNext(root) ;
//root = newnode ;
//打印结果 : E D C B B B A
}
}
}
public class LinkDemo {
public static void main(String args[]) {
Link l = new Link();
l.addNode("A");
l.addNode("B");
l.addNode("B");
l.addNode("B");
l.addNode("C");
l.addNode("D");
l.addNode("E");
l.printNode();
}
}
--------------------编程问答-------------------- 谢谢大大们 弄懂了 --------------------编程问答-------------------- 如果是无限循环我这么给你解释不用看就知道你this关键字没有真正的理解!this表示当前对象你应该在研究研究 --------------------编程问答-------------------- class Link {
public void print(Node root) {
System.out.print(this.data + "\t");
if (this.next != null)
this.next.print(this.next);
}
class Node {
private String data;
private Node next;
public Node(String data) {
this.data=data;
}
public void print() {
System.out.print(this.data+"\t");
if(this.next!=null) this.next.print();
}
public void add(Node newnode) {
if(this.next==null) this.next=newnode;
else this.next.add(newnode);
}
}
private Node root;
public void printNode() {
if(this.root!=null) this.root.print();
}
public void addNode(String data) {
Node newnode=new Node(data);
if(this.root==null) this.root=newnode;
else this.root.add(newnode);
}
}
修改后的代码
public class LinkDemo {
public static void main(String args[]) {
Link l=new Link();
l.addNode("A");
l.addNode("B");
l.addNode("C");
l.addNode("D");
l.addNode("E");
l.printNode();
}
} --------------------编程问答-------------------- class Link {
class Node {
private String data;
private Node next;
public Node(String data) {
this.data=data;
}
public void print() {
System.out.print(this.data+"\t");
if(this.next!=null) this.next.print();
}
public void add(Node newnode) {
if(this.next==null) this.next=newnode;
else this.next.add(newnode);
}
}
private Node root;
public void printNode() {
if(this.root!=null) this.root.print();
}
public void addNode(String data) {
Node newnode=new Node(data);
if(this.root==null) this.root=newnode;
else this.root.add(newnode);
}
}
public class LinkDemo {
public static void main(String args[]) {
Link l=new Link();
l.addNode("A");
l.addNode("B");
l.addNode("C");
l.addNode("D");
l.addNode("E");
l.printNode();
}
}
补充:Java , Java SE