gpt4 book ai didi

c - 如何用已有的元素填充链表?

转载 作者:行者123 更新时间:2023-11-30 18:10:45 26 4
gpt4 key购买 nike

我正在尝试用我拥有的元素创建一个链表,但我需要 24 个节点,而且我不想像这样结束:

head->next->next->next->next->next->next->next->id = 1;

如何防止这种情况发生?

我尝试创建类似的东西,但所有节点(显然)都指向相同的数据。

void init_board(block **head)
{
block temp;

temp.id=0;
temp.name="Start";
temp.price=0;
temp.rent=0;
temp.next = NULL;

*head = &temp;
(*head)->next = NULL;
(*head)->next = (block*) malloc(sizeof(block*));
temp = head->next;

temp.id=1;
temp.name="End";
temp.price=16000;
temp.rent=800;
temp.next = NULL;
}

最佳答案

I am trying to create a linked list with elements I have, but I need 24 nodes for this and I don't want to end up like this:

head->next->next->next->next->next->next->next->id = 1;How can I prevent this?

更新head(不仅是*head)

 block temp;
...
temp = head->next;

你不能这样做,因为tempstruct,但next是一个指针

I tried creating something likes but all nodes(obviously) are pointing the same data.

您需要为所有新元素分配一个新单元,包括当前放入堆栈中的第一个元素(从不返回堆栈中保存的内容的地址)

(head)->next = (block) malloc(sizeof(block*));

这不是您想要的,您需要分配一个 block ,而不是一个 block *

使用两个单元初始化的示例:

void init_board(block **plast)
{
*plast = malloc(sizeof(block));
(*plast)->id=0;
(*plast)->name="Start";
(*plast)->price=0;
(*plast)->rent=0;
(*plast)->next = malloc(sizeof(block));
plast = &(*plast)->next;
(*plast)->id=1;
(*plast)->name="End";
(*plast)->price=16000;
(*plast)->rent=800;
(*plast)->next = NULL;
}

int main()
{
block * l;

init_board(&l);
}
<小时/>

当然,如果你有 20 个 block 要初始化以扩展每个情况是不切实际的,可能这些值来自文件或类似的数组:

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

typedef struct block {
int id;
const char * name;
int price;
int rent;
struct block * next;
} block;

const block Boards[] = {
{ 0, "Start", 0, 0, NULL },
{ 2, "Intermediate", 123, 456, NULL },
{ 1, "End", 16000, 800, NULL }
};

void init_board(block **plast)
{
for (const block * b = Boards; b != Boards + sizeof(Boards)/sizeof(Boards[0]); ++b) {
*plast = malloc(sizeof(block));

(*plast)->id = b->id;
(*plast)->name = b->name;
(*plast)->price = b->price;
(*plast)->rent = b->rent;
(*plast)->next = NULL;
plast = &(*plast)->next;
}
}

int main()
{
block * blocks;

init_board(&blocks);

/* debug */
for (block * b = blocks; b != NULL; b = b->next)
printf("%d %s %d %d\n", b->id, b->name, b->price, b->rent);

/* free resources */
while (blocks != NULL) {
block * b = blocks;

blocks = blocks->next;
free(b);
}

return 0;
}

编译和执行:

pi@raspberrypi:/tmp $ gcc -pedantic -Wextra -Wall l.c
pi@raspberrypi:/tmp $ ./a.out
0 Start 0 0
2 Intermediate 123 456
1 End 16000 800

valgrind下执行:

pi@raspberrypi:/tmp $ valgrind ./a.out
==6819== Memcheck, a memory error detector
==6819== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==6819== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==6819== Command: ./a.out
==6819==
0 Start 0 0
2 Intermediate 123 456
1 End 16000 800
==6819==
==6819== HEAP SUMMARY:
==6819== in use at exit: 0 bytes in 0 blocks
==6819== total heap usage: 4 allocs, 4 frees, 1,084 bytes allocated
==6819==
==6819== All heap blocks were freed -- no leaks are possible
==6819==
==6819== For counts of detected and suppressed errors, rerun with: -v
==6819== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)
pi@raspberrypi:/tmp $

关于c - 如何用已有的元素填充链表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55990814/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com