了解结构/链接列表

时间:2017-06-09 00:15:57

标签: c list structure

我遇到过这种结构,我很难“解剖”它们,因为我用不同的方式写它们,因为我很笨。

这是结构:

typedef struct no_disciplina * ListDisciplinas;

typedef struct no_disciplina 
{     
     char * nome;    
     struct no_apont_aluno * alunos;     
     ListDisciplinas next; 
} NoDisciplina;

typedef struct no_aluno * ListAlunos; 

typedef struct no_aluno 
{     
     char * nome;
     int numero;
     struct no_apont_disciplina * disciplinas;
     ListAlunos next;
 } NoAluno; 

 typedef struct no_apont_disciplina * ListApontDisciplinas;

 typedef struct no_apont_disciplina
 {    ListDisciplinas disciplina;
      ListApontDisciplinas next;
 } NoApontDisciplina; 

typedef struct no_apont_aluno * ListApontAlunos;

typedef struct no_apont_aluno
{     
     ListAlunos aluno;
     ListApontAlunos next;
} NoApontAluno; 

据说这些链接列表没有标题(一个节点只由指向列表第一个“真实”节点的指针组成)所以我该如何定义它?

提前致谢:D!

1 个答案:

答案 0 :(得分:0)

所以你要像这样定义你的链表,

typedef struct node {
    char val;
    struct node * next;
} node_t;

然后要使用带头的链表,你会做这样的事情,

node_t * head = NULL;
head = malloc(sizeof(node_t));
if (head == NULL) {
    return 1;
}

head->val = 'a';
head->next = NULL;

所以现在'head'将成为您的标题,您定义的任何后续节点将附加到'head',最后一个元素是尾部。

相关问题