1.实现的接口和功能
//打印链表 void SLTPrint(SLTNode** phead); //头插 void PushFont(SLTNode** phead, SLTDataType x); //尾插 void PushBack(SLTNode** phead, SLTDataType x); //头删 void PopFont(SLTNode** phead); //尾删 void PopBack(SLTNode** phead); //删除链表 void Destory(SLTNode** phead); //创建一个结点 SLTNode* Create(SLTDataType x); //查找x的结点 SLTNode* Find(SLTNode** phead,SLTDataType x); //查找指定结点并删除 void FindDele(SLTNode** phead, SLTDataType x); //删除指定结点的下一个结点 void Delenext(SLTNode** phead, SLTDataType* pos); //删除指定结点的上一个结点 void DeleAfter(SLTNode** phead, SLTDataType* pos);
2.代码块
声明代码:
#include<stdio.h> #include<stdlib.h> #include<assert.h> typedef int SLTDataType; typedef struct SListNode { SLTDataType data; struct SListNode* next; }SLTNode; //打印链表 void SLTPrint(SLTNode** phead); //头插 void PushFont(SLTNode** phead, SLTDataType x); //尾插 void PushBack(SLTNode** phead, SLTDataType x); //头删 void PopFont(SLTNode** phead); //尾删 void PopBack(SLTNode** phead); //删除链表 void Destory(SLTNode** phead); //创建一个结点 SLTNode* Create(SLTDataType x); //查找x的结点 SLTNode* Find(SLTNode** phead,SLTDataType x); //查找指定结点并删除 void FindDele(SLTNode** phead, SLTDataType x); //删除指定结点的下一个结点 void Delenext(SLTNode** phead, SLTDataType* pos); //删除指定结点的上一个结点 void DeleAfter(SLTNode** phead, SLTDataType* pos);
实现代码:
#include"SList.h" SLTNode* Create(SLTDataType x) { SLTNode* list = (SLTNode*)malloc(sizeof(SLTNode)); if (list == NULL) { perror("malloc fail\n"); } list->next = NULL; list->data = x; return list; } void SLTPrint(SLTNode** phead) { assert(*phead); SLTNode* list = *phead; while (list) { printf("%d ", list->data); list = list->next; } printf("\n"); } void PushFont(SLTNode** phead, SLTDataType x) { SLTNode* p = Create(x); if (*phead == NULL) { *phead = p; } else { p->next = *phead; *phead = p; } } void PushBack(SLTNode** phead, SLTDataType x) { SLTNode* p = Create(x); if (*phead == NULL) { *phead = p; } SLTNode* prev = NULL; SLTNode* ps = *phead; while (ps) { prev = ps; ps = ps->next; } prev->next = p; } void PopFont(SLTNode** phead) { assert(*phead); SLTNode* p = (*phead)->next; free(*phead); *phead = p; } void PopBack(SLTNode** phead) { assert(*phead); if ((*phead)->next == NULL) { free(*phead); *phead = NULL; } SLTNode* p = *phead; SLTNode* prev = NULL; while (p->next) { prev = p; p = p->next; } free(p); prev->next = NULL; } SLTNode* Find(SLTNode** phead, SLTDataType x) { assert(*phead); SLTNode* p = *phead; while (p->data!=x) { p = p->next; } if (p->data == x) return p; else return NULL; } void FindDele(SLTNode** phead, SLTDataType x) { assert(*phead); SLTNode* p = *phead; SLTNode* prev = NULL; while (p!=NULL&&p->data!=x) { prev = p; p = p->next; } if (p->data == x) { prev->next = p->next; free(p); } } void Destory(SLTNode** phead) { assert(*phead); SLTNode* later = NULL; SLTNode *p= *phead; while (p) { later = p->next; free(p); p = later; } } void Delenext(SLTNode** phead, SLTDataType* pos) { assert(phead); SLTNode* p = *phead; while (p != pos) { p = p->next; } if (p->next == NULL) { return; } SLTNode* ps = p->next; p->next = p->next->next; free(ps); } void DeleAfter(SLTNode** phead, SLTDataType* pos) { assert(phead); assert(&phead); if ((*phead)->next == NULL) { return; } SLTNode* p = *phead; SLTNode* prev = NULL; while (p->next!= pos) { prev = p; p = p->next; } if (prev == NULL) { *phead = p->next; return; } prev->next = p->next; free(p); }
测试用例代码:
#include"SList.h" int main() { SLTNode* list = NULL; PushFont(&list,1); PushFont(&list, 2); PushFont(&list, 3); PushBack(&list,4); PushBack(&list, 5); PushBack(&list, 6); SLTPrint(&list); PopFont(&list); PopBack(&list); SLTPrint(&list); FindDele(&list, 5); SLTNode*p= Find(&list,4); SLTNode* ps = Find(&list, 1); Delenext(&list,p); SLTPrint(&list); DeleAfter(&list, ps); SLTPrint(&list); Destory(&list); return 0; }
结尾:今天的分享到此结束,喜欢的朋友如果感觉有帮助可以点赞三连支持,咱们共同进步!