双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。一般我们都构造双向循环链表。
代码如下:
double-list.c
double-list.h- 1 /**
- 2 * C data structure double linked list example.
- 3 *
- 4 * License - MIT.
- 5 */
- 6
- 7 #ifndef __DOUBLE_LIST_H__
- 8 #define __DOUBLE_LIST_H__
- 9
- 10
- 11 #include <stdio.h>
- 12 #include <stdlib.h>
- 13 #include <stdbool.h>
- 14
- 15 typedef struct _DOUBLELIST{
- 16 int data;
- 17 struct _DOUBLELIST *prev, *next;
- 18 } DOUBLELIST, *LPDOUBLELIST;
- 19
- 20
- 21 bool doulist_empty (LPDOUBLELIST lpHead);
- 22 int doulist_show (LPDOUBLELIST lpHead);
- 23 int doulist_insert (LPDOUBLELIST lpHead, LPDOUBLELIST lpNode);
- 24 int doulist_insert_tail (LPDOUBLELIST lpHead, LPDOUBLELIST lpNode);
- 25 int doulist_del (LPDOUBLELIST lpNode);
- 26 int doulist_init (LPDOUBLELIST *lpHead);
- 27 int doulist_clear (LPDOUBLELIST lpHead);
- 28
- 29
- 30 #endif /* __DOUBLE_LIST_H__ */
复制代码
main.c- 1 /**
- 2 * C data structure double linked list example.
- 3 *
- 4 * License - MIT.
- 5 */
- 6
- 7 #include <stdio.h>
- 8
- 9 #include "double-list.h"
- 10
- 11
- 12 #define MAX_TEST_NUM 10
- 13
- 14
- 15 /**
- 16 * test_sort - Sort the data.
- 17 */
- 18 int test_sort(LPDOUBLELIST lpHead)
- 19 {
- 20 LPDOUBLELIST pos, tmp;
- 21
- 22 pos = lpHead->prev;
- 23
- 24 while (pos != lpHead)
- 25 {
- 26 if (1 == (pos->data % 2)) {
- 27 pos = pos->prev;
- 28 }
- 29 else {
- 30 tmp = pos;
- 31 pos = pos->prev;
- 32
- 33 doulist_del(tmp);
- 34 doulist_insert_tail(lpHead, tmp);
- 35 }
- 36 }
- 37
- 38 return 0;
- 39 }
- 40
- 41
- 42 /**
- 43 * test_create - Create single list example.
- 44 */
- 45 int test_create(LPDOUBLELIST lpHead)
- 46 {
- 47 int i;
- 48 LPDOUBLELIST newNode;
- 49
- 50 for (i = 0; i < MAX_TEST_NUM; i++) {
- 51 newNode = (LPDOUBLELIST) malloc(sizeof(DOUBLELIST));
- 52 if (NULL == newNode) {
- 53 return -1;
- 54 }
- 55
- 56 newNode->data = i + 1;
- 57 doulist_insert_tail(lpHead, newNode);
- 58 }
- 59
- 60 return 0;
- 61 }
- 62
- 63
- 64 /**
- 65 * Main function.
- 66 */
- 67 int main(void)
- 68 {
- 69 LPDOUBLELIST lphead = NULL;
- 70
- 71 doulist_init(&lphead);
- 72
- 73 test_create(lphead);
- 74 doulist_show(lphead);
- 75
- 76 test_sort(lphead);
- 77 doulist_show(lphead);
- 78
- 79 doulist_clear(lphead);
- 80
- 81 return 0;
- 82 }
复制代码
Makefile- 1 # Makefile
- 2 CC = gcc
- 3 CFLAGS = -Wall -g -O0
- 4
- 5 SRC = main.c double-list.c
- 6
- 7 OBJ = doulist-test
- 8
- 9 $(OBJ) : $(SRC)
- 10 $(CC) $(CFLAGS) -o $@ $^
- 11
- 12 clean:
- 13 $(RM) $(OBJ) *.o *.*.sw?
复制代码
完整代码请参考Github: [Link] [https://github.com/Phoebus-Ma/C-Helper/tree/main/Class-1/List.C]
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |