Java中的链接列表FIFO队列创建

时间:2014-05-11 21:55:33

标签: java data-structures linked-list

我是Java编程新手(大约4个月)。我刚拿起一份"数据结构& Java中的算法" Robert Lafore的第二版,我正在阅读章节并尝试后面的问题。由于我没有参加官方课程,我需要某种形式的评分来教我如何改进我的工作..比堆叠中的天才更好的地方。

我目前正在使用第5章“链接列表”。

以下是问题5.1 基于已排序的链接列表实现优先级队列。删除操作 优先级队列应删除具有最小密钥的项目。

容器类

public class LinkLL {


public static LinkLL leftConnector; //left side of node
public static String fName; //item in actual node
public static LinkLL rightConnector; //Right side of node


public LinkLL(String key)
{
    fName = key; 
    //A new LinkLL linked list has been Instantiated, 
    //although it is empty.

    rightConnector = null;
    leftConnector = null;
}


public void showName()
{
    System.out.print(fName + " " + "There are " + SortedLL.nElems + "in the list at this moment.");

}

}

这里我们有insert,delete..etc等方法。我觉得我在"插入"也许是删除?但我的"排序"和"显示"正在给我一笔钱。例如,我想显示输入的每个字符串,但是我拥有它的方式,只显示第一个字符串。

"抽象"

public class SortedLL {

static LinkLL currentNode;
static LinkLL firstNode;
static final LinkLL lastNode = null;
static LinkLL prequelNode;
static LinkLL sequelNode;
static float nElems;
static LinkLL l;

public static void insert(String key)
{
    if ( nElems == 0) //could have also been if(nElems == 0)
    {   
        l = new LinkLL(key); //if nothing exists, create one
        l.rightConnector = sequelNode;//the right connector is equal to the sequelNode
        sequelNode = lastNode;  // Sequel Node is equal to lastNode which is 'null'.
        prequelNode = sequelNode;
        firstNode.leftConnector = lastNode;
    }

    else if( !(nElems == 0) )
    {
        LinkLL NewEndNode;
        NewEndNode = lastNode;
        l.rightConnector = NewEndNode;
        l.leftConnector = prequelNode.rightConnector;           

    }
    //when such occurs, nodes must be re-connected
    nElems++;
}

public void remove()
{   

    if(nElems == 0) System.out.println("There are no items to remove.");
    //if( !find(key) ) System.out.println(key +" could not be located, " + "and thus cannot be removed.");


        //while ( find(key)  ) //while the key can be found
        {
            //currentNode = key;

            prequelNode.rightConnector = sequelNode.leftConnector;


            nElems--;
        }
    //when such occurs, nodes should be reconnected

}

public boolean find(LinkLL key)
{
    if (isEmpty() == true)
    {
        System.out.println("The List is Empty");
    }

    else
    {
        for(int i = 0; i<nElems+1; i++) 
        {
            if (i == nElems+1) 
            //added '+1' to make sure that the item to be searched for is not at the bottom(list scanned throughly)
                System.out.println("The key " + key + "has NOT been found!");
            else 
                System.out.println("The key " + key + "has been found!");
        }

    }
    return true;
}


public void sort()
{


}

public boolean isEmpty()
{
    if (firstNode != null) return false;
    else return false;
}

public void displayNode()
{
    LinkLL first = null;
    LinkLL current = first ;

    while (current != null)
    {
        l.showName();
        current = current.rightConnector;

    }       
}

}

主要()

public class sortedLLApp {
public static void main(String []args)
{
    SortedLL s = new SortedLL();

    s.isEmpty();

    s.insert("Jack");
    s.insert("Jill");
    s.insert("John");
    s.insert("Jenn");
    s.insert("James");
    s.displayNode();



}

}

1 个答案:

答案 0 :(得分:1)

显然sort没有做任何事情,因为你根本没有实现它。但也许你应该尝试一下并将其作为一个单独的问题提交。


displayNode()什么都不做的原因是整个方法都使用局部变量。此外,由于某种原因,您已将大部分代码声明为static,这完全违背了拥有可重用类的目的。不可否认,我还没有实际运行你的代码,但为什么不尝试这个:

  1. static课程中删除单词SortedLL的所有实例。
  2. displayNode()更改为:
  3. 代码:

    public void displayNode()
    {
        LinkLL current = firstNode;
    
        while (current != null)
        {
            l.showName();
            current = current.rightConnector;
        }       
    }
    
相关问题