如何创建双向链接列表,并且不使用ADT中的指针?

发布于 2025-01-17 20:01:58 字数 874 浏览 4 评论 0原文

现在我试图定义一个ADT并用它来创建一个链表,我想让它适合函数List newList(void);。我知道如果像 List* newList(void); 那样我该怎么办。如果我试图拟合List* newList(void);,那么我将像这样定义这个ADT,

typedef struct List{
    struct List *next;
    struct List *prev;
    int number;
} List;

但是如果我以这种方式定义,链表中的节点将都是指针,我不这样做不想用。现在我尝试用下面的方式定义链表。

typedef struct List{
    struct List next;
    struct List prev;
    int number;
} List;

但我收到错误消息:

List.h:4:17: error: field ‘next’ has incomplete type
    4 |     struct List next;
      |                 ^~~~
List.h:5:17: error: field ‘prev’ has incomplete type
    5 |     struct List prev;
      |                 ^~~~

我尝试将 typedef 移动到 .c 文件中(现在它在头文件中),但它仍然不起作用。有人可以帮我吗?

Now I am trying to define an ADT and use it to create a linked list, and I want to let it fit the function List newList(void);. I know what should I do if it's like List* newList(void);. If I am trying to do fit List* newList(void);, then I will define this ADT like

typedef struct List{
    struct List *next;
    struct List *prev;
    int number;
} List;

But If I define in this way, the nodes in linked list will be all pointers, which I don't want to use. Now I try to define linked list in the way below.

typedef struct List{
    struct List next;
    struct List prev;
    int number;
} List;

But I received the error message:

List.h:4:17: error: field ‘next’ has incomplete type
    4 |     struct List next;
      |                 ^~~~
List.h:5:17: error: field ‘prev’ has incomplete type
    5 |     struct List prev;
      |                 ^~~~

I tried to move typedef into .c file (now it's in header file), but it still not work. Can someone help me please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

物价感观 2025-01-24 20:01:58

但是,如果我以这种方式定义,链接列表中的节点将全部
指针,我不想使用

你是错误的。列表中的所有节点都将具有类型struct List。但是它们将动态分配。

至于此功能

List newList(void);

,这是没有意义的。

您需要声明该函数,

List * newList( int number );

例如,

List * newList( int number )
{
    List *node = malloc( sizeof( List ) );

    if ( node != NULL )
    {
        node->next = NULL;
        node->prev = NULL;
        node->number = number;
    }

    return node;
}

如果您不想在可以使用列表定义为数组时动态分配节点时,则可以动态分配节点。在这种情况下,您可以使用该功能将节点添加到列表中,

List newList( int number )
{
    List node = { .next = NULL, .prev = NULL, .number = number };
    return node;
}

但是在这种情况下,您需要声明一个确实可以描述列表的结构。实际上,您当前的结构仅声明列表的节点。

But If I define in this way, the nodes in linked list will be all
pointers, which I don't want to use

You are mistaken. All nodes in the list will have the type struct List. But they will be allocated dynamically.

As for this function

List newList(void);

then it just does not make a sense.

You need to declare the function like

List * newList( int number );

For example

List * newList( int number )
{
    List *node = malloc( sizeof( List ) );

    if ( node != NULL )
    {
        node->next = NULL;
        node->prev = NULL;
        node->number = number;
    }

    return node;
}

If you do not want to allocate nodes dynamically when you can use for example the approach when a list is defined as an array. In this case you can add nodes to the list using the function

List newList( int number )
{
    List node = { .next = NULL, .prev = NULL, .number = number };
    return node;
}

But in this case you need to declare one more structure that indeed will describe a list. Your current structure in fact declares just a node of a list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文