c - 在链表的 "struct name *next"内添加 "struct name"的目的是什么?我的意思是我实际上无法处理到底发生了什么

标签 c linked-list structure

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

typedef struct employ
{
    int reg;
    int sal;
    char *name;
    struct employ *next; // I want to know the purpose of this line
}EMP;

int main(void)
{
    EMP *first,*emp1,*emp2,*emp3,*ans;
    first = (EMP *)malloc(sizeof(EMP));
    first->next = NULL;
    /////first///////
    emp1 = (EMP *)malloc(sizeof(EMP));
    if(emp1 == NULL)
    {
        perror("malloc error");
        exit(1);
    }

    emp1->reg = 100;
    emp1->sal = 30000;
    emp1->name = "james";
    emp1->next = first;
    first = emp1;

    /* I havent completed the program bcoz its not necessary here */

最佳答案

在链表中,每个节点都包含一个指向下一个节点的指针。

struct employ *next;

用于实现该效果

+---------+        +---------+
|  Node1  |------->|  Node2  |----> ...  
+---------+        +---------+

如果你在node1,next是一个指向node2的指针,这样你就可以从当前元素访问下一个元素

关于c - 在链表的 "struct name *next"内添加 "struct name"的目的是什么?我的意思是我实际上无法处理到底发生了什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24885063/

相关文章:

c# - 如何在应用程序关闭时处理释放非托管结构?

c++ - #if/#endif 预处理指令

c++ - 为什么我不能在 switch/case 中使用 unsigned short?

c - 使用顶点缓冲区对象为 OpenGL 中的基元赋予颜色

c++ - 双向链表中的排序插入

c - 结构中数组的值仍在变化

c - 如何制作结构数组

c - 我在 C 中创建了一个 push 和 pop 函数,但不知道如何按顺序打印它们

java - 在 HashMap 或 LinkedList 中将嵌套类设为静态的原因是什么?

c - 这段代码用什么来确定数组的大小?