java关于链表的输出

WellerV 2012-07-03 01:05:03
我这是怎么了?输出无限循环。。。由于我是记事本写的 不好改错啊
...全文
483 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
WellerV 2012-10-19
  • 打赏
  • 举报
回复
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();
}
}
WellerV 2012-10-19
  • 打赏
  • 举报
回复
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();
}
}
「已注销」 2012-08-03
  • 打赏
  • 举报
回复
如果是无限循环我这么给你解释不用看就知道你this关键字没有真正的理解!this表示当前对象你应该在研究研究
WellerV 2012-07-13
  • 打赏
  • 举报
回复
谢谢大大们 弄懂了
newfarmerchi 2012-07-03
  • 打赏
  • 举报
回复

public void print(Node root) {
System.out.print(this.data + "\t");
if (this.next != null)
this.next.print(this.next);
}


五哥 2012-07-03
  • 打赏
  • 举报
回复
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();
}
}
WellerV 2012-07-03
  • 打赏
  • 举报
回复
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();
}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