双链表及其功能实现
创始人
2025-05-30 13:16:59

实现功能

ListBuyNode ————————————申请一个新节点并赋值

ListInit ———————————————计算链表的长度

ListDestory —————————————销毁链表

ListPushBack ————————————尾插

ListPushFront ————————————头插

ListPopBack —————————————尾删

ListPopFront ————————————头删

ListFindByVal ————————————按值查找链表

ListInsertAfter ————————————任意位置插入

ListErase ——————————————任意位置删除

ListPrint ——————————————打印链表

List.h

#pragma once#define _CRT_SECURE_NO_WARNINGS 1#include
#include
#include
#includetypedef int ListDataType;typedef struct ListNode
{struct ListNode* preview;struct ListNode* next;ListDataType val;
}ListNode;extern ListNode* ListBuyNode(ListDataType x);//创建一个节点
extern ListNode* ListInit();//链表初始化,创造表头
extern void DListDestory(ListNode** pphead);//销毁
extern void ListPrint(ListNode* phead);//打印
extern void ListPushBack(ListNode* phead, ListDataType x);//尾插
extern void ListPopBack(ListNode* phead);//尾删
extern void ListPushFront(ListNode* phead, ListDataType x);//头插
extern void ListPopFront(ListNode* phead);//头删
extern ListNode* ListFindVal(ListNode* phead, ListDataType x);//按值查找
extern void ListInsertAfter(ListNode* pos, ListDataType x);//在任意位置插入
extern void ListErase(ListNode* pos);//在任意位置删除

List.c

#include "DList.h"ListNode* ListInit()
{ListNode* phead = ListBuyNode(0);phead->preview = phead;phead->next = phead;return phead;
}void DListDestory(ListNode** pphead)
{ListNode* cur = (*pphead)->next;while (cur != (*pphead)){cur = cur->next;free(cur->preview);}free(cur);*pphead = NULL;
}ListNode* ListBuyNode(ListDataType x)
{ListNode* newnode = malloc(sizeof(ListNode));assert(newnode);newnode->val = x;newnode->preview = NULL;newnode->next = NULL;return newnode;
}void ListPushBack(ListNode* phead, ListDataType x)
{assert(phead);ListNode* newnode = ListBuyNode(x);assert(newnode);phead->preview->next = newnode;newnode->preview = phead->preview;newnode->next = phead;phead->preview = newnode;
}bool ListEmpty(ListNode* phead)
{return (phead->next == phead) ? true : false;
}
void ListPrint(ListNode* phead)
{assert(phead);if (ListEmpty(phead))printf("List is empty!");for (ListNode* cur = phead->next; cur != phead; cur = cur->next)printf("%d ", cur->val);printf("\n");
}void ListPopBack(ListNode* phead)
{assert(phead && phead->next != phead);ListNode* tail = phead->preview;phead->preview = tail->preview;tail->preview->next = phead;free(tail);
}void ListPushFront(ListNode* phead, ListDataType x)
{assert(phead);ListNode* newnode = ListBuyNode(x);phead->next->preview = newnode;newnode->next = phead->next;phead->next = newnode;newnode->preview = phead;
}void ListPopFront(ListNode* phead)
{assert(phead && phead->next != phead);ListNode* cur = phead->next;phead->next = cur->next;cur->next->preview = phead;free(cur);
}ListNode* ListFindVal(ListNode* phead, ListDataType x)
{assert(phead);ListNode* cur = phead->next;while (cur != phead){if (cur->val == x)return cur;cur = cur->next;}return NULL;
}void ListInsertAfter(ListNode* pos, ListDataType x)
{assert(pos);ListNode* newnode = ListBuyNode(x);newnode->preview = pos;newnode->next = pos->next;pos->next->preview = newnode;pos->next = newnode;
}void ListErase(ListNode* pos)
{assert(pos && pos->next != pos);ListNode* prev = pos->preview;ListNode* next = pos->next;prev->next = next;next->preview = prev;free(pos);
}

test.c

#include "DList.h"void test1()
{ListNode* pList = ListInit();ListPushFront(pList, 4);ListPushFront(pList, 3);ListPushFront(pList, 2);ListPushFront(pList, 1);ListPrint(pList);DListDestory(&pList);
}void test2()
{ListNode* pList = ListInit();ListPushBack(pList, 5);ListPushFront(pList, 4);ListPushFront(pList, 3);ListPushFront(pList, 2);ListPushFront(pList, 1);ListPrint(pList);ListPopBack(pList);ListPopBack(pList);ListPopBack(pList);ListPopBack(pList);ListPopBack(pList);ListPrint(pList);
}void test3()
{ListNode* pList = ListInit();ListPushBack(pList, 5);ListPushFront(pList, 4);ListPushFront(pList, 3);ListPushFront(pList, 2);ListPushFront(pList, 1);ListPrint(pList);ListPopFront(pList);ListPopFront(pList);ListPopFront(pList);ListPopFront(pList);ListPrint(pList);ListPopFront(pList);ListPrint(pList);
}void test4()
{ListNode* pList = ListInit();ListInsertAfter(pList, 5);ListInsertAfter(pList, 4);ListInsertAfter(pList, 1);ListInsertAfter(pList->next, 3);ListInsertAfter(pList->next, 2);ListPrint(pList);ListErase(pList->next->next->next->next);ListNode* cur = ListFindVal(pList, 4);if (cur != NULL)ListErase(cur);elseprintf("The node with this value does not exist!\n");ListPrint(pList);ListErase(pList->next);ListErase(pList->next);ListErase(pList->next);ListPrint(pList);
}void main()
{printf("---------------------test1----------------------\n");test1();printf("---------------------test2----------------------\n");test2();printf("---------------------test3----------------------\n");test3();printf("---------------------test4----------------------\n");test4();
}

相关内容

热门资讯

荼蘼什么意思 岁月缱绻葳蕤生香... 感谢作者【辰夕】的原创独家授权分享编辑整理:【多肉植物百科】百科君坐标:云南 曲靖春而至,季节流转,...
世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...
阿西吧是什么意思 阿西吧相当于... 即使你没有受到过任何外语培训,你也懂四国语言。汉语:你好英语:Shit韩语:阿西吧(아,씨발! )日...