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

时间:2017-10-29 00:01:53

标签: c

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

修改
char s;中的char *s;struct Basket 添加了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;
}

3 个答案:

答案 0 :(得分:0)

好的,首先,你要分配“Baskets”并尝试将它们分配为“Strings”,这发生在第一个“Basket”创建中。

接下来,您尝试将指向chars数组的指针指定给单个字符。尝试在结构中使用指针值。这种情况发生在所有篮子创作中。

此外,您正在尝试将“篮子”投射到不同类型的“字符串”。

您可能也会将字符串空格终止,即“a”,“b”和“c”

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

答案 1 :(得分: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;
}

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

Take a look at the code running in onlinegdb

在CodeBlocks中运行示例:

enter image description here

答案 2 :(得分:0)

这是错误的:

node2->next = node;

建议

node1->next = node2; 
node2->next = NULL;`
相关问题