Vì vậy, bài tập về nhà của tôi là: (Danh sách Liên kết Đôi) Thực hiện danh sách liên kết kép giữ các giá trị số nguyên. Điền danh sách với các giá trị [11, 22, 77, 44,55,66, 33]. Hiển thị nội dung của danh sách. Viết một phương thức để tách giá trị thành số lẻ đầu tiên, thậm chí sau này. Trong ví dụ của chúng ta, danh sách được sắp xếp lại phải là: [11, 77, 55, 33, 22, 44, 66].Không thể tạo Danh sách Liên kết được Nhân đôi Liên kết
Tôi có thể làm cho nó hoạt động và thực hiện những gì được cho là lên đến 6 con số, nhưng thêm số thứ 7 nó bị lỗi.
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;
}
}
Tại sao điều này bị giảm giá? Người hỏi thừa nhận nó là bài tập về nhà, cung cấp mã, và đưa ra một vấn đề cụ thể. Nó trông giống như một nỗ lực đức tin tốt, và họ không yêu cầu được trả lời. Downvotes cho nỗ lực ngăn cản mọi người đặt câu hỏi. – ambagesia