gpt4 book ai didi

c - 隐藏类型的性质

转载 作者:行者123 更新时间:2023-11-30 17:40:36 26 4
gpt4 key购买 nike

为了学习链表(双向链接)的概念,我正在编写一堆简单的常用函数来处理它们。大多数函数都采用指向 DL_List 的指针结构体,它是句柄,不仅包含第一个和最后一个元素的链接,还包含列表中的当前位置。

       typedef struct DL_Node {
struct DL_Node *next = NULL;
struct DL_Node *prev = NULL;

int data;
} DL_Node;

typedef struct DL_List {
struct DL_Node *first = NULL;
struct DL_Node *last = NULL;

struct DL_Node *cur = NULL;
} DL_List;

因此我总是必须传递一个指向句柄的指针。

   int main() {...
DL_List list; // lives on the stack

init_list(&list, 9);
push(&list, 7);
append(&list, 10);
insert_after(&list, -31);
print_list(&list);
...}

所以问题是:有什么办法可以避免重读传递 &list 吗?也许是类型定义?

github repo

最佳答案

你可以做到

int main() {...
DL_List list; // lives on the stack
DL_List * plist = &list;

init_list(plist, 9);
push(plist, 7);
append(plist, 10);
insert_after(plist, -31);
print_list(plist);
...}

如果您想避免输入 &,但它并不能真正为您节省一些东西。

一个好的编译器应该优化它。

关于c - 隐藏类型的性质,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21398241/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com