当前位置: 代码迷 >> J2SE >> java 建立单链表解决方案
  详细解决方案

java 建立单链表解决方案

热度:50   发布时间:2016-04-24 00:55:56.0
java 建立单链表
我按照清华大学出版社的java语言与面向对象程序设计(第2版)写了一个程序建立单链表
但是程序运行的结果显示插入节点的顺序是逆序的,我想正序插入,不知道如何解决。我认为是LinkList类里面的insert()函数的问题,且出在Node next=mNode上,导致每次next都指向头结点,使插入的节点总是位于上次插入的节点之前,但不知道如何让插入的节点位于上次插入点之后,求高人解答,我的代码:

import java.io.*;
public class Link {
public static void main(String args[])
{
String s="";
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
s=br.readLine();
}catch(IOException e){}
LinklistCheck(s);

}
public static void LinklistCheck(String s)
{
int i=1;
System.out.println("开始建立链表");
LinkList a=new LinkList();
for(i=0;i<s.length();i++)
a.insert(s.charAt(i));
//i++;
System.out.println("检验链表是否建立成功");
System.out.println(a.Display());
}

}
class Node
{
private char data;
private Node next;

Node()
{
data=' ';
next=null;
}
Node(char data)
{
this.data=data;
next=null;
}
Node(char data,Node next)
{
this.data=data;
this.next=next;
}
public char getData()
{
return data;
}
public void setNext(Node next)
{
this.next=next;
}
Node getNext()
{
return next;
}
}
class LinkList
{
Node mNode;
//Boolean i;
LinkList()
{
mNode=null;
//i=false;
}
LinkList(char data)
{
mNode=new Node(data);
//i=false;
}
public String Display()
{
Node next=mNode;
String s="";
while(next!=null)
{
s=s+next.getData()+" ";
next=next.getNext();
}
return s;
}
public void insert(char data) //将插入节点放在上次插入节点之后
{

Node next=mNode;
if(mNode==null)
{
mNode=new Node(data);
}
else
{
//mNode=new Node(data,mNode);
next.setNext(new Node(data,next.getNext()));
next=next.getNext();
}
}
}


------解决方案--------------------
Java code
import java.io.*;public class Link {    public static void main(String args[]) {        String s = "";        try {            BufferedReader br = new BufferedReader(new InputStreamReader(                    System.in));            s = br.readLine();        } catch (IOException e) {        }        LinklistCheck(s);    }    public static void LinklistCheck(String s) {        int i = 1;        System.out.println("开始建立链表");        LinkList a = new LinkList();        for (i = 0; i < s.length(); i++)            a.insert(s.charAt(i));        // i++;        System.out.println("检验链表是否建立成功");        System.out.println(a.Display());    }}class Node {    private char data;    private Node next;    Node() {        data = ' ';        next = null;    }    Node(char data) {        this.data = data;        next = null;    }    Node(char data, Node next){        this.data = data;        this.next = next;    }    public char getData() {        return data;    }    public void setNext(Node next) {        this.next = next;    }    Node getNext() {        return next;    }}class LinkList{    Node head;    Node tail;    // Boolean i;        LinkList() {        head = null;        tail = null;        // i=false;    }    LinkList(char data){        head = new Node(data);        tail = head;        // i=false;    }    public String Display(){        Node next = head;        String s = "";        while (next != null){            s += next.getData() + " ";            next = next.getNext();        }        return s;    }    public void insert(char data) // 将插入节点放在上次插入节点之后    {        if (tail == null){            head = new Node(data);            tail = head;        }else {            Node node = new Node(data);            tail.setNext(node);            tail = node;        }    }}
  相关解决方案