//链表中的元素
class Link3{
public long dData;
public Link3 next;
public Link3(long d){
dData=d;
}
//显示链表
public void displayLink(){
System.out.println(dData+" ");
}
}
//链表
class FirstLastList{
private Link3 first;
private Link3 last;
public FirstLastList(){
first=null;
last=null;
}
//向链表表尾插入元素
public void insertLast(long dd){
Link3 newLink=new Link3(dd);
if(isEmpty()){
first=newLink;
}else{
last.next=newLink;
}
last=newLink;
}
//删除表头元素
public long deletFirst(){
long temp=first.dData;
if(first.next==null){
last=null;
}
first =first.next;
return temp;
}
}
//对列
class LinkQueque{
private FirstLastList theList;
//插入队尾元素
public void insert(long dd){
theList.insertLast(dd);
}
//删除对头元素
public long remove(){
return theList.deletFirst();
}
}
public class LinkQuequeApp { public static void main(String[] args) {
// TODO Auto-generated method stub
LinkQueque theQueque=new LinkQueque();
theQueque.insert(20);
theQueque.insert(70);
theQueque.insert(60);
theQueque.displayQueque();
System.out.println(" ");
theQueque.insert(80);
theQueque.insert(10);
theQueque.insert(90);
theQueque.displayQueque();
//删除元素!!!错误出现在这里提示出现空指向异常!!为什么??
theQueque.remove();
theQueque.remove();
theQueque.displayQueque();
}}
怎么会出现空指向异常?
package Java数据结构与算法char5;
/*用链表实现队列!!
*
* */
public class LinkQuequeApp { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
LinkQueque theQueque=new LinkQueque();
theQueque.insert(20);
theQueque.insert(70);
theQueque.insert(60);
theQueque.displayQueque();
System.out.println(" ");
theQueque.insert(80);
theQueque.insert(10);
theQueque.insert(90);
theQueque.displayQueque();
theQueque.remove();
theQueque.remove();
theQueque.displayQueque();
}}class Link3{
public long dData;
public Link3 next;
public Link3(long d){
dData=d;
}
public void displayLink(){
System.out.println(dData+" ");
}
}class FirstLastList{
private Link3 first;
private Link3 last;
public FirstLastList(){
first=null;
last=null;
}
public boolean isEmpty(){
return first==null;
}
public void insertLast(long dd){
Link3 newLink=new Link3(dd);
if(isEmpty()){
first=newLink;
}else{
last.next=newLink;
}
last=newLink;
}
public long deletFirst(){
long temp=first.dData;
if(first.next==null){
last=null;
}
first =first.next;
return temp;
}
public void displayList(){
while(!isEmpty()){
System.out.print(" "+deletFirst());
}
}
}class LinkQueque{
private FirstLastList theList;
public LinkQueque(){
theList=new FirstLastList();
}
public boolean isEmpty(){
return theList.isEmpty();
}
public void insert(long dd){
theList.insertLast(dd);
}
public long remove(){
return theList.deletFirst();
}
public void displayQueque(){
System.out.println("Queque(front-->rear");
theList.displayList();
}
}那我给全一点
public long deletFirst(){
//建议这里插入判断first是否为空,remove时first为空
long temp=first.dData;
if(first.next==null){
last=null;
}
first =first.next;
return temp;
}
那到 了这三句的时候theQueque.remove();
theQueque.remove();
theQueque.displayQueque();first 和 last 已经是NULL 了,这是你去remove时直接调deletFirst() 运行这句话long temp=first.dData;
当然会抛出NulPointerException
为什么
displayQueque的时候要删除?不懂