繁体   English   中英

您何时知道需要分配链接列表和结构

[英]When do you know you need to allocate for a link list and struct

什么时候应该分配内存,为什么我的结构似乎错误? 如何重复创建链接列表的节点?

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

#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;
}

好的,首先,您要分配“篮子”并尝试将它们分配为“字符串”,这是在第一个“篮子”创建中发生的。

接下来,您尝试将一个指向char数组的指针分配给单个字符。 尝试在您的结构中使用指针值。 这在所有购物篮创建中都会发生。

另外,您然后尝试将“ Basket”转换为不同类型的“ String”。

您也可能在以“ a”,“ b”和“ c”表示的字符串中使用以NULL结尾的字符串

据我所知,其余的应该没问题

您需要先修正一些错字:

  • 如果您有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该指针的副本。 相反,我们传递其地址,以便函数可以转到引用的位置并对其进行更改。

看看在onlinegdb中运行的代码

在CodeBlocks中运行的示例:

在此处输入图片说明

这是错误的:

node2->next = node;

建议

node1->next = node2; 
node2->next = NULL;`

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM