从单个链表c节目中删除节点

时间:2013-03-23 10:24:21

标签: c computer-science singly-linked-list

当我在VS&amp ;;中构建程序时没有错代码块。然而,当我运行它时,它或者在我输入索引号后打破或者只是无限地显示字母......

#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;
}

4 个答案:

答案 0 :(得分: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);
}

什么东西......我希望你能得到它...... :)

答案 1 :(得分:0)

temp->item=value; - 当您未初始化时,您将取消引用temp。未定义的行为,崩溃和焚烧。

答案 2 :(得分:0)

在你的循环中,

    temp->item=value;

没有为temp分配空间。因此,这将崩溃!!

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

通常,您需要分配一个temp节点,并将其作为head分配给head = temp。缺少的另一点是,在创建新节点时,next应该NULL初始化为temp->next = NULL

removeNode

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

pre将指向要删除的节点,但temphead仍将指向列表的headfindNode不会修改temphead。因此tmphead->next = cur会修改head始终

答案 3 :(得分: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;
}

我修改了最后一部分并在main函数中添加了temp->next=NULL。这个程序现在运行得很好!