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日

悬赏问题

  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误