2013-08-27 9 views
6

मैं किसी लिंक किए गए सूची से नोड को हटाने के बारे में कैसे जा सकता हूं?सी लिंक की गई सूची से नोड निकालें

यहाँ मेरी कोड है:

void RemoveNode(Node * node, Node ** head) { 
    if (strcmp(node->state, (*(*head)->next).state) == 0) { 
     Node * temp = *head; 
     *head = (*head)->next; 
     free(temp); 
     return; 
    } 

    Node * current = (*head)->next; 
    Node * previous = *head; 
    while (current != NULL && previous != NULL) { 
     if (strcmp(node->state, (*current->next).state) == 0) { 
      Node * temp = current; 
      previous->next = current->next; 
      free(temp); 
      return; 
     } 
     current = current->next; 
     previous = previous->next; 
    } 
    return; 
} 

लेकिन मैं हो रही SEG दोष रहते हैं।

मुझे लगता है कि मैं कुछ बेवकूफ कर रहा हूं .... कोई विचार?

+1

मौजूदा पुन: असाइन करने से पहले 'पिछला = वर्तमान' की बजाय 'पिछला = पिछला-> अगला' क्यों करें? –

+0

इसके अलावा, अगर आपको सेगमेंटेशन दोष मिलते हैं, तो अपने प्रोग्राम को डीबगर में चलाएं। यह आपको रोक देगा जहां आपको अपनी समस्या है, और आप कॉलस्टैक और चर की जांच कर सकते हैं। कम से कम आपको कॉलस्टैक को शामिल करने के लिए अपना प्रश्न संपादित करना चाहिए, और बताएं कि प्रदान किए गए कोड में क्रैश होता है। –

+0

इसके अलावा, क्या आप हमेशा * वैध * (* सिर) -> अगला 'सूचक है? अगर सूची खाली है तो क्या होगा? अगर सूची में केवल एक नोड है तो क्या होगा? –

उत्तर

5

मेरा अनुमान है:

void RemoveNode(Node * node, Node ** head) { 
    if (strcmp(node->state, ((*head)->state) == 0) { 
     Node * temp = *head; 
     *head = (*head)->next; 
     free(temp); 
     return; 
    } 

    Node * current = (*head)->next; 
    Node * previous = *head; 
    while (current != NULL && previous != NULL) { 
     if (strcmp(node->state, current->state) == 0) { 
      Node * temp = current; 
      previous->next = current->next; 
      free(temp); 
      return; 
     } 
     previous = current; 
     current = current->next; 
    } 
    return; 
} 
+4

यदि आपने बताया कि आपने क्या बदल दिया है तो यह अधिक उपयोगी होगा। –

+0

मैंने 'अगले' मान के साथ वर्तमान मूल्य के साथ तुलना बदल दी, और मैंने पिछले लूप में पिछले के अपडेट को बदल दिया। – Jiminion

+0

धन्यवाद, यह बिल्कुल ठीक था! – Travv92

2

मैं सुझाव है कि आप प्रत्यावर्तन के साथ ऐसा करने की कोशिश, एक "डबल सूचक" के लिए जरूरत से बचने के लिए। यह तर्क को बेहद सरल बना देगा। This link में यह एक बहुत अच्छा स्पष्टीकरण और कार्यान्वयन करने का कार्यान्वयन है। यदि आप रिक्त लिंक्ड सूची से नोड को निकालने का प्रयास करते हैं तो यह विशेष रूप से भी काम करेगा।

Node *ListDelete(Node *currP, State value) 
{ 
    /* See if we are at end of list. */ 
    if (currP == NULL) 
    return NULL; 

    /* 
    * Check to see if current node is one 
    * to be deleted. 
    */ 
    if (currP->state == value) { 
    Node *tempNextP; 

    /* Save the next pointer in the node. */ 
    tempNextP = currP->next; 

    /* Deallocate the node. */ 
    free(currP); 

    /* 
    * Return the NEW pointer to where we 
    * were called from. I.e., the pointer 
    * the previous call will use to "skip 
    * over" the removed node. 
    */ 
    return tempNextP; 
    } 

    /* 
    * -------------- RECURSION------------------- 
    * Check the rest of the list, fixing the next 
    * pointer in case the next node is the one 
    * removed. 
    */ 
    currP->next = ListDelete(currP->next, value); 


    /* 
    * Return the pointer to where we were called 
    * from. Since we did not remove this node it 
    * will be the same. 
    */ 
    return currP; 
} 
संबंधित मुद्दे