2012-11-07 20 views
8

मैं ऐसे प्रोग्राम पर काम कर रहा हूं जो जावा की अंतर्निहित लिंक्ड लिस्ट क्लास का उपयोग नहीं करता है; मैं इसे खरोंच से बना रहा हूँ। मैं एक विधि लिखने के अलावा सब कुछ के साथ सफल रहा हूं जो एक नोड को लिंक की गई सूची की किसी विशेष स्थिति में सम्मिलित करता है।लिंक्ड सूची के मध्य में नोड डालने, और गलती से नल नोड डालने के साथ-साथ

मेरे पास एक विधि है जो एक विशेष नोड को "वर्तमान" नोड के रूप में सेट करती है। तो, उदाहरण के लिए, मैं एक लिंक्ड सूची है कि इस तरह दिखता है: ->कुत्तों ->अच्छा - -> बनाने बिल्लियों>जाते, और "वर्तमान" के बराबर है 2; इसका मतलब है कि "वर्तमान" नोड "कुत्तों" है।

यहाँ से, मान लीजिए कि मैं की स्थिति में एक नया नोड सम्मिलित करना चाहते हैं चलो "वर्तमान" जिसका जानकारी क्षेत्र पढ़ता और। यदि सही ढंग से किया, अंतिम लिंक्ड सूची हो जाएगा: बिल्लियों ->और ->कुत्तों ->अच्छा - -> बनाने>जाते; "और" स्थिति "कुत्तों" को प्रतिस्थापित करेगा।

तो मेरी समस्या यहां है: मेरी विधि स्थिति दो पर एक नया नोड डालने के लिए काम करती है, लेकिन नए बनाए गए नोड को पूर्व-मौजूदा नोड्स से जोड़ने में कुछ गड़बड़ हो रही है। न केवल मैं अपना नया नोड सूची में डाल रहा हूं, लेकिन मैं "कुत्तों" से पहले कोई जानकारी नहीं के साथ एक नोड भी डाल रहा हूं। मेरी कोड वर्तमान में चलाता है के रूप में, उत्पादन इस प्रकार है: ->और - बिल्लियों> (खाली) ->कुत्तों ->बनाने ->अच्छा ->पालतू जानवर

मैं 99.9% सुनिश्चित करता हूं कि समस्या कोड के (यदि वर्तमान! = शून्य) भाग में है, तो मैं इसे ठीक करने का तरीका नहीं समझ सकता।

कोई विचार नहीं है कि मैं नोड के अलावा एक खाली नोड क्यों डाल रहा हूं, मैं वास्तव में जोड़ना चाहता हूं?

public void insert() { 

    System.out.println("Please enter the text you wish to insert, or type \"end\" if you are done inserting."); 
    String theString; 
    theString = console.nextLine(); 

    while (!theString.equals("end")){ 
     newNode = new Node(); 
     newNode.info = theString; 
     newNode.next = null; 

     if (first == null){ 
      first = newNode; 
      last = newNode; 
     } else if (current != null){ 
      Node p = new Node (current.info, current.next); 
      current.info = newNode.info; 
      current.next = p; 
     } 
     else { 
      last.next = newNode; 
      last = newNode; 
     } 

     System.out.println("Please enter the text you wish to insert, or type \"end\" if you are done inserting."); 
     theString = console.nextLine(); 
    } 
} 

संपादित

पूरे कार्यक्रम काफी लंबा है, लेकिन यहाँ "setline" विधि है जो जो कुछ भी स्थिति उपयोगकर्ता पर उनके नोड डालने के लिए चाहता है के लिए वर्तमान बराबर सेट करता है। यह एक पैरामीटर "int लाइन" लेता है जो उपयोगकर्ता संकेत के माध्यम से प्राप्त होता है।

public Node setLine(int line) { 

    int index = 0; 
    current = first; 
    while (index < line) { 
     previous = current; 
     current = current.next; 
     index++; 
    } 
    return current; 
} 
+0

इस प्रकार की चीज़ आपको बस, चरण-दर-चरण, आमतौर पर System.out.println कॉल के साथ काम करना है। यह वास्तव में एक आम समस्या है, लेकिन आपको खुद को समझने की जरूरत है। –

+1

ध्यान दें कि आप इस मामले में जहां आप दूसरी नई नोड बनाने का शक होना चाहिए - आप केवल कभी भी किसी भी डालने के लिए एक की जरूरत है चाहिए। –

+0

'कंसोल', 'java.util.Scanner' क्या है? –

उत्तर

0

यहां एक कोड है जो नोड को ठीक से सम्मिलित करता है। यह एक अच्छा प्रारंभिक बिंदु होना चाहिए, शुभकामनाएं (आप यहां और अधिक पढ़ सकते हैं: http://www.algolist.net/Data_structures/Singly-linked_list/Insertion)।

public class SinglyLinkedList { 

     public void addLast(SinglyLinkedListNode newNode) {  
      if (newNode == null)  
        return;  
      else {  
        newNode.next = null;  
        if (head == null) {  
         head = newNode;  
         tail = newNode;  
        } else {  
         tail.next = newNode;  
         tail = newNode;  
        }  
      }  
     } 

     public void addFirst(SinglyLinkedListNode newNode) {  
      if (newNode == null)  
        return;  
      else {  
        if (head == null) {  
         newNode.next = null;  
         head = newNode;  
         tail = newNode;  
        } else {  
         newNode.next = head;  
         head = newNode;  
        }  
      }  
     } 

     public void insertAfter(SinglyLinkedListNode previous,  
        SinglyLinkedListNode newNode) {  
      if (newNode == null)  
        return;  
      else {  
        if (previous == null)  
         addFirst(newNode);  
        else if (previous == tail) 
         addLast(newNode);  
        else {  
         SinglyLinkedListNode next = previous.next;  
         previous.next = newNode;  
         newNode.next = next;  
        }  
      }  
     }  
} 
+0

क्यों 'नया नोड' कभी शून्य होगा? और आप इसे अनदेखा करने के बजाय एनपीई क्यों नहीं फेंकेंगे? – EJP

-1

आप निम्न विधि है जिसके बीच में नोड सम्मिलित करता है, सूचकांक के आधार पर देख सकते हैं।

public boolean insertInMiddle(int index, int data){ 

    boolean isInserted = false; 

    Node node = new Node(data); 
    Node temp = head; 
    int i=0; 
    if(index >= 0 && index <= size()){ 
     isInserted = true; 
     if(index == 0){ 
      if(head !=null){ 
       node.nextNode = head; 
       head.prevNode = node; 
       head = node; 
      }else{ 
       head = node; 
       tail=node; 
      } 
     }else{ 
      while(i<index){ 
       temp = temp.nextNode; 
       i++; 
      }    
      if(temp == null){ 
       node.nextNode = temp; 
       node.prevNode = tail; 
       node.prevNode.nextNode = node; 
       tail=node; 
      }else{ 
       node.nextNode = temp; 
       node.prevNode = temp.prevNode; 
       temp.prevNode = node; 
       node.prevNode.nextNode = node; 
      } 
     } 
    }  
    return isInserted; 
} 

//Method to get the size 
public int size(){ 
    int size = 0; 

    Node node = head; 
    if(node !=null){ 
     while (node !=null){ 
      size++; 
      node = node.nextNode; 
     } 
    } 

    return size; 
} 
+0

क्या नहीं पूछा गया था। – EJP

संबंधित मुद्दे