【问题标题】:Data structures and algorithms in JavaJava中的数据结构和算法
【发布时间】:2015-11-17 06:41:01
【问题描述】:

好的,我正在尝试在这本书上做这个编程项目。它是这样说的:

循环列表是一个链表,其中最后一个链接指向第一个链接。 有很多方法可以设计一个循环列表。有时有一个指针指向 列表的“开始”。然而,这使得列表不像一个真正的圆圈,并且 更像是一个普通的列表,它的结尾附加在它的开头。做一个 单链循环列表的类,没有结尾也没有开头。这 对列表的唯一访问是单个引用,当前,可以指向任何链接 在名单上。此引用可以根据需要在列表中移动。 (看 针对这种循环列表理想情况的编程项目 5.5 适合。)您的列表应该处理插入、搜索和删除。你可以 如果这些操作发生在下游的一个环节上会很方便 当前指向的链接。 (因为上游链接是单链接的,你 不绕圈子就无法做到。)你也应该是 能够显示列表(尽管您需要随意打破圆圈 点将其打印在屏幕上)。移动电流的 step() 方法 到下一个链接也可能会派上用场。

这是我目前所拥有的,我拥有的主要方法无法更改。

class circularL
{
    int data;

    circularL next;
    circularL prev;

    public circularL(int d)
    {
        data = d;
        next = null;
        prev = null;
    }

    public int getData()
    {
        return data;
    }
}

class circularLL
{
    circularL currPt;

    //constructor
    public circularLL()
    {
        currPt = null;
    }

    /******************************************
    * insertLink() function performs *
    * inserting a new link with the data item*
    * to the existing list. *
    *****************************************/
    public void insertLink(int dd)
    {
        circularL theLink = new circularL(dd);

        if(currPt==null)
        {
            theLink.next = theLink;
            theLink.prev = theLink;
        }
        else if(currPt.next==null&&currPt.prev==null&& currPt!=null)
        {
            theLink.next = currPt;
            theLink.prev = currPt;
            currPt.prev = theLink;
            currPt.next = theLink;
        }
        else if(currPt != null)
        {
            theLink.next = currPt.next;
            theLink.prev = currPt;
            currPt.next.prev = theLink;
            currPt.next = theLink;
        }

        currPt = theLink;
    }

    /**************************************************
    * The search() function search for the particular*
    * key by traversing through the linked list. *
    *************************************************/
    public int find(int key)
    {
        circularL tempCurr= currPt;

        if(tempCurr.data == key)
        {
            return key;
        }
        else
        {
            tempCurr = tempCurr.next;
        }

        while(tempCurr.data!=currPt.data)
        {
            if(tempCurr.data==key)
            {
                return key;
            }
            else
            {
                tempCurr = tempCurr.next;
            }
        }

        System.out.println("Key not found!");
        return -99;
    }

    /******************************************
    * The delete() function delete the linked*
    * list at the current pointer and return *
    * the value of the data item. *
    *****************************************/
    public int delete()
    {
        circularL tempCurr = currPt;
        currPt.next.prev = currPt.prev;
        currPt.prev.next = currPt.next;
        currPt = tempCurr.prev;
        return tempCurr.data;
    }

    /********************************************
    * The printRing() function prints out the *
    * linked list by traversing the linked list*
    * in either direction. *
    *******************************************/
    public void displayList(boolean direction)
    {
        circularL tempLink = currPt;

        do
        {
            System.out.print(tempLink.getData() +" ");
            tempLink=direction?tempLink.next:tempLink.prev;
        }
        while(tempLink.data!=currPt.data);

        System.out.println("");
    }
}

class CircApp
{
    public static void main(String[] args)
    {
        Link f, d;
        circularLL theList = new circularLL();  // make list

        theList.insertLink(10);      // insert items
        theList.insertLink(20);
        theList.insertLink(30);
        theList.insertLink(40);
        theList.insertLink(50);
        theList.insertLink(60);
        theList.insertLink(70);

        theList.displayList();              // display list

        f = theList.find(30);               // find item
        if( f != null)
            System.out.println("Found link with key " + f.iData);
        else
            System.out.println("Can't find link with key 30");
        theList.displayList();              // display list

        System.out.println("Inserting link with key 80");
        theList.insertLink(80);
        theList.displayList();              // display list

        d = theList.delete(60);             // delete item
        if( d != null )
            System.out.println("Deleted link with key " + d.iData);
        else
            System.out.println("Can't delete link with key 60");
        theList.displayList();              // display list

        f = theList.find(99);               // find item
        if( f != null)
            System.out.println("Found link with key " + f.iData);
        else
            System.out.println("Can't find link with key 99" );
        theList.displayList();              // display list

        d = theList.delete(13);             // delete item
        if( d != null )
            System.out.println("Deleted link with key " + d.iData);
        else
            System.out.println("Can't delete link with key 13");
        theList.displayList();              // display list

        System.out.println("Stepping through list");
        for(int j=0; j<theList.getSize(); j++)
        {
            theList.step();
            theList.displayList();
        }

        System.out.println("Will delete and step one by one");
        while(theList.isEmpty() == false)
        {
            theList.delete();
            theList.step();
            theList.displayList();
        }

    }  // end main()
}

【问题讨论】:

  • 请整理一下代码的格式。
  • 还有什么问题吗?
  • 我想这样做而不给我错误。
  • @CssStudent 你遇到了什么错误?
  • @CSSStudent 我们为什么要“获取 [您的] 代码并运行它”只是为了查看您应该向我们展示的错误?说真的,没有提到任何问题或任何调试尝试。

标签: java algorithm data-structures


【解决方案1】:

以下是错误:

1.

CircApp.java:134: error: cannot find symbol
    Link f, d;

原因:没有您试图引用的 Link 类。 解决:创建一个Link 类,或具有类似行为的东西。

  1. 在第 145、152、156、160、173、177、183、191 行:您正在使用方法displayList,即not defined

    在第 152、172 行:您正在使用方法 delete,即 not defined

    在第 182、190 行:您正在使用方法step,即not defined

    在第 180 行:您正在使用方法getSize,即not defined

    在第 187 行:您正在使用方法 isEmpty,即 not defined

解决:定义这些方法

如果您想在定义这些方法之前运行代码,至少为它们创建一个空定义或注释这些行。

PS:永远不要在 SO 上发布代码来解决编译错误。这显然意味着你不想学习。

【讨论】:

  • “永远不要在 SO 上发布代码来解决编译错误。”那你为什么要回答并奖励你试图阻止的行为呢?
  • 当时是免费的。想我应该帮忙。就是这样:)
猜你喜欢
  • 2022-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-20
  • 2013-06-19
  • 1970-01-01
  • 2011-02-08
  • 1970-01-01
相关资源
最近更新 更多