2014-05-11 59 views
0

我是Java新手(约4个月)。我刚刚拿到了Robert Lafore的“Data Structures & Java算法”第二版的副本,我正在阅读章节并试图解决后面的问题。因为我没有参加正式课程,所以我需要一些评分方式来教我如何改进我的工作。比堆栈中的天才更好的地方。链接列表Java Java中的FIFO排队更正

我目前在第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."); 

} 

} 

在这里我们有方法,如插入,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; 

    }  
} 

} 

的Main()

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

很明显嘛sort什么也不做,因为你还没有实现它。但是,也许你应该尝试一些并将其作为单独的问题提交。


你的displayNode()什么都不做的原因是整个方法使用局部变量。另外,出于某种原因,你已经将大部分代码声明为static,这完全违背了具有可重用类的目的。从SortedLL类的词static

  1. 删除所有实例:不可否认,我还没有实际运行您的代码还,但你为什么不试试这个。
  2. 变化displayNode()这样:

代码:

public void displayNode() 
{ 
    LinkLL current = firstNode; 

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