首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

双端链表删除最后一个元素报错 怎么解决 非常感谢

2013-06-25 
双端链表删除最后一个元素报错 如何解决 非常感谢!代码如下package org.firstlastlist.abc/* * demonstra

双端链表删除最后一个元素报错 如何解决 非常感谢!

代码如下

package org.firstlastlist.abc;
/*
 * demonstrates list with first and last references
 * to run this program: C> java FirstLastApp
 */
/* ---- class Link ---- */
class Link{
public long dData;  /* data item*/
public Link next;   /* next link in list*/

public Link(long d){        //constructor
dData = d;
}

public void displayLink(){   // display this link
System.out.print("{"+dData+"}");
}
}// end class Link


/* ----class FirstLastList ---- */
class FirstLastList{
private Link first;  // ref to first link
private Link last;   // ref to last link

//--constructor--
public FirstLastList(){  // constructor
first = null;        // no link on list yet
last = null;
}
//----isEmpty() method----
public boolean isEmpty(){  // true if no links
return (first==null);
/*if(first==null){
return true;
}
else
return false;
*/
}

//----insertFirst()----
public void insertFirst(long dd){   // insert at front of list
Link newlink = new Link(dd);    // make new link
/*if(isEmpty()){*/        //this is right or ..
if(first==null){
//first = newlink;
last = newlink;       //newlink<--last
}
else{
newlink.next = first; // newlink --> old first
//first = newlink;
}
first = newlink;          // first-->newlink
}
//----insertLast()----
public void insertLast(long dd){  // insert at the end of list
Link newlink = new Link(dd);
if(isEmpty()){
first = newlink;
//last = newlink;
}
else{
last.next = newlink;
}
last = newlink;
}

//----deleteFirst()----
public Link deleteFirst(){// delete first link
Link temp= first;
if (first.next==null)
last = null;
first = first.next;
return temp;
}
//----displayList()----
public void displayList(){
Link current = first;
while(current != null){ // current.next!= null is wrong!!!
current.displayLink();


current = current.next;
}
System.out.println("\n");
}

}//end class FirstLastList


/* ----public class---- */
public class FirstLastApp {
public static void main(String[] args) {
FirstLastList theList = new FirstLastList();

System.out.println("\nInsert from first: first-->last");
theList.insertFirst(10);
theList.insertFirst(9);
theList.insertFirst(8);
theList.insertFirst(7);
theList.displayList();

System.out.println("\n\nInsert from last: first-->last");
theList.insertLast(11);
theList.insertLast(12);
theList.insertLast(13);
theList.insertLast(14);
theList.displayList();

Link delfirst = theList.deleteFirst();
System.out.println("\n\ndelfirst = "+delfirst.dData);
System.out.println("Delete first: first-->last ");
theList.displayList();

System.out.println("\n\nDelete last: first-->last");

theList.deleteFirst();
theList.displayList();

theList.deleteFirst();
theList.displayList();

theList.deleteFirst();
theList.displayList();

theList.deleteFirst();
theList.displayList();

theList.deleteFirst();
theList.displayList();

theList.deleteFirst();
theList.displayList();

theList.deleteFirst();
theList.displayList();

theList.deleteFirst();
theList.displayList();
}// end main
}//end class FirstLastApp

Java
[解决办法]
帮你修改了一下deleteFirst方法:
public Link deleteFirst() {// delete first link
if (isEmpty()) {
throw new RuntimeException("The list is empty.");
}
Link temp = first;
if (first.next == null) {
first = null;
last = null;
}
return temp;
}

[解决办法]
try catch一下不就好了

热点排行