【问题标题】:Trouble with signature of function to add node to end of linked list将节点添加到链表末尾的函数签名出现问题
【发布时间】:2013-10-05 03:46:57
【问题描述】:

在我正在编写的程序中,我需要一个链表,所以它是一个非常具体的实现。它需要:

  1. 能够将节点添加到末尾
  2. 能够删除数据与指定值匹配的节点

数据是一个cstring,长度不超过20个字符。我对 C 语言不是很熟悉,并且遇到以下签名 void addToEnd(llist root, char entery[51]) 的错误。我尝试用node 替换llist,但错误是“未知类型名称节点”。我怎样才能摆脱它?

这是代码

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

typedef struct node
{
  char entery[51];
  struct node* next;
} llist;

/*may be losing root address permanently*/
void addToEnd(llist root, char entery[51])
{
    while(root->next != NULL)
        root = root->next;
    node last = malloc(sizeof(struct node));
    root->next = last;
    strcpy(last, entery);
}

int main()
{
    struct node *root = malloc(sizeof(struct node));
    root->next = NULL;
    strcpy(root->entery, "Hello");

    struct node *conductor = root;//points to a node while traversing the list

    if(conductor != 0)
        while(conductor->next != 0)
            conductor = conductor->next;

    /* Creates a node at the end of the list */
    conductor->next = malloc(sizeof(struct node));

    conductor = conductor->next;

    if (conductor == NULL)
    {
        printf( "Out of memory" );
        return EXIT_SUCCESS;
    }
    /* initialize the new memory */
    conductor->next = NULL;
    strcpy(conductor->entery, " world\n");

    addToEnd(root, " at the");
    addToEnd(root, " end");

    /*print everything in list*/
    conductor = root;
    if(conductor != NULL)
    {
        while(conductor->next != NULL)
        {
            printf("%s", conductor->entery);
            conductor = conductor->next;
        }
        printf("%s", conductor->entery);
    }

    return EXIT_SUCCESS;
}

我不清楚的一件事是,在我看到的所有示例中,它们是typedef the struct。为什么?让我详细说明一下:你怎么知道你是想通过node 还是struct node。另外,我真的不明白这一点,struct node 并不比单个 typedef 名称长多少。

【问题讨论】:

  • 使用您的 typedef,lliststruct node 相同...
  • 为什么过去的结构节点,节点是类型,为什么需要结构?
  • @Celeritas 在C 中,struct node 是一种类型,但不是node。在 C++ 等其他语言中,somename 等价于 struct somename

标签: c linked-list malloc


【解决方案1】:

问题:

  1. 第 12 行:void addToEnd(llist root, char entery[51]) 应为 void addToEnd(llist *root, char entery[51])。这里的 root 必须是指针类型,否则你实际上不能在函数内部修改它的值并使其在函数外部可见。

  2. 第 16 行:node last = malloc(sizeof(struct node)); 应为 struct node *last = malloc(sizeof(struct node));。因为在 C 中你必须使用关键字struct 来引用类型名称,并且它应该是一个指针,否则它不能用 malloc 初始化。

至于您的typedef 问题,我相信它是可选的,人们只是为了方便而使用它。就个人而言,我不经常在struct 上使用typedef

已编辑:

您的代码也带有错误。抱歉,我之前只关注语法。

请注意,C 语言中的malloc 并不能保证分配的内存是零分配的,它实际上可以是内部的任何东西。所以需要手动填写:在addToEnd末尾添加一行last-&gt;next = NULL;

【讨论】:

  • @Celeritas 我已经在帖子中对其进行了编辑,这是关于last-&gt;next = NULL; 的。 :)
【解决方案2】:

要引用你的链表struct,使用struct node,在typedef之后,也可以使用llist。您也可以像链接的问题一样使用。

typedef struct node
{
  char entery[51];
  struct node* next;
} node;

在这种样式中,您可以使用nodestruct node 相同。

您面临的语法错误是,您误用了箭头运算符-&gt;,它与struct指针 一起使用。对于struct,使用点运算符.

所以对于函数

void addToEnd(llist root, char entery[51])
{
    while(root->next != NULL)
        root = root->next;

你应该传入一个指针:

void addToEnd(llist* root, char entery[51])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-02
    • 2013-11-13
    • 1970-01-01
    相关资源
    最近更新 更多