gpt4 book ai didi

java - 链表的 getlast() 中出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-02 09:02:39 27 4
gpt4 key购买 nike

我的链接列表遇到这个问题,当我尝试访问最后一个节点数据部分时,它会抛出空异常

代码部分:主类中的对象的 getlast() 方法

package lab1_ds;
public class LAB1_DS {
public static void main(String[] args) {
singlylinkedlist mylist=new singlylinkedlist();
singlylinkedlist<person> plist=new singlylinkedlist();
plist.addfirst(new person("Hesssa","SA"));
plist.addfirst(new person("Nora","SA"));
plist.display();
plist.addnode(new person("Farah","SA"),2);
plist.display();
System.out.println(plist.first().getName());
plist.removeNode(plist.getlast());
plist.display();
}

}

链表代码为:

package lab1_ds;
public class singlylinkedlist <E>{
private static class Node<E>{
private E element;
private Node<E> next;

public Node(E element, Node<E> next) {
this.element = element;
this.next = next;
}

public E getElement() {
return element;
}

public Node<E> getNext() {
return next;
}

public void setNext(Node<E> next) {
this.next = next;
}

}
private Node<E> head=null;
private Node<E> tail=null;
private int size=0;

public singlylinkedlist() {
}
public void display(){
Node<E> current;
current=head;
int count=0;
System.out.println("\n-----------Display method-----------");
while(current!=null){
count++;
System.out.println("Linked list ("+count+"):"+current.getElement());
current=current.getNext();

}
}
public int size(){
return size;
}
public boolean isEmpty(){
return size==0;
}
public E getlast(){
if (isEmpty())
return null;
return tail.getElement();
}




public void setTail(Node<E> tail) {
this.tail = tail;
}

public E first(){
if (isEmpty())
return null;
return head.getElement();
}
public void addfirst(E value){
Node<E> newNode= new Node(value,null);
newNode.next=head;
head=newNode;
size=size+1;
if(size==1)
head=tail;
}
public void addlast(E value){
Node<E> newNode= new Node(value,null);
if(size==0)
head=tail=newNode;
else{ tail.next=newNode;
tail=newNode;
}
size=size+1;
}
public void addnode(E value,int pos){
Node<E> current;
if(pos==1)
addfirst(value);
if(pos==size+1)
addlast(value);
Node<E> newNode= new Node(value,null);
current=head;
int count=1;
while(count<pos-1 && current.next!=null){
current=current.next;
count=count+1;
}
newNode.next=current.next;
current.next=newNode;
size++;//or size=size+1;

}
public void removeFirst(){
if (isEmpty()){
System.out.println("linked list is empty");
return;
}
head=head.getNext();
size--;//or size=size-1;
if(size==0)
tail=null;//only node

}
public void findNode(E place){
if (isEmpty()){
System.out.println("linked list is empty");
return;
}
Node<E> current=head;
int count=1;
while (current!=null ){
if(current.getElement()==place || current.getElement().equals(place)){
System.out.println("found in posittin #"+count);
return;}
count++;
current=current.getNext();
}//end while loop

System.out.println("\n.......Node is not found!......");
}
public void removeNode(E place){
Node<E> current=head;
Node<E> prev=head;
if (isEmpty()){
System.out.println("linked list is empty");
return;
}
while(current.getElement()!=place && !current.getElement().equals(place)){
if(current.next==null){
System.out.println("\n not found...");
return;
}
prev=current;
current=current.next;
}//end loop
if(current==head){
removeFirst();
}
else {
prev.next=current.next;
size--;
}
if(current==tail){//node i'm trying to remove is the last node
tail=prev;
}
}
}

错误显示:

Exception in thread "main" java.lang.NullPointerException
at lab1_ds.singlylinkedlist.getlast(singlylinkedlist.java:52)
at lab1_ds.LAB1_DS.main(LAB1_DS.java:34)

第 52 行:

  return tail.getElement(); 

第 34 行:

plist.removeNode(plist.getlast());

请帮助我,我似乎无法弄清楚,实验室助理说它应该可以工作(getlast())

最佳答案

问题出在您的 addfirst 方法中。您正在将 head 重新分配为 null,而不是将 tail 重新分配给 head。正确版本:

public void addfirst(E value) {
Node<E> newNode = new Node(value, null);
newNode.next = head;
head = newNode;
size = size + 1;
if (size == 1)
tail = head;
}

进行此更改后,输出为:

-----------Display method-----------
Linked list (1):person{name='Hesssa', state='SA'}

-----------Display method-----------
Linked list (1):person{name='Nora', state='SA'}
Linked list (2):person{name='Hesssa', state='SA'}

-----------Display method-----------
Linked list (1):person{name='Nora', state='SA'}
Linked list (2):person{name='Farah', state='SA'}
Linked list (3):person{name='Hesssa', state='SA'}
Nora

-----------Display method-----------
Linked list (1):person{name='Nora', state='SA'}
Linked list (2):person{name='Farah', state='SA'}

旁注:

请参阅类和方法的命名约定。它们应该是蛇形的。例如,方法名称来自 addfirst -> addFirst。类名来自 singlylinkedlist -> SinglyLinkedList

关于java - 链表的 getlast() 中出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60053815/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com