लिंक नहीं करना तो मेरी होमवर्क असाइनमेंट है: (दोगुना लिंक्ड सूचियों) एक दोगुना से जुड़े सूची पूर्णांक मूल्यों पकड़े लागू करें। मूल्यों के साथ सूची को पॉप्युलेट करें [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;
}
}
यह क्यों कम किया जा रहा है? पूछने वाले ने यह होमवर्क स्वीकार किया, कोड प्रदान किया, और एक ठोस समस्या दी। यह एक अच्छा विश्वास प्रयास की तरह दिखता है, और उन्होंने जवाब देने के लिए नहीं कहा था। प्रयासों के लिए डाउनवोट लोगों को प्रश्न पूछने से हतोत्साहित करते हैं। – ambagesia