大家好呀!👋这个是付青云同学的博客!😁
目前一直在学习C语言。🐸
写博客是为了来记录我的学习过程,同时也希望通过博客能够帮助到需要帮助的人。
如果我的博客可以帮助到你,不妨给我一个关注哦😁
前面实现了单链表
双向链表
声明部分
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int LTDateType;
typedef struct ListNode
{
LTDateType data;
struct ListNode* next;
struct ListNode* prev;
}LTNode;
//初始化
LTNode* ListInit(LTNode* phead);
//创建新节点
LTNode* BuyListNode(LTDateType x);
//打印
void ListPrint(LTNode* phead);
//尾插
void ListPushBack(LTNode* phead, LTDateType x);
//尾删
void ListPopBack(LTNode* phead);
//头插
void ListPushFront(LTNode* phead, LTDateType x);
//头删
void ListPopFront(LTNode* phead);
//查找
LTNode* ListFind(LTNode* phead, LTDateType x);
//x位置之后插入
void ListInsertBack(LTNode* pos, LTDateType x);
//x位置之前插入
void ListInsertFront(LTNode* pos, LTDateType x);
//删除
void ListErast(LTNode* pos);
//删除所有链表
void ListDestroy(LTNode* phead);
函数实现部分
#define _CRT_SECURE_NO_WARNINGS
#include "List.h"
LTNode* BuyListNode(LTDateType x)
{
LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
newnode->data = x;
newnode->next = NULL;
newnode->prev = NULL;
return newnode;
}
void ListPrint(LTNode* phead)
{
assert(phead);
LTNode* cur = phead->next;
while (cur != phead)
{
printf("%d ", cur->data);
cur = cur->next;
}
}
LTNode* ListInit(LTNode* phead)
{
//哨兵位头节点
phead = (LTNode*)malloc(sizeof(LTNode));
phead->next = phead;
phead->prev = phead;
return phead;
}
void ListPushBack(LTNode* phead, LTDateType x)
{
/*assert(phead);
LTNode* tail = phead->prev;
LTNode* newnode = BuyListNode(x);
tail->next = newnode;
newnode->prev = tail;
newnode->next = phead;
phead->prev = newnode;*/
ListInsertFront(phead, x);
}
void ListPopBack(LTNode* phead)
{
/*assert(phead);
assert(phead->next != phead);
LTNode* tail = phead->prev;
phead->prev = tail->prev;
tail->prev->next = phead;
free(tail);
tail = NULL; */
ListErast(phead->prev);
}
void ListPushFront(LTNode* phead, LTDateType x)
{
/*assert(phead);
LTNode* cur = phead->next;
LTNode* newhead = BuyListNode(x);
newhead->prev = phead;
newhead->next = cur;
phead->next =newhead;*/
ListInsertBack(phead, x);
}
void ListPopFront(LTNode* phead)
{
/*assert(phead);
assert(phead != phead->next);
LTNode* cur = phead->next;
phead->next = cur->next;
cur->next->prev = phead;
free(cur);
cur = NULL;*/
ListErast(phead->next);
}
LTNode* ListFind(LTNode* phead, LTDateType x)
{
assert(phead);
LTNode* cur = phead->next;
while (phead != cur)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
void ListInsertBack(LTNode* pos, LTDateType x)
{
assert(pos);
LTNode* newnode = BuyListNode(x);
LTNode* cur = pos->next;
newnode->next = cur;
newnode->prev = pos;
pos->next = newnode;
cur->prev = newnode;
}
void ListInsertFront(LTNode* pos, LTDateType x)
{
assert(pos);
LTNode* newnode = BuyListNode(x);
LTNode* cur = pos->prev;
newnode->next = pos;
newnode->prev = cur;
cur->next = newnode;
pos->prev = newnode;
}
void ListErast(LTNode* pos)
{
assert(pos);
assert(pos != pos->next);
LTNode* cur = pos->next;
cur->prev = pos->prev;
pos->prev->next = cur;
free(pos);
pos = NULL;
free(pos);
}
void ListDestroy(LTNode* phead)
{
assert(phead);
LTNode* cur = phead->next;
while (cur != phead)
{
LTNode* next = cur;
cur = cur->next;
free(next);
}
free(phead);
}
总结
单链表与双向链表
虽说都是链表,在结构上双向链表看起来比单链表复杂了,但是实现起来它比单链表方便多了。