为什么一直显示段错误

为什么一直显示段错误?
本题要求编程实现两个函数,调用Createlist()函数采用头插法创建一个整数链表,遇到-1创建结束;调用Separate()函数实现按指定的值将链表分成两部分,前半部分的结点数值大于等于指定值,后半部的结点数值小于指定值。要求调整后的链表结点没有重复数据(重复结点只保留原来链表的第1个结点),且保持各组数据在原链表中的相对位置不变。

函数接口定义:
ListNode *Createlist(void);
void Seperate( int num, ListNode *head, ListNode **headB, ListNode **headL);
Createlist()函数根据输入的整数(数值>=0)采用头插法创建链表,遇到 -1 结束链表的创建,且 -1不作为链表结点数据。

Separate()函数实现按指定的值将链表分成两部分,前半部分的结点数值大于等于指定值,后半部的结点数值小于指定值。要求调整后的链表结点没有重复数据(重复结点只保留原来链表的第1个结点),且保持各组数据在原链表中的相对位置不变。

裁判测试程序样例:

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

struct ListNode{
    int data;
    struct ListNode *next;
};

struct ListNode *Createlist(void);
void Seperate( int num,struct ListNode *head,struct ListNode **headN);
void Printlist(struct ListNode *head);
int main()
{
    struct ListNode *head=NULL,*head1=NULL;
    int  num;

    head = Createlist();  //使用在头部插入的方式形成链表 
    scanf("%d",&num);
    
    Printlist(head);  //输出创建好的链表数据 

    Seperate( num,head, &head1);
    Printlist(head1);  //输出新链表 
    return 0;
}

void Printlist(struct ListNode *head)
{
    struct ListNode *p=head;
    if(p != NULL)
    {
        for ( ; p != NULL; p = p->next )
                printf("%d ", p->data);        
    }
    else
    {
        printf("No Chain!");
    }
    printf("\n");
}

/* 请在这里填写答案 */


我的答案

/* 请在这里填写答案 */
int n;
struct ListNode *Createlist(void)
{
    struct ListNode *h,*p;
    int x=0,i=0;
    int a[1001];
    scanf("%d",&a[0]);
    i++;
    while(a[i-1]!=-1){
        scanf("%d",&a[i]);
        i++;
    }
    n=i-1;
    for(i=0;i<n;i++){
        p=(struct ListNode*)malloc(sizeof(struct ListNode));
        p->data=a[i];
        if(i==0){
            h=p;
            p->next=NULL;
        }else{
            p->next=h;
            h=p;
        }
    }
    
    return h;
}
void Seperate( int num,struct ListNode *head,struct ListNode **headN)
{
    struct ListNode *p,*q,*h=NULL,*r,*t,*s;
    int i,j,flag=0,x;
    t=head;
    for(i=1;t->next!=NULL;i++){
        p=head;
        x=t->next->data;
        for(j=0;j<i;j++){
            if(p->data==x){
                flag++;
                s=t->next;
                t->next=s->next;
                free(s);
            }
            p=p->next;
        }
        i-=flag;
        if(flag==0){
            t=t->next;
        }
        flag=0;
    }
    p=head;
    while(p!=NULL){
        if(p->data>=num){
            r=(struct ListNode*)malloc(sizeof(struct ListNode));
            r->data=p->data;
            if(h==NULL){
                h=r;
                q=r;
            }else{
                q->next=r;
                q=r;
            }
        }
        p=p->next;
    }
    p=head;
    while(p!=NULL){
        if(p->data<num){
            r=(struct ListNode*)malloc(sizeof(struct ListNode));
            r->data=p->data;
            if(h==NULL){
                h=r;
                q=r;
            }else{
                q->next=r;
                q=r;
            }
        }
        p=p->next;    
    }
    q->next=NULL;
    *headN=h;
}


img

执行抛异常了
通常是指针飞了导致的
Createlist的时候,不要先存数组,直接存链表
题目没有说只有1000个数,而你数组只有1001,估计是越界了

