linkedlist:count = count-> next给出分段错误

时间:2017-11-23 16:46:10

标签: c pointers linked-list segmentation-fault

#include<stdio.h>
#include<malloc.h>
typedef struct nde{
    int data;
    struct nde *next;
}node,*pnode;
void inst_beg(node *,int);
void inst_end(node *,int);
void inst_any(node *,int,int);
int del_begin(node *);
int del_end(node *);
int del_any(node*,int);
void display(node *);
main()
{
    pnode head= (node *)malloc(1*sizeof(node));
    head->data=0;
    head->next=NULL;
    inst_any(head,1,1);
    inst_any(head,2,2);
//  inst_any(head,3,3);
    display(head);
}

void inst_any(node *head,int pos, int data){
    pnode nd=(node *)malloc(1*sizeof(node));
    nd->data=data;
    //pnode count=(node *)malloc(1*sizeof(node));
    pnode count;
    count=head;
    printf("head: %p",head);
    printf("count: %p",count);
    int i=0;
    while(i<pos-1){
        count=count->next; //Problem is here for inst_any(phead,2,2) 
    }
    nd->next=count->next;
    count->next=nd;
    //printf("done");   
}

void display(node * head){
    pnode count=head;
    while(count->next!=NULL){
        printf("%d",count->data);
        count=count->next;
    }
}

count的值在循环内变为null,所以当第二次调用inst_any(head,2,2)时,我们无法顺从它。用gdb检查第一次计数是否成功指向head。同样也是第二次发生。在count = head之后,它第二次给出正确的值。不知道之后发生了什么。为什么当它进入循环计数的值变为零时。

3 个答案:

答案 0 :(得分:3)

看看这段代码:

while(i<pos-1){
    count=count->next; //Problem is here for inst_any(phead,2,2) 
}

整个循环。因此,您的条件i<pos-1可能会立即失效....或者它是真的且保持为真,因为您永远不会在循环中修改ipos

在后一种情况下,您走一个链表。最后,您会找到结尾(count->nextNULL)并仍然将此NULL分配给count。在下一次迭代中,您尝试取消引用NULL以访问->next。尝试取消引用NULL未定义的行为,分段错误是典型的后果。

转到并重新考虑您的计划(例如,检查您的循环条件中count->next是否仍然不是NULL

答案 1 :(得分:1)

我已使用以下评论更正了您的代码。请在代码中阅读我的评论,否则你无法意识到自己的错误。希望这会对你有所帮助。

typedef struct nde{
    int data;
    struct nde *next;
}node,*pnode;

void inst_beg(node *,int);
void inst_end(node *,int);
void inst_any(node *,int,int);
int del_begin(node *);
int del_end(node *);
int del_any(node*,int);
void display(node *);


void main()
{
    pnode head= (node *)malloc(sizeof(node)); //No need to multiply by one
    head->data=0;
    head->next=NULL;
    inst_any(head,1,1);
    inst_any(head,2,2);
    inst_any(head,3,3);
    display(head);
    inst_any(head,4,4); //I am adding this statement so that you can better understand where it going to be inserted
    display(head);
    inst_any(head,7,7);
}

void inst_any(node *head,int pos, int data){
    pnode nd=(node *)malloc(sizeof(node));
    nd->data=data;
    //pnode count=(node *)malloc(1*sizeof(node));
    pnode count;
    count=head;
    printf("head: %p\n",head);
    printf("count: %p\n",count);
    int i=0;
    while(i < (pos-1)){
        if(count == NULL){
         printf("No position available for request pos =%d\n", pos);
         return;//This condition is important. If your position is not exist in the list and count reached the end of the list just return with a error message
       }
        count=count->next; //Problem is here for inst_any(phead,2,2)
        i++;//you must increment i
    }
    nd->next=count->next;
    count->next=nd;//Here count must not be null, else it will create Segmentation fault. Therefore inside from while loop above we have checked whether it is null or not. If null return from this method. 
    //printf("done\n");
}

void display(node * head){
    pnode count=head;
    while(count!=NULL){//You have to correct it to print last node of the list
        printf("%d\n",count->data);
        count=count->next;
    }
}

答案 2 :(得分:1)

该功能的已发布代码:inst_any()初始化data字段,但无法初始化next字段。

推荐:

nd->data=data;
nd->next = NULL;

然后循环:

while(i<pos-1){
    count=count->next; //Problem is here for inst_any(phead,2,2)
}

无法更新计数器i,因此循环永不退出。此外,当链表不包含足够的条目时,此循环将直接在列表末尾运行。所以循环还需要检查count->next是不是NULL。