无法理解链表(c)

时间:2019-01-23 20:51:42

标签: c linked-list

我不明白链表是如何工作的。

让我说我有一些代码

typedef struct trip {
    int id, year, something;
    char name[100], destination[100];
    struct trip *next;                       //X1

}TRIP;                                       //X2
    FILE *fr;
    TRIP *p_a, *p_p;                         //X3
    fr =fopen("list.txt","r");
    int j=0;
    p_p = (TRIP *) malloc(sizeof(TRIP));
    p_a = p_p;
    while ((fscanf(fr,"%d",&p_a->id)), p_a->id>0 )
    {
            fscanf(fr,"%s",&p_a->name);
            fscanf(fr,"%s",&p_a->destination);
            fscanf(fr,"%d",&p_a->year);
            fscanf(fr,"%d",&p_a->something);
            p_a->next = (TRIP *) malloc(sizeof(TRIP));
            p_a = p_a->next;

    }
    p_a->next = NULL;                        //X4
    fclose(fr);
    return p_p;
}

我不确定某些行在做什么。这些行用x1 x2 x3 x4注释。 请有人可以向我解释吗?

3 个答案:

答案 0 :(得分:1)

x1

  

struct行程* next; // X1

在“行程”数据结构中,它包含一个指向另一个行程数据结构的指针。

您可以使用它来指向链接列表的其他结构,以“链接”元素。

x2

  

} TRIP; // X2

这将(我忘记了合适的词)对该结构进行别名。 TRIP =行程。

这就是说

trip a; // OR
TRIP a;

都有效

x3

  

TRIP * p_a,* p_p; // X3

这是声明两个变量,类型为 TRIP * (或 trip * ,它们是等效的)

此类型是指向旅行数据结构的指针。

x4

  

p_a-> next = NULL; // X4

这是指针中的成员选择。也就是说,它现在将p_a指向的跳闸结构中的“ next”元素的值设置为NULL。

如果有帮助,它将等同于

(*p_a).next = NULL

答案 1 :(得分:1)

typedef struct trip {                       
    int id, year, something;
    char name[100], destination[100];
    struct trip *next;                   //Define a pointer to a trip struct
                                         //called next. This will hold a
                                         //pointer to the next element in
                                         //the list.

}TRIP;                                   //The name of the typedef statement
                                         //After this you can define a trip 
                                         //without writing 'struct trip' but 
                                         //just 'TRIP'

FILE *fr;
TRIP *p_a, *p_p;                         //Define two pointers to trip structs
fr =fopen("list.txt","r");
int j=0;
p_p = (TRIP *) malloc(sizeof(TRIP));
p_a = p_p;
while ((fscanf(fr,"%d",&p_a->id)), p_a->id>0 )
{
        fscanf(fr,"%s",&p_a->name);
        fscanf(fr,"%s",&p_a->destination);
        fscanf(fr,"%d",&p_a->year);
        fscanf(fr,"%d",&p_a->something);
        p_a->next = (TRIP *) malloc(sizeof(TRIP));
        p_a = p_a->next;

}
p_a->next = NULL;                        //After the last line has been added
                                         //to the list, end the list by making
                                         //the next pointer NULL
fclose(fr);
return p_p;
}

答案 2 :(得分:1)

x1:next是指向struct trip另一个实例的指针。图片可能会有所帮助:

+-------+------+       +-------+------+       +-------+------+
| stuff | next | ----> | stuff | next | ----> | stuff | next | ----|||
+-------+------+       +-------+------+       +-------+------+

其中stuff是您存储在该节点中的所有其他数据(idyearsomethingname,{{1} }。 destination成员存储另一个节点的地址。

C允许您在next类型的定义内声明指向struct类型的指针:

struct

您无需知道struct foo { ... struct foo *fooptr; ... }; 类型的大小即可创建指向它的指针,并且指向所有struct类型的指针都具有相同的大小和表示形式。也就是说,即使structstruct foo可能具有根本不同的定义,指向struct bar的指针也具有指向struct foo的指针的大小。

x2:struct bar工具允许您为类型创建别名-typedef是类型名称TRIP的别名。如果您想简化复杂类型或抽象出实现细节({{1}中的struct trip typedef名称就是后者的示例),这将非常有用。

x3:此行将FILEstdio.h声明为指向p_a的指针。