头文件DList.h
#include
#include #include typedef int ElemType; typedef struct Node { ElemType data; struct Node*next; struct Node*prio; }Node, *PNode; typedef struct List { PNode first; PNode last; int size; }List; void InitList(List *list); Node *buy_node(Node *list);//创建节点 void push_back(List *list, ElemType elem);//尾插 void push_front(List *list, ElemType elem);//头插 void show_list(List*list);//显示 void pop_back(List *list);//尾删 void pop_front(List *list);//头删 void insert_val(List *list, ElemType elem);//按值插入 Node* find_val(List *list, ElemType elem);//查找 int length(List *list);//长度 void delete_val(List *list, ElemType elem);//按值删除 void sort(List *list);//排序 void reserve(List *list);//逆序 void clear(List *list);//清除链表 void destroy(List *list);
源文件DList.c
#include"DList.h" #include
#include void InitList(List *list) { Node *s = (Node *)malloc(sizeof(Node)); assert(s != NULL); list->first = list->last = s; list->last->next = NULL; list->first->prio = NULL; list->size = 0; } Node *buy_node(ElemType x) { Node *s = (Node *)malloc(sizeof(Node)); assert(s != NULL); s->data = x; s->next = s->prio = NULL; return s; } void push_back(List *list, ElemType elem) { Node *p = buy_node(elem); p->prio = list->last->next; list->last->next = p; list->last = p; list->size++; } void push_front(List *list, ElemType elem) { Node *p = buy_node(elem); if (list->first== list->last) { push_back(list, elem); } else { p->next = list->first->next; p->next->prio = p; p->prio = list->first; list->first->next = p; } list->size++; } void show_list(List *list) { Node *p = list->first->next; while (p != NULL) { printf("%d-->", p->data); p = p->next; } printf("Nul.\n"); } void pop_back(List *list) { if (list->size == 0 ) return; Node *p = list->first; while (p->next!= list->last) p = p->next; free(list->last); list->last = p; list->last->next = NULL; list->size--; } void pop_front(List *list) { Node *p = list->first->next;//指向第一个数据结点 if (list->size == 0) return; if (list->first->next==list->last) { list->last = list->first; list->last->next = NULL; list->first->prio = NULL; } else { p->next->prio = list->first; list->first->next = p->next; } free(p); list->size--; } void insert_val(List *list, ElemType elem) {//必须单链表有序 Node *p = list->first; while (p->next != NULL && p->next->data < elem) { p = p->next; } if (p->next == NULL) push_back(list, elem); else { Node*s = buy_node(elem); s->next = p->next; s->next->prio = s; s->prio = p; p->next = s; } list->size++; } Node* find_val(List *list, ElemType elem) { Node *p = list->first->next; while (p != NULL && p->next != elem) p = p->next; return p; } int length(List *list) { return list->size; } void delete_val(List *list, ElemType elem) { if (list->size == 0) return; Node *p = find_val(list, elem); if (p == NULL) printf("%d不在双向链表中!\n", elem); if (p == list->last) {//要删除的结点是最后一个 pop_back(list); } else//要删除中间结点 { p->next->prio = p->prio; p->prio->next = p->next; } free(p); list->size--; } void sort(List *list) { if (list->size == 0 || list->size == 1) return; Node *s = list->first->next; Node*q = s->next; list->last = s; list->last->next = NULL; while (q != NULL) { s = q; q = q->next; Node *p = list->first; while (p->next != NULL && p->next->data < s->data) p = p->next; if (p->next == NULL) {//尾插 push_back(list, s->data); } else { s->next = p->next; s->next->prio = s; s->prio = p; p->next = s; } } } void reserve(List *list) { if (list->size == 0 || list->size == 1) return; Node *p = list->first->next; Node*q = p->next; list->last = p; list->last->next = NULL; while (q != NULL) { p = q; q = q->next; p->next = list->first->next; p->next->prio = p; p->prio = list->first; list->first->next = p; } } void clear(List *list) {//清除掉头结点之后的所有节点 if (list->size == 0) return; Node *p = list->first->next; while (p != NULL) { if (p == list->last) { list->last = list->first; list->last->next = p->next; } else {//使用头部删除结点方式 //pop_back(list); p->next->prio = list->first; list->first->next = NULL; } free(p); p = list->first->next; } list->size = 0; } void destroy(List *list) { clear(list); free(list->first); list->first = list->last = NULL; }
测试文件Main.c
#include"DList.h" #include
#include #include int main() { List mylist; InitList(&mylist); ElemType elem; Node *p = NULL; InitList(&mylist); int select = 1; if (select == 0) return 0; while (select) { printf("*******************************\n"); printf("*[1] push_back***[2] push_front\n"); printf("*[3] show_list***[4] pop_back\n"); printf("*[5] pop_front***[6] insert_val\n"); printf("*[7] find_val ***[8] length\n"); printf("*[9] delete_val**[10] sort\n"); printf("*[11] resever****[12] clear\n"); printf("*[13*] destroy****[0] quit_syste\n"); printf("*******************************\n"); printf("请选择您要的 *** 作:\n"); scanf_s("%d", &select); if (select == 0) break; switch (select) { case 1: printf("请输入你要插入的数据:\n"); while (scanf_s("%d", &elem), elem != -1) { push_back(&mylist, elem); } break; case 2: printf("请输入要插入的数据(-1表示结束)\n"); while (scanf_s("%d", &elem), elem != -1) { push_front(&mylist, elem); } break; case 3: show_list(&mylist); break; case 4: pop_back(&mylist); break; case 5: pop_front(&mylist); break; case 6: printf("请输入要插入的数据:\n"); scanf_s("%d", &elem); insert_val(&mylist, elem); break; case 7: printf("请输入要查找的数据:\n"); scanf_s("%d", &elem); p = find_val(&mylist, elem);//返回该节点的地址 if (p == NULL) printf("要查找的数据在链表中不存在\n"); else printf("%d在双链表中存在!\n", elem); break; case 8: printf("单链表的长度为%d:\n", length(&mylist)); break; case 9: printf("请输入要删除的值\n"); scanf_s("%d", &elem); delete_val(&mylist, elem); break; case 10: sort(&mylist); break; case 11: reserve(&mylist); break; case 12: clear(&mylist); break; default: printf("输入的命令错误,请重新输入!\n"); break; } } destroy(&mylist); }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)