Doubly Linked List
Doubly List.h
#include "List.h"
//创建
ListNode* BuyListNode(LTDataType x)
{
ListNode* node = (ListNode*)malloc(sizeof(ListNode));
node->next = NULL;
node->prev = NULL;
node->data = x;
return node;
}
//数据初始化
void ListInit(ListNode** pphead)
{
*pphead = BuyListNode(0);
(*pphead)->next = *pphead;
(*pphead)->prev = *pphead;
}
//清除
void ListClear(ListNode* phead)
{
assert(phead);
ListNode* cur = phead->next;
while (cur != phead)
{
ListNode* next = cur->next;
free(cur);
cur = next;
}
phead->next = phead;
phead->prev = phead;
}
void ListDestory(ListNode** pphead)
{
assert(*pphead);
ListClear(*pphead);
free(*pphead);
*pphead = NULL;
}
//打印
void ListPrint(ListNode* phead)
{
assert(phead);
ListNode* cur = phead->next;
while (cur != phead)
{
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}
//尾插
void ListPushBack(ListNode* phead, LTDataType x)
{
//assert(phead);
phead....tail newnode
tail是尾指针,双向链表找尾是phead->prev
//ListNode* tail = phead->prev;
//ListNode* newnode = BuyListNode(x);
phead tail newnode
//tail->next = newnode;
//newnode->prev = tail;
//newnode->next = phead;
//phead->prev = newnode;
ListInsert(phead, x);
}
//尾删
void ListPopBack(ListNode* phead)
{
/*assert(phead);
assert(phead->next != phead);
ListNode* tail = phead->prev;
ListNode* tailprev = tail->prev;
tailprev->next = phead;
phead->prev = tailprev;
free(tail);
tail = NULL;*/
ListErase(phead->prev);
}
//头插
void ListPushFront(ListNode* phead, LTDataType x)
{
/*assert(phead);
ListNode* newnode = BuyListNode(x);
ListNode* first = phead->next;
newnode->next = first;
first->prev = newnode;
phead->next = newnode;
newnode->prev = phead;*/
ListInsert(phead->next, x);
}
//头删
void ListPopFront(ListNode* phead)
{
/*assert(phead);
assert(phead->next != phead);
ListNode* first = phead->next;
ListNode* second = first->next;
phead->next = second;
second->prev = phead;
free(first);*/
ListErase(phead->next);
}
//查找
ListNode* ListFind(ListNode* phead, LTDataType x)
{
assert(phead);
assert(phead->next != phead);
ListNode* cur = phead->next;
while (cur != phead)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
//值前插入
void ListInsert(ListNode* pos, LTDataType x)
{
ListNode* posPrev = pos->prev;
ListNode* newnode = BuyListNode(x);
//posPrev newnode pos;
posPrev->next = newnode;
newnode->prev = posPrev;
newnode->next = pos;
pos->prev = newnode;
}
//值删
void ListErase(ListNode* pos)
{
assert(pos);
ListNode* posPrev = pos->prev;
ListNode* posNext = pos->next;
posPrev->next = posNext;
posNext->prev = posPrev;
free(pos);
}
Doubly List.c
#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
typedef int LTDataType;
typedef struct ListNode ListNode;
struct ListNode
{
struct ListNode* next;
struct ListNode* prev;
LTDataType data;
};
//创建节点
ListNode* BuyListNode(LTDataType x);
//清除,毁灭
void ListClear(ListNode* phead);
void ListDestory(ListNode** pphead);
//初始化,打印
void ListInit(ListNode** pphead);
void ListPrint(ListNode* phead);
//尾插,尾删,头插,头删
void ListPushBack(ListNode* phead, LTDataType x);
void ListPopBack(ListNode* phead);
void ListPushFront(ListNode* phead, LTDataType x);
void ListPopFront(ListNode* phead);
//查找,值前插入,删除值
ListNode* ListFind(ListNode* phead,LTDataType x);
void ListInsert(ListNode* pos, LTDataType x);
void ListErase(ListNode* pos);
Doubly Test.c
#include "List.h"
void TestList1()
{
ListNode* phead = NULL;
//设置初始化函数(传二级指针)
ListInit(&phead);
ListPushBack(phead,1);
ListPushBack(phead,2);
ListPushBack(phead,3);
ListPushBack(phead,4);
ListPrint(phead);
ListPopBack(phead);
ListPopBack(phead);
ListPopBack(phead);
ListPopBack(phead);
//ListPopBack(phead);
//为啥没有中间值,没有搞清楚找尾是phead->prev=tail;
ListPrint(phead);
ListPushFront(phead,1);
ListPushFront(phead,2);
ListPushFront(phead,3);
ListPushFront(phead,4);
ListPushFront(phead,5);
ListPrint(phead);
ListPopFront(phead);
ListPopFront(phead);
ListPopFront(phead);
ListPopFront(phead);
ListPrint(phead);
ListDestory(&phead);
}
void TestList2()
{
ListNode* phead = NULL;
//设置初始化函数(传二级指针)
ListInit(&phead);
ListPushFront(phead, 1);
ListPushFront(phead, 2);
ListPushFront(phead, 3);
ListPushFront(phead, 4);
ListPushFront(phead, 5);
ListPrint(phead);
ListPopFront(phead);
ListPrint(phead);
ListNode* pos = ListFind(phead,3);
ListInsert(pos,30);
ListPrint(phead);
ListErase(pos);
ListPrint(phead);
ListDestory(&phead);
}
int main()
{
//单元测试
TestList1();
return 0;
}
到这里我们解题完毕
如果对您有帮助的话点一个免费的赞和收藏叭!