2013-03-23 111 views
0

當我在VS &代碼塊中構建程序時沒有任何問題。然而,當我運行它時,它要麼分手後,我的指數類型或只顯示字母無限......從單個鏈接列表中刪除節點c編程

#include<stdio.h> 
#include<stdlib.h> 


// list_node structure 

typedef struct list_node{ 
    int item; 
    struct list_node *next; 
}ListNode; 
//call functions that will be used 

void printNode(ListNode *head); 
int removeNode(ListNode **ptrhead, int index); 
ListNode *findNode(ListNode *head, int index); 

int main(){ 
    int index,value; 
    ListNode *head=NULL; 
    ListNode *temp; 
    //build the list 

    printf("Enter a value:"); 
    scanf("%d",&value); 
    do{ 
       temp->item=value; 
     if(head==NULL){ 
      head=(struct list_node *)malloc(sizeof(ListNode)); 
      temp=head; 
     } 
     else{ 
      temp->next=(struct list_node *)malloc(sizeof(ListNode)); 
      temp=temp->next; 
     } 
     printf("Enter a value:"); 
     scanf("%d",&value); 

    }while(value!=-1); 

    printf("Enter the index: "); 
    scanf("%d",&index); 
    // remove the node at the position indexed 

    // when I used debugger, I saw it didn't execute this step. Maybe there's something wrong with it.... 

    removeNode(&head,index); 

    printNode(head); 

    return 0; 
} 


void printNode(ListNode *head){ 
    if (head==NULL) 
     exit(0); 
    while(head!=NULL){ 
     printf("%d",head->item); 
     head=head->next; 
    } 
    printf("\n"); 
} 

ListNode *findNode(ListNode *head,int index){ 
    if(head==NULL||index<0) 
     return NULL; 
    while(index>0){ 
     head=head->next; 
     index--; 
    } 
    return head; 
} 


int removeNode(ListNode **ptrhead,int index){ 
    ListNode *pre,*cur,*temphead; 

    temphead=*ptrhead; 

    if(findNode(temphead,index)!=NULL){ 
     pre=findNode(temphead,index); 
     cur=pre->next; 
     temphead->next=cur; 
     return 0; 
    } 
    else 
     return -1; 
} 

回答

0
prev=NULL; 
cur=head; 
/* traverse the list until you find your target */ 
while (cur != NULL && cur->id != search_id) { 
    prev=cur; 
    cur=cur->next; 
} 
/* if a result is found */ 
if (cur != NULL) { 
    /* check for the head of the list */ 
    if (prev == NULL) 
    head=cur->next; 
    else 
    prev->next=cur->next; 
    /* release the old memory structure */ 
    free(cur); 
} 

怎麼了老兄....我希望你會得到它.. 。:)

0

temp->item=value; - 您在未初始化時取消temp的引用。未定義的行爲,崩潰和燒傷。

+0

我應該初始化[temp]還是[temp-> next]?我以爲我已經用[ListNode * temp = NULL;]初始化了[temp]。 – Baller 2013-03-24 06:42:37

0

在你的循環,

temp->item=value; 

沒有足夠的空間已被分配給溫度。因此,這將崩潰!

if(head==NULL){ 
     head=(struct list_node *)malloc(sizeof(ListNode)); 
     temp=head; 

通常,woud分配temp節點並將其分配給head作爲head = temp。其中缺少的另一點是創建一個新的節點時,該next應該被初始化爲NULLtemp->next = NULL

removeNode

if(findNode(temphead,index)!=NULL){ 
     pre=findNode(temphead,index); 

pre將指向節點被移除,但temphead會仍然指着名單的headfindNode不會修改temphead。所以tmphead->next = cur將修改head總是

+0

@ user2201968很高興能有所幫助。希望我的回答足夠了。 – Ganesh 2013-03-24 07:48:35

+0

int removeNode(ListNode ** ptrhead,int index){ \t ListNode * pre,* cur; \t if(index == - 1) \t \t return 1; (findNode((* ptrhead),(index))!= NULL){ \t else if(findNode \t \t cur = pre> next; \t \t pre> next = cur-> next; \t \t return 0; \t} \t else \t \t return 1; } – Baller 2013-03-24 08:07:42

0
int removeNode(ListNode **ptrhead,int index){ 
ListNode *pre,*cur; 
if (index==-1) 
    return 1; 
else if(findNode((*ptrhead),(index))!=NULL){ 
    pre=findNode((*ptrhead),(index-1)); 
    cur=pre->next; 
    pre->next=cur->next; 
    return 0; 
} 
else 
    return 1; 
} 

我修改的最後一部分,並在主函數中添加temp->next=NULL。這個程序現在可以運行良好!