m0_59696689 2021-08-09 16:28 采纳率: 70.6%
浏览 27
已结题

c语言关于结构的编写错误

如图,split代表全部的数,然后如果输入的数中有0, 0之后的都归为after,之前的都归为before。

img
我对于结构中套结构有点不理解,所以发出来

//我写的代码
// Given a list with at least one node, and exactly one 0,
// split the list into a list with everything before the 0,
// and a list with the 0 and everything after.
// Return a malloced split_list struct with each of these lists.
struct split_list *split(struct node *head) {
    struct split_list *p = malloc(sizeof (struct split_list));
    while (head != NULL) {
        if (head->data == 0) {
            while (head != NULL) {
                p->after->data = head->data;
                head = head->next;
                p->after = p->after->next;
            }
        } else {
            p->before->data = head->data;
            head = head->next;
            p->before = p->before->next;
        }
        head = head->next;
    }

    return p;

}
//相关代码
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

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

struct split_list {
    struct node *before;
    struct node *after;
};

struct split_list *split(struct node *head);
struct node *strings_to_list(int len, char *strings[]);
void print_list(struct node *head);

// DO NOT CHANGE THIS MAIN FUNCTION

int main(int argc, char *argv[]) {
    // create linked list from command line arguments
    struct node *head = strings_to_list(argc - 1, &argv[1]);

    struct split_list *list_split = split(head);
    printf("before = ");
    print_list(list_split->before);
    printf("after = ");
    print_list(list_split->after);

    return 0;
}





// DO NOT CHANGE THIS FUNCTION
// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
    struct node *head = NULL;
    int i = len - 1;
    while (i >= 0) {
        struct node *n = malloc(sizeof (struct node));
        assert(n != NULL);
        n->next = head;
        n->data = atoi(strings[i]);
        head = n;
        i -= 1;
    }   
    return head;
}

// DO NOT CHANGE THIS FUNCTION
// print linked list
void print_list(struct node *head) {
    printf("[");    
    struct node *n = head;
    while (n != NULL) {
        // If you're getting an error here,
        // you have returned an invalid list
        printf("%d", n->data);
        if (n->next != NULL) {
            printf(", ");
        }
        n = n->next;
    }
    printf("]\n");
}

  • 写回答

2条回答 默认 最新

  • CSDN专家-link 2021-08-09 16:34
    关注

    after指针都是空的,你就去操作data,必死无疑
    而且原有链表节点已经有现成的,不需要再申请新空间,直接把源链表节点挂到after和before链表就可以了

    //我写的代码
    // Given a list with at least one node, and exactly one 0,
    // split the list into a list with everything before the 0,
    // and a list with the 0 and everything after.
    // Return a malloced split_list struct with each of these lists.
    struct split_list *split(struct node *head) {
        struct split_list *p = malloc(sizeof (struct split_list));
        p->after = NULL
        p->before = NULL;
        struct node * a = p->after;
        struct node * b = p->before;
        while (head != NULL) {
            if (head->data == 0) {
                    if(p->after == NULL)
                    {
                          p->after = head;
                          a = p->after;
                    }
                    else
                    {
                          a->next = head;
                          a = a->next;
                    }
                }
            } else {
               if(p->before == NULL)
                    {
                          p->before= head;
                          b = p->before;
                    }
                    else
                    {
                          b->next = head;
                          b = b->next;
                    }
            }
            head = head->next;
        }
        return p;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 8月17日
  • 已采纳回答 8月9日
  • 创建了问题 8月9日

悬赏问题

  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!