回 帖 发 新 帖 刷新版面

主题:各位大侠们来看看这个数据结构的题目吧

package ConsoleApplication;
class link
{
    public int data;
    public double ldata;
    public link next;
    public link(int id, double ldata)
    {
        this.data = id;
        this.ldata = ldata;
    }
    public void displayLink()
    {
        System.out.println("{" + data + "," + ldata + "}");

    }

}
class linkList
{
    private link first;
    public linkList()
    {
        first = null;
    }
    public void insertFirst(int id, double dd)
    {
        link newnode = new link(id, dd);
        newnode.next = first;
        newnode = first;
    }
    public link find(int key)
    {

        link current = first;
        while (current.data != key)
        {
            if (current.next == null)
                return null;
            else

                current = current.next;
        }

        return current;
    }

    public link delete(int key)
    {
        link current = first;
        link previous = first;
        while (current.data != key)
        {
            if (current.next == null)
                return null;
            else
            {
                previous = current;
                current = current.next;
            }
        }//end while,stay at current and previous
        //deal with the new Node's relationship
        if (current == first)
            first = first.next;
        else
            previous.next = current.next;
        return current;

    }
    public void dispalyList()
    {
        System.out.println("List is now displaying(first--->last)");
        link current = first;
        while (current != null)
        {
            current.displayLink();
            current = current.next;
        }
    }


}//end the class linkList
class linkListDemo
{
    public static void main(String args[])
    {
        linkList list = new linkList();
        list.insertFirst(0, 123);
        list.insertFirst(1, 2343);
        list.insertFirst(2, 342.2443);
        list.insertFirst(3, 3432);
        list.insertFirst(4, 35);
        list.insertFirst(5, 4243);
        list.dispalyList();
        System.out.println("Now,start to find Node");
        System.out.println("=======================================================");
        link f = list.find(5);
        if (f != null)
            System.out.println("Find the answer at" + "\t" + f.data + "---->" + f.ldata);
        else
            System.out.println("Error has comeout!");
        System.out.println("Now,start to delete Node");
        System.out.println("=======================================================");
        link del = list.delete(3);
        if (del != null)
            System.out.println("The data has been deleted");
        else
            System.out.println("Error has come out!");
        System.out.println("=======================================================");
        System.out.println("Now,the new linkList is ");
        list.dispalyList();
        System.out.println("=======================================================");

    }
}

/////////////////////////////////////////////////////////////////////////
编译器老是说我没有把引用对象转换到实例对象
我觉得没什么逻辑错误啊 
而且每次都错再find 函数部分的 啊 
高手们给小弟指点一下哈
谢谢了

回复列表 (共2个回复)

沙发

public void insertFirst(int id, double dd)
    {
        link newnode = new link(id, dd);
        newnode.next = first;
        newnode = first;//!!!!!!!!!!!!!!!!!!!
    }
//把 newnode = first;改为first=newnode;

板凳

你的程序中first自始至终一直是个空值,当执行到link f = list.find(5);这句话的时候,到达find()方法后执行link current = first;也就是说current也变成了空的,你用current去调用next属性肯定会报空指针异常呀.

我来回复

您尚未登录,请登录后再回复。点此登录或注册