0

当我在 VS 和代码块中构建程序时没有任何问题。虽然,当我运行它时,它要么在我输入索引号后坏掉,要么只是无限显示字母......

#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

4 回答 4

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

怎么了伙计……我希望你能明白……:)

于 2013-03-23T10:27:14.500 回答
0

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

于 2013-03-23T10:27:52.963 回答
0

在你的循环中,

    temp->item=value;

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

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

通常,您会分配一个temp节点并将其分配给headas head = temp。缺少的另一点是创建新节点时,next应将其初始化NULLtemp->next = NULL

removeNode,

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

pre将指向要删除的节点,但temphead仍将指向head列表。findNode不会修改temphead. 所以tmphead->next = cur会修改headalways

于 2013-03-23T10:30:12.380 回答
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到main函数中。这个程序现在可以运行良好了!

于 2013-03-24T08:14:30.203 回答