gpt4 book ai didi

c - 您什么时候知道需要分配链接列表和结构

转载 作者:行者123 更新时间:2023-11-30 16:39:34 25 4
gpt4 key购买 nike

我应该什么时候分配内存?为什么我的结构看起来是错误的?如何以重复的方式创建链接列表的节点?

编辑:
struct Basket 中的 char s;char *s;
添加了 display()
printf("%c\n", node->s);printf("%s\n", node->s);display 中()
删除了brifer显示的一些重复代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 100

struct Basket {
char *s;
struct Basket *next;
}
/*
creat_nodes(struct Basket *node) {
struct Basket *newnode = (struct Basket*) malloc(sizeof(struct Basket));
newnode = (struct String *) malloc(sizeof(struct String));
newnode->s = (char *) malloc(MAXLEN);
strcpy(newnode->s, "a character or string");
//link next, EDGE CASE IF it is LAST NODE?
// if (node->next == NULL) return
// else newnode-next->node
}

show_strings(struct Basket *list) {
struct String *pt; // WHY THIS ?
pt = list // because always because list always point to the first;
while(1) {
if(pt == NULL) break;
Printf(pt->s);
pt = pt->next; // does 'pt->=next' work?
}
}

*/

void display(struct Basket *node)
{
while (node != NULL)
{
printf("%s \n", node->s);
node = node->next;
}
}
int main(){
// DUMMY VERSION/Test Version of create_nodes()
struct String *node;
node = (struct Basket *) malloc (sizeof(struct Basket));
node->s = (char *) malloc(100);
strcpy(node->s, "Hello");
node->next = NULL; // RIGHT TO LEFT

struct String *node2;
node2 = (struct Basket *) malloc (sizeof(struct Basket));
node2->s = (char *) malloc(100);
strcpy(node2 --> s, "World");
node2->next = node;

//creat_nodes(node);
return 0;
}

最佳答案

您有一些拼写错误需要首先修复:

  • 如果您有一个 Basket 链表,则下一个指针必须为 struct Basket * 类型:

    struct Basket {
    char *s;
    struct String *next; //here needs to be struct Basket *
    };
  • 您有一个字符串列表,因此 display 函数必须使用 %s 而不是 %c 相应地显示它们

    void display(struct Basket *node){
    while (node != NULL)
    {
    printf("%c", node->s); //<---------here
    node = node->next;
    }

对于creat_nodes函数,它必须接收链表的头部,创建一个新节点并将其链接到某个现有节点。我假设您想将其链接到结尾:

//note that now the function also receives the string to be stored
void creat_nodes(struct Basket **node, const char* stringToStore) {
struct Basket *newnode = (struct Basket *)malloc(sizeof(struct Basket)); //create the new node
newnode->s = strdup(stringToStore); //story a copy of the string with strdup
newnode->next = NULL; //set the next as null, because this will be the last node

if (*node == NULL){ //if this is the first node is NULL there is no list
*node = newnode; //so the first node will be the new node
return;
}

struct Basket *current = *node;

//if there are nodes, navigate to the last one
while (current->next != NULL){
current = current->next;
}

current->next = newnode; //append the newnode as the next of the last one
}

现在可以在 main 中调用此函数,次数与您想要添加后续 Basket 节点的次数相同:

int main() {

struct Basket *listhead = NULL; //create the list

creat_nodes(&listhead, "a"); //add a node with "a"
creat_nodes(&listhead, "b"); //add a node with "b"
creat_nodes(&listhead, "c"); //add a node with "c"

display(listhead);

return 0;
}

请注意如何使用 &listhead 调用 creat_nodes 函数。当第一个列表节点为 NULL 时,我们希望在 creat_nodes 函数内更改它,但为了做到这一点,我们不能只传递指针本身,否则它会main 中此指针的副本。相反,我们传递它的地址,以便函数可以转到引用的位置并更改它。

Take a look at the code running in onlinegdb

在 CodeBlocks 中运行的示例:

enter image description here

关于c - 您什么时候知道需要分配链接列表和结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46995655/

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