## 新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > 如何定义链表结点的数据结构？

# 如何定义链表结点的数据结构？

1.1.1 数据与p_next分离

1 typedef struct _slist_node{

2 struct _slist_node *p_next; // 指向下一个结点的指针

3 }slist_node_t;

1 typedef struct _slist_int{

2 slist_node_t node; // 包含链表结点

3 int data; // int类型数据

4 }slist_int_t;

1 #include

2 typedef struct _slist_int{

3 slist_node_t node;

4 int data;

5 }slist_int_t;

6

7 int main (void)

8 {

9 slist_node_t head = {NULL};

10 slist_int_t node1, node2, node3;

11 slist_node_t *p_tmp;

12

13 node1.data = 1;

15 node2.data = 2;

17 node3.data = 3;

19 p_tmp = head.p_next;

20 while (p_tmp != NULL){

21 printf(%d , ((slist_int_t *)p_tmp)->data);

22 p_tmp = p_tmp->p_next;

23 }

24 return 0;

25 }

int slist_init (slist_node_t *p_head);

2 {

3 if (p_head == NULL){

4 return -1;

5 }

6 p_head -> p_next = NULL;

7 return 0;

8 }

2 {

3 slist_node_t *p_tmp;

4

5 if ((p_head == NULL) || (p_node == NULL)){

6 return -1;

7 }

8 p_tmp = p_head;

9 while (p_tmp -> p_next != NULL){

10 p_tmp = p_tmp -> p_next;

11 }

12 p_tmp -> p_next = p_node;

13 p_node -> p_next = NULL;

14 return 0;

15 }

1 slist_node_t *p_tmp = head.p_next;

2 while (p_tmp != NULL){

3 printf(%d , ((slist_int_t *)p_tmp)->data);

4 p_tmp = p_tmp->p_next;

5 }

slist_node_t *slist_next_get (slist_head_t *p_head, slist_node_t *p_pos);// 获取某一结点的后一结点

1 slist_node_t *slist_next_get (slist_head_t *p_head, slist_node_t *p_pos)