1.单向链表(节点的创建,添加,删除,排序)
#include#include int m = 0 ; typedef struct save { int x ; save *next ; } s ; s* create(s *hd, int n) ; void output(s *hd, int n) ; void release(s *hd) ; void menu(s *hd, int n) ; s* add(s *hd) ; s* dele(s *hd, int k, int n) ; s* sort(s *hd) ; main(void) { s *hd = NULL ; int n ; printf("请输入你想创建的节点数量:n") ; scanf("%d", &n) ; if (0 == n) { printf("节点数量为零,程序结束") ; exit(0) ; } printf("请输入%d个数据:", n) ; hd = create(hd, n) ; output(hd, n) ; menu(hd, n) ; } s* create(s *hd, int n) { s *mid = NULL, *tail = NULL ; hd = (s *)malloc(sizeof(s)) ; scanf("%d", &hd->x) ; tail = hd ; for (int i = 1 ; i < n ; i++) { mid = (s *)malloc(sizeof(s)) ; scanf("%d", &mid->x) ; tail->next = mid ; tail = mid ; } tail->next = NULL ; return hd ; } void output(s *hd, int n) { s *p = hd ; for (int i = 0 ; i < n ; i++) { printf("第%d个节点:%4dn", i+1, p->x) ; p = p->next ; } } void release(s *hd) { s *p = hd, *temp = NULL ; while (p) { temp = p->next ; free(p) ; p = temp ; } } void menu(s *hd, int n) { int x, k ; printf("请选择你想进行的操作:n1.将节点数据升序排列n2.添加节点n3.删除节点n4.退出程序n") ; scanf("%d", &x) ; if (1 == x) { hd = sort(hd) ; output(hd, n) ; menu(hd, n) ; } else if (2 == x) { hd = add(hd) ; output(hd, n+1) ; menu(hd, n+1) ; } else if (3 == x) { m = 0 ; printf("请输入删除节点数据:n") ; scanf("%d", &k) ; for (int i = 0 ; i < n ; i++) hd = dele(hd, k, n) ; output(hd, m) ; menu(hd, m) ; } else if (4 == x) release(hd) ; else { printf("输入无效,") ; menu(hd, n) ; } } s* add(s *hd) { s *p = NULL, *pr = hd, *temp = NULL ; p = (s *)malloc(sizeof(s)) ; printf("请输入添加节点数据:n") ; scanf("%d", &p->x) ; p->next = NULL ; if (p->x <= hd->x) { p->next = hd ; return p ; } while (p->x > pr->x && pr->next != NULL) { temp = pr ; pr = pr->next ; } if (p->x <= pr->x) { p->next = pr ; temp->next = p ; } else pr->next = p ; return hd ; } s* dele(s *hd, int k, int n) { s *p = hd, *pr = NULL ; if (k == hd->x) { p = hd->next ; return p ; } while (k != p->x && p->next != NULL) { pr = p ; p = p->next ; } if (k == p->x) { pr->next = p->next ; free(p) ; } else m++ ; if (n == m) printf("未找到相关节点n") ; return hd ; } s* sort(s *hd) { s *p = NULL, *pr = hd, *temp = NULL ; int k, t ; while (pr->next != NULL) { k = pr->x ; p = pr ; while (p != NULL) { if (k > p->x) { k = p->x ; temp = p ; } p = p->next ; } if (k != pr->x) t = pr->x, pr->x = temp->x, temp->x = t ; pr = pr->next ; } return hd ; }
2.双向链表的创建
#include#include typedef struct Node { Node *previou ; int data ; Node *next ; } N ; void output1(N *first, int n) { N *p = first ; for (int i = 0 ; i < n ; i++) { printf("第%d个节点:%4dn", i+1, p->data) ; p = p->next ; } } void output2(N *last, int n) { N *p = last ; for (int i = 0 ; i < n ; i++) { printf("第%d个节点:%4dn", n-i, p->data) ; p = p->previou ; } } main(void) { N *first = NULL, *last = NULL, *mid = NULL ; int n ; printf("请输入你想要创建的节点数量:n") ; scanf("%d", &n) ; if (0 == n) { printf("节点数量为零,退出程序") ; exit(0) ; } printf("请输入%d个节点数量:n", n) ; first = (N *)malloc(sizeof(N)) ; scanf("%d", &first->data) ; first->previou = NULL ; first->next = NULL ; last = first ; for (int i = 1 ; i < n ; i++) { mid = (N *)malloc(sizeof(N)) ; scanf("%d", &mid->data) ; last->next = mid ; mid->previou = last ; last = mid ; } last->next = NULL ; printf("正向输出:n") ; output1(first, n) ; printf("反向输出:n") ; output2(last, n) ; }



