[英]Java Doubly linked list addLast() and removelast() method
我正在使用 java 處理雙向鏈表,它們維護對一個稱為“哨兵節點”或“無”的特殊節點的引用。 我只有一個構造函數來創建空列表。
對於我的 addlast class 是將 elem 添加到列表的末尾。 當我測試我的代碼時,控制台一直說“無法讀取字段“next”,因為“this.tail”是 null“誰能解釋為什么以及如何解決它?
例如:在 DList.addLast(DList.java:79) 在 DList.main(DList.java:205)
import java.util.*;
public class DList implements Iterable<String> {
private static class DListNode {
public String data;
public DListNode next;
public DListNode previous;
}
//Returns an iterator over the list as inner class
private class DListIterator implements Iterator<String> {
private DListNode pointer;
public DListIterator() {
if(nil.next ==nil)
pointer = nil;
else
pointer = nil.next;
}
public boolean hasNext() {
return pointer.next!=null;
}
public String next() {
if (!hasNext())
throw new NoSuchElementException();
return pointer.next.data;
}
}
private DListNode nil;
private DListNode head;
private DListNode tail;
private int numElements;
//create empty list.
public DList() {
nil = new DListNode();
nil.previous = nil;
nil.next = nil;
nil.data = null;
numElements = 0;
}
/*
* //create list with element. public DList(String elem) { DListNode temp = new
* DListNode(); this.tail = this.head = temp; numElements = 1; }
*/
//adds elem to the front of the list
public void addFirst(String elem) {
DListNode newHead = new DListNode();
newHead.data = elem;
newHead.next = head;
newHead.previous = nil;
if(head==null)
head = newHead;
else {
head.previous = newHead;
}
head = newHead;
numElements++;
}
//adds elem to the end of the list
public void addLast(String elem) {
DListNode newTail = new DListNode();
newTail.data = elem;
newTail.previous = tail;
//If the Linked List is empty, then make the new node as head
if (head == nil)
head = tail = newTail;
else { //Change the next of last node
newTail = tail.next;
tail = newTail;
}
tail = newTail;
numElements++;
}
//get the first value from node
public String getFirst() {
if(head==nil)
throw new NoSuchElementException("This is empty node.");
return head.data;
}
//get the last value from node
public String getLast() {
DListNode last = head;
while (last.next != null)
last = last.next;
if(last==null)
throw new NoSuchElementException("This is empty node.");
return last.data;
}
//get value of particular index
public String get(int index) {
//looking start at index 0
DListNode current = head;
if(index<0 || index>numElements)
throw new IndexOutOfBoundsException("The index is out of bound");
else {
for(int i=0; i<index; i++)
current = current.next;
}
return current.data;
}
//changes the value at “position” index and returns the old value
public String set(int index, String value) {
DListNode temp = new DListNode();
if(index<0 || index>numElements)
throw new IndexOutOfBoundsException("The index is out of bound");
else {
DListNode current = head;
for (int i = 0; i < index; i++) {
current = current.next;
}
DListNode previous = current.previous;
previous.next = temp;
temp.previous = previous;
temp.next = current;
current.previous = temp;
numElements++;
return temp.data;
}
}
//Returns true if obj appears in the list and false otherwise.
public boolean contains(Object obj) {
return nil.data.equals(obj);
}
//Returns the index of obj if it is in the list and -1 otherwise
public int indexOf(Object obj) {
if(contains(obj))
get(numElements);
return -1;
}
public int size() {
return numElements;
}
public String toString() {
String result = " ";
DListNode temp = head;
while(temp!=null) {
result += temp.data + " ";
temp = temp.next;
}
return result;
}
//removes the front element of the list and return it
public String removeFirst() {
if(size()==0)
throw new NoSuchElementException();
DListNode tmp = head;
head = head.next;
head.previous = nil;
numElements--;
return tmp.data;
}
//removes the last element of the list and returns it
public String removeLast() {
if(size()==0)
throw new NoSuchElementException();
DListNode tmp = tail;
tail = tail.next;
tail.next = nil;
numElements--;
return tmp.data;
}
//Returns an iterator over the list.
public Iterator<String> iterator() {
return new DListIterator();
}
public static void main(String[] args) {
//create a DoublyLinkedList object
DList dl_List = new DList();
//Check the addFirst method
dl_List.addFirst("is");
System.out.println(dl_List);
dl_List.addFirst("This");
System.out.println(dl_List);
//Check the addLast method
dl_List.addLast("Doubly");
System.out.println(dl_List);
dl_List.addLast("linked");
System.out.println(dl_List);
//check the getFirst method
System.out.println(dl_List.getFirst());
//check the getLast method
System.out.println(dl_List.getLast());
//test remove method
dl_List.removeFirst();
dl_List.removeLast();
System.out.println(dl_List);
//test get method
System.out.println(dl_List.get(1));
System.out.println(dl_List.size());
}
}
發生這種情況你沒有在構造函數中設置尾巴,所以他變成了 null。 要解決此問題,請在構造函數中設置tail或添加檢查語句以檢查tail是否不等於null,然后再訪問tail.next。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.