阅读量:0
1.ListNode.h头文件
#pragma once #include<stdio.h> #include<stdlib.h> #include<assert.h> #include<string.h> typedef int LTDataType; typedef struct ListNode { struct ListNode* next; struct ListNode* prev; LTDataType data; }LN; //初始化 LN* ListInit(); //尾插 void ListPushBank(LN* plist, LTDataType x); //头插 void ListPushFront(LN* plist, LTDataType x); //打印 void List_print(LN* plist); //尾删 void ListPopBank(LN* plist); //头删 void ListPopFront(LN* plist); //查找 LN* ListFind(LN* plist,LTDataType x); //pos之前插入x void ListInsert(LN* pos, LTDataType x); //删除pos位置 void ListErase(LN* pos); //销毁 void ListDestroyed(LN* plist);
2.ListNode.c源文件函数的实现代码
#include"ListNode.h" //创建节点 LN* BuyListNode(LTDataType x) { LN* newnode = (LN*)malloc(sizeof(LN)); newnode->data = x; newnode->next = newnode->prev = newnode; return newnode; } //初始化 LN* ListInit() { LN* newnode = BuyListNode(0);//哨兵位 } //尾插 void ListPushBank(LN* plist,LTDataType x) { assert(plist); LN* ret = plist->prev; LN* newnode = BuyListNode(x); ret->next = newnode; newnode->next = plist; newnode->prev = ret; plist->prev = newnode; //快捷 //ListInsert(plist->prev->next, x); } //打印 void List_print(LN* plist) { LN* ret = plist->next;//哨兵不需要打印 while (ret != plist) { printf("%d->", ret->data); ret = ret->next; } printf("NULL\n"); } //头插 void ListPushFront(LN* plist, LTDataType x) { assert(plist); LN* newnode = BuyListNode(x); LN* first = plist->next; plist->next = newnode; newnode->next = first; first->prev = newnode; newnode->prev = plist; //ListInsert(plist->next, x); } //尾删 void ListPopBank(LN* plist) { assert(plist); assert(plist->next != plist); LN* pcur = plist->prev; pcur->prev->next = plist; plist->prev = pcur->prev; free(pcur); pcur = NULL; //ListErase(plist->prev); } //头删 void ListPopFront(LN* plist) { assert(plist); assert(plist->next != plist); LN* first = plist->next; plist->next = first->next; first->next->prev = plist; free(first); //快捷方式 //ListErase(plist->next); } //查找 LN* ListFind(LN* plist, LTDataType x) { assert(plist); LN* ret = plist->next; while (ret != plist) { if (x == ret->data) { return ret; } ret = ret->next; } return NULL; } //pos之前插入内容 void ListInsert(LN* pos, LTDataType x) { assert(pos); LN* newnode = BuyListNode(x); LN* pcur = pos->prev; pcur->next = newnode; newnode->next = pos; newnode->prev = pcur; pos->prev = newnode; } //删除pos位置 void ListErase(LN* pos) { assert(pos); LN* prev = pos->prev; LN* next = pos->next; prev->next = next; next->prev = prev; free(pos); } //销毁 void ListDestroyed(LN* plist) { assert(plist); LN* cur = plist->next; while (cur != plist) { LN* ret = cur->next; free(cur); cur = ret; } free(plist); plist = NULL; printf("销毁成功\n"); }
3.test.c测试代码
#define _CRT_SECURE_NO_WARNINGS 1 #include"ListNode.h" void menu() { printf("*******************\n"); printf("1.尾插 2.头插\n"); printf("3.尾删 4.头删\n"); printf("5.打印 6.查找\n"); printf("7.pos插入 8.删除pos\n"); printf("9.销毁 10.退出\n"); printf("*******************\n"); } int main() { LN* plist = ListInit(); int input = 0, x = 0, y = 0; do { menu(); printf("请输入你需要操作的内容\n"); scanf("%d", &input); switch (input) { case 1: printf("请输入你要尾插的内容,输入-1结束\n"); do { scanf("%d", &x); if (x != -1) { ListPushBank(plist, x); } } while (x != -1); break; case 2: printf("请输入你要头插的内容,输入-1结束\n"); do { scanf("%d", &x); if (x != -1) { ListPushFront(plist, x); } } while (x != -1); break; case 3: ListPopBank(plist); printf("尾删成功\n"); break; case 4: ListPopFront(plist); printf("头删成功\n"); break; case 5: List_print(plist); break; case 6: printf("请输入你要查找的数值\n"); scanf("%d", &x); LN* ret = ListFind(plist, x); if (ret != NULL) { printf("找到了\n"); } else { printf("找不到你要的数值\n"); } break; case 7: printf("请输入pos的值\n"); scanf("%d", &x); LN* ret2 = ListFind(plist, x); if (ret2 == NULL) { printf("你要查找的pos节点不存在\n"); exit(1); } else { printf("请输入你要插入的数值\n"); scanf("%d", &y); ListInsert(ret2, y); printf("插入成功\n"); } break; case 8: printf("请输入你要删除的pos点\n"); scanf("%d", &x); LN* ret3 = ListFind(plist, x); if (ret3 == NULL) { printf("要删除的pos点不存在\n"); exit(1); } else { ListErase(ret3); printf("删除成功\n"); } break; case 9: ListDestroyed(plist); break; case 10: input = -1; printf("退出中..."); break; default: printf("请选择1-10的方法\n"); break; } } while (input != -1); return 0; }