1.头文件(dlist.h)
#pragma once typedef int ElemType; typedef struct DNode { ElemType data; struct DNode* next; struct DNode* prior; }DNode,*DList; //初始化plist void InitList(DList plist); //往plist中头部插入数字val bool Insert_head(DList plist, ElemType val); //往plist中的尾部插入数字val bool Insert_tail(DList plist, ElemType val); //在plist中查找val值,找到返回该节点地址,失败返回NULL DNode* Search(DList plist, ElemType val); //删除plist中的第一个val bool DeleteVal(DList plist, ElemType val); //判断plist是否为空链表(没有数据节点) bool IsEmpty(DList plist); //获取plist长度,数据节点的个数 int GetLength(DList plist); //获取plist链表的pos位置的值 bool GetElem(DList plist, int pos, int* rtval);//rtval:输出参数 //获取val的前驱 DNode* Prior(DList plist, ElemType val); //获取val的后继 DNode* Next(DList plist, ElemType val); //输出plist的所有数据 void Show(DList plist); //清空数据 void Clear(DList plist); //销毁 void Destroy(DList plist);
2.函数实现文件(dlist.cpp)
#include<stdio.h> #include<stdlib.h> #include<assert.h> #include"dlist.h" void InitList(DList plist) { assert(plist != NULL); if (plist == NULL) return; plist->next = NULL; plist->prior = NULL; } bool Insert_head(DList plist, ElemType val) { DNode* p = (DNode*)malloc(sizeof(DNode)); assert(p != NULL); if (p == NULL) return false; p->data = val; p->next = plist->next; plist->next = p; p->prior = plist; if (p->next != NULL) p->next->prior = p; return true; } bool Insert_tail(DList plist, ElemType val) { DNode* p = (DNode*)malloc(sizeof(DNode)); assert(p != NULL); if (p == NULL) { return false; } p->data = val; DNode* q; for (q = plist; q->next != NULL; q = q->next) ; p->next = q->next; q->next = p; p->prior = q; return true; } //在plist中查找val值,找到返回该节点地址,失败返回NULL DNode* Search(DList plist, ElemType val) { for (DNode* p = plist->next; p != NULL; p = p->next) { if (p->data == val) return p; } return NULL; } //删除plist中的第一个val bool DeleteVal(DList plist, ElemType val) { DNode* p = Search(plist, val); if (p == NULL) return NULL; p->prior->next = p->next; if (p->next != NULL) { p->next->prior = p->prior; } free(p); } //判断plist是否为空链表(没有数据节点) bool IsEmpty(DList plist) { return plist->next == NULL; } //获取plist长度,数据节点的个数 int GetLength(DList plist) { int count=0; for (DNode* p = plist->next; p != NULL; p = p->next) { count++; } return count; } //获取plist链表的pos位置的值 bool GetElem(DList plist, int pos, int* rtval)//rtval:输出参数 { if (pos < 0 || pos >= GetLength(plist)) { return false; } DNode* p = plist->next; for (int i = 0; i < pos; i++, p = p->next) { ; } *rtval = p->data; return true; } //获取val的前驱 DNode* Prior(DList plist, ElemType val) { DNode* p = Search(plist, val); if (p == NULL) return NULL; return p->prior; } //获取val的后继 DNode* Next(DList plist, ElemType val) { DNode* p = Search(plist, val); if (p == NULL) return NULL; return p->next; } //输出plist的所有数据 void Show(DList plist) { for (DNode* p = plist->next; p != NULL; p = p->next) { printf("%d", p->data); } printf("\n"); } //清空数据 void Clear(DList plist) { Destroy(plist); } //销毁 void Destroy(DList plist) { DNode* p; while (plist->next!=NULL) { p = plist->next; plist->next = p->next; free(p); } }
3.测试文件(test,cpp)
#include<stdio.h> #include"dlist.h" int main() { DNode head; InitList(&head); for (int i = 0; i < 10; i++) { //Insert_head(&head, i); Insert_tail(&head, i); } DeleteVal(&head, 0); DeleteVal(&head, 1); DeleteVal(&head, 9); DeleteVal(&head, 20); Show(&head); Destroy(&head); Destroy(&head); }
数据结构:双向链表
最新推荐文章于 2023-10-15 19:18:49 发布