通过引用传递结构? [C]

发布于 2024-08-26 19:34:46 字数 271 浏览 9 评论 0原文

我正在尝试创建一个链接列表,在一个结构中包含两个单独的列表。也就是说,它包含“姓名”和“年龄”值。然后程序可以输出按姓名排序或按年龄排序的列表。所以我本质上需要两个链表。

但是,我需要程序了解姓名列表和年龄列表的根/头。

我不知道如何将其发送到我的函数。

我已经有了要添加到每个列表中的姓名和年龄,然后我必须以某种方式将其发送到每个列表的头部。因为我想返回两个头,所以我不能使用返回函数。如何让我的 main 知道头/根列表的更改?

希望问题很清楚,感谢您的回答!

I am trying to create a linked list, with two seperate lists in one structure. That is, it holds a 'name' and a 'age' value. Then the program can either output the list sorted by name, or sorted by age. So i need two linked lists, essentially.

However, i need the program to be aware of the root/head of both the name list and the age list.

I'm not sure how to send this to my function.

I will already have the name and the age I want to add to each list, and then somehow I have to send it the head of each list. Because there is two heads i want to return, i cant use the return function. How can I make my main aware of the changes to the head/root lists?

Hopefully the question is clea, and thanks for the answers!

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

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

发布评论

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

评论(4

铜锣湾横着走 2024-09-02 19:34:47

您可以将两个头包裹在一个结构中。

You may wrap both heads in a single structure.

耀眼的星火 2024-09-02 19:34:47

您可以将单个列表项链接到两个列表并独立对它们进行排序:

/* List item is linked into two lists */
struct list
{
    struct list* name_next; /* next in name list */
    struct list* age_next;  /* next in age list */

    char*    name;
    unsigned age;
};

/* Holds both list heads */
struct book
{
    struct list* name_sorted;
    struct list* age_sorted;
};

You can link single list item into two lists and sort them independently:

/* List item is linked into two lists */
struct list
{
    struct list* name_next; /* next in name list */
    struct list* age_next;  /* next in age list */

    char*    name;
    unsigned age;
};

/* Holds both list heads */
struct book
{
    struct list* name_sorted;
    struct list* age_sorted;
};
哆啦不做梦 2024-09-02 19:34:47

您可以简单地将一个指向两个头的指针作为参数传递,然后更改该指针,没有任何问题。

 void list_add(char *name, int age, list_type *list, list_type **name_head, list_type **age_head)
 {
     /* Add the name and age and calculate the two heads. */
     (*name_head) = calculated_head;
     (*age_head) = calculated_head;
 }

You can simply pass a pointer to the pointer to the two heads as a argument and then change the pointer without any problem.

 void list_add(char *name, int age, list_type *list, list_type **name_head, list_type **age_head)
 {
     /* Add the name and age and calculate the two heads. */
     (*name_head) = calculated_head;
     (*age_head) = calculated_head;
 }
甜嗑 2024-09-02 19:34:47
  1. 您可以按照《Kingdom of Fish》建议使用指针。我只是想补充一点,您的文档应该表明这些值是“输出”,即使它们是参数。

  2. 您可以使用元组数据类型。这是一个简单的:

    typedef struct Tuple{void*a,*b;}*Tuple;
    元组 tuple(void*a,void*b){
      元组 z=malloc(sizeof(struct Tuple));
      z->a=a,z->b=b;
      返回z;
    }
    

    但请注意,跟踪动态分配内存可能很困难,除非您使用 垃圾收集器,你需要非常小心。

  3. 您可以使用不太通用的数据结构。例如,您可以通过 person-object 将索引串联起来:

    struct Person;typedef struct Person*Person;
    struct Person{Person*prev_by_name,*next_by_name,*prev_by_age,*next_by_age; ...};
    

    添加额外的索引(链接列表)变得微不足道,如果您仔细考虑,您可以让预处理器为您完成大部分工作。

    此方法还使您能够询问“戴夫之后谁是下一个最年轻的人”

    最后,通过以这种方式对索引进行线程化,对一个人的索引的所有更新都将保存在一起,这将更容易避免同步错误。

  4. 您可以使用单独的函数,并且只需“记住”调用它们即可。如果这是用于家庭作业,并且您尚未解决如何返回多个值,那么您可能想要选择这个“解决方案”,因为它可能是您的老师正在寻找的。

  1. You can use pointers as Kingdom of Fish suggested. I just wanted to add that your documentation should indicate that these values are "outputs" even though they are parameters.

  2. You can use a tuple data-type. Here is a simple one:

    typedef struct Tuple{void*a,*b;}*Tuple;
    Tuple tuple(void*a,void*b){
      Tuple z=malloc(sizeof(struct Tuple));
      z->a=a,z->b=b;
      return z;
    }
    

    Beware though, that tracking dynamically allocating memory can be difficult, and unless you are using a garbage collector, you need to be very careful.

  3. You can use a less-general data structure. For example, you could thread the indexes through the person-object:

    struct Person;typedef struct Person*Person;
    struct Person{Person*prev_by_name,*next_by_name,*prev_by_age,*next_by_age; ...};
    

    Adding additional indexes (linked lists) becomes trivial, and if you give it some thought, you can make the preprocessor do most of the work for you.

    This method also gives you the ability to ask "who is the next-youngest-person after Dave"

    Finally, by threading the indexes in this way, all updates to a person's indexes will be kept together, which will make it easier to avoid synchronization bugs.

  4. You can use separate functions and simply "remember" to call them both. If this is for homework, and you haven't yet addressed how to return multiple values, then you might want to choose this "solution" simply because it's probably what your teacher is looking for.

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