gpt4 book ai didi

java - 无法使双向链表工作

转载 作者:搜寻专家 更新时间:2023-10-31 20:23:03 25 4
gpt4 key购买 nike

<分区>

所以我的家庭作业是:(双向链表)实现一个包含整数值的双向链表。用值 [ 11, 22, 77, 44,55,66, 33 ] 填充列表。显示列表的内容。写一个方法,先把值分成奇数,再偶数。在我们的示例中,重新排序的列表应为:[ 11, 77, 55, 33, 22, 44, 66 ]。

我可以让它工作并为最多 6 个数字做它应该做的事情,但是添加第 7 个数字会出错。

Exception in thread "main" java.lang.NullPointerException
at MyList.sortData(MyList.java:100)
at Driver.main(Driver.java:24)



public class Driver {

/**
* @param args
*/
public static void main(String[] args) {

MyList<Integer> list = new MyList<Integer>();
list.add(11);
list.add(22);
list.add(77);
list.add(44);
list.add(55);
list.add(66);
list.add(33);

System.out.println("<<Original Data>>");
list.showData();
System.out.println();

System.out.println("<<Sorted Data>>");
list.sortData(list);
list.showData();

}

}





public class MyList<E>{

private Node<E> head;
private Node<E> tail;
int count;

public MyList(){
head = null;
tail = null;
count = 0;
}

public Node<E> add(E newData){
Node<E> current = new Node<E>(newData);

// case 1: list is empty
if (count == 0){
count ++;
head = current;
tail = current;
return current;
}

// case 2: list is not empty
if (count > 0){
count ++;
tail.setNext(current);
current.setPrior(tail);
tail = current;

return current;
}
return null;
}

public Node<E> removeFirst(){
Node<E> firstNode = head;

if (head == tail){
count = 0;
head = tail = null;
return firstNode;
}
else{
head = firstNode.getNext();
count --;
return firstNode;
}
}

public Node<E> removeLast(){
Node<E> lastNode = tail;
Node<E> current = head;

if (head == tail){
count = 0;
head = tail = null;
return lastNode;
}
else{
count --;
tail = tail.getPrior();
tail.setNext(null);
return lastNode;
}
}

public Node<E> removeAt(int index){
Node<E> current = head;
Node<E> next = null;
Node<E> prior = null;

if (index > count) return null;
if (index == 0) return removeFirst();
if (index == count -1) return removeLast();

else{
for(int i = 0; i < index; i++){
current = current.getNext();
}

next = current.getNext();
prior = current.getPrior();
prior.setNext(next);
next.setPrior(prior);
count--;
return next;
}
}

public void sortData(MyList<Integer> list){
Node<E> current = head;
int key = 0;

while(count > 0){
Node<E> data = current;

if((Integer)current.getData() % 2 == 0){
current = (Node<E>) list.removeAt(key);
list.add((Integer)data.getData());
}

else{
key++;
current = current.getNext();
}

count--;
}
}

public void showData(){
Node<E> current = head;
System.out.print("[");
while(current != null){
System.out.println(current.showData());
current = current.getNext();
}

System.out.print("]");
}
}



public class Node<E> implements Comparable<E>{
private E data;
private Node<E> next;
private Node<E> prior;

public Node(E newElement){
data = newElement;
next = null;
prior = null;

}

public Node<E> getPrior() {
return prior;
}

public void setPrior(Node<E> prior) {
this.prior = prior;
}

public E getData() {
return data;
}

public void setData(E data) {
this.data = data;
}

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

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

public String showData(){
String result = " " + data +" ";
return result;
}

@Override
public int compareTo(E otherData) {
String thisData = (String) data;
if (thisData.compareTo((String)otherData) == 0)
return 0;
else if (thisData.compareTo((String)otherData) < 0)
return -1;
else
return 1;
}
}

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