两个函数写的都有问题,见修改注释,供参考:

#include <stdio.h>
#include <stdlib.h>
struct ListNode {
    int    data;
    struct ListNode* next;
};

struct ListNode* Createlist(void);
void Seperate(int num, struct ListNode* head, struct ListNode** headN);
void Printlist(struct ListNode* head);

int main()
{
    struct ListNode* head = NULL, * head1 = NULL;
    int  num;

    head = Createlist();  //使用在头部插入的方式形成链表 
    scanf("%d", &num);

    Printlist(head);  //输出创建好的链表数据 

    Seperate(num, head, &head1);
    Printlist(head1);  //输出新链表 
    return 0;
}

void Printlist(struct ListNode* head)
{
    struct ListNode* p = head;
    if (p != NULL)
    {
        for (; p != NULL; p = p->next)
            printf("%d ", p->data);
    }
    else
    {
        printf("No Chain!");
    }
    printf("\n");
}

/* 请在这里填写答案 */
//int n;
struct ListNode* Createlist(void)
{
    struct ListNode* h = NULL, * p = NULL;
    //int x = 0, i = 0;
    int  a;   //a[1001];
    //scanf("%d", &a[0]);
    //i++;
    //while (a[i - 1] != -1) {
    //    scanf("%d", &a[i]);
    //    i++;
    //}
    //n = i - 1;
    while (1) {    //for (i = 0; i < n; i++) {
        scanf("%d", &a);
        if (a == -1)   break;
        p = (struct ListNode*)malloc(sizeof(struct ListNode));
        p->data = a;
        //if (i == 0) {
        //    h = p;    
        //  p->next = NULL;
        //}else {
        p->next = h;
        h = p;
        //}
    }
    return h;
}
void Seperate(int num, struct ListNode* head, struct ListNode** headN)
{
    struct ListNode* p = NULL, * h = NULL, * t = NULL, * s = NULL;
    int i, j, flag = 0, x;
    t = head;
    for (i = 1; t->next != NULL; i++) { //保留链表去重
        p = head;
        x = t->next->data;
        for (j = 0; j < i; j++) {
            if (p->data == x) {
                flag++;
                s = t->next;
                t->next = s->next;
                free(s);
            }
            p = p->next;
        }
        i -= flag;
        if (flag == 0) {
            t = t->next;
        }
        flag = 0;
    }
    p = head; *headN = NULL;     //以下修改
    while (p) {
        t = p;
        p = p->next;
        t->next = NULL;
        if (t->data < num)
        {
            if (!(*headN))
                *headN = t;
            else {
                h = *headN;
                while (h->next) h = h->next;
                h->next = t;
            }
        }
        else {
            if (!(*headN))
                *headN = t;
            else {
                h = *headN; s = NULL;
                while (h && h->data > num) s = h, h = h->next;
                if (!s) {
                    t->next = *headN;
                    *headN = t;
                }
                else {
                    t->next = s->next;
                    s->next = t;
                }
            }
        }
    }
}
#if 0       //修改,以下代码删除
    p = head;
    while (p != NULL) {
        if (p->data >= num) {
            r = (struct ListNode*)malloc(sizeof(struct ListNode));
            r->data = p->data;
            if (h == NULL) {
                h = r;
                q = r;
            }
            else {
                q->next = r;
                q = r;
            }
        }
        p = p->next;
    }
    p = head;
    while (p != NULL) {
        if (p->data < num) {
            r = (struct ListNode*)malloc(sizeof(struct ListNode));
            r->data = p->data;
            if (h == NULL) {
                h = r;
                q = r;
            }
            else {
                q->next = r;
                q = r;
            }
        }
        p = p->next;
    }
    q->next = NULL;
    *headN = head;
}
#endif
不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 关于该问题,我找了一篇非常好的博客,你可以看看是否有帮助,链接:段错误

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^