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

标签 c

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

编辑:
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/

相关文章:

c++ - 位设置问题

c - 尝试将多个源文件与 C 中的一个 header 链接

c - 如何处理错误

c - 通过地址(指针)传递

c - libjson-c : json_object_object_foreach segmentation fault

c - C 中多个嵌套结构的硬故障

c - 在函数中可以在链表开头插入元素,但从函数返回时起点为 NULL

c++ - 在 C++ 中递增 16 字节 char 数组

c - 如何在 C 中将循环字符串读入另一个更大的字符串

c - 如何在c中正确解析字符串