Appearance
下面定义了一个单向链表的数据结构,并给出了头节点和普通节点的初始化函数:
c
typedef int datatype;
typedef struct Node {
union { int len; datatype data; };
struct Node *next;
} *LinkedList;
linkedlist create_head() {
linkedlist p = (linkedlist)malloc(sizeof(struct node));
if (p == NULL)
return NULL;
p->next = NULL;
p->len = 0;
return p;
}
linkedlist create_node() {
linkedlist p = (linkedlist)malloc(sizeof(struct node));
if (p == NULL)
return NULL;
p->next = NULL;
p->data = 0;
return p;
}之所以将 int 别名为 datatype,是为了强化链表具有通用性的概念,如果到时候需要在其他数据类型上使用,也方便移植。