目录
一.声明及定义
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int LTDataType;
typedef struct ListNode
{
LTDataType data;
struct ListNode* next;
struct ListNode* prev;
}ListNode;
// 创建返回链表的头结点.
ListNode* ListCreate();
//双链表的销毁
void ListDestory(ListNode* pHead);
//双链表的打印
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);
//在pos前进行插入
void ListInsert(ListNode* pos, LTDataType x);
//删除pos结点
void ListErase(ListNode* pos);
二.函数功能的实现
#include"DList.h"
ListNode* Buynode(LTDataType x)
{
ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
if (newnode == NULL)
{
perror("malloc fail");
exit(-1);
}
else
{
newnode->data = x;
newnode->next = NULL;
newnode->prev = NULL;
}
return newnode;
}
ListNode* ListCreate()
{
ListNode* head = Buynode(0);//创建头结点
head->next = head;
head->prev = head;
return head;
}
void ListDestory(ListNode* pHead)
{
assert(pHead);
ListNode* cur = pHead;
while (cur)
{
ListNode* next = cur->next;
free(cur);
cur = next;
}
free(pHead);
}
void ListPrint(ListNode* pHead)
{
assert(pHead);
ListNode* cur = pHead->next;
while (cur!=pHead)
{
printf("%d ", cur->data);
cur = cur->next;
}
}
void ListPushBack(ListNode* pHead, LTDataType x)
{
assert(pHead);
ListNode* newnode = Buynode(x);
ListNode* tail = pHead->prev;
tail->next = newnode;
newnode->prev = tail;
pHead->prev = newnode;
newnode->next = pHead;
}
void ListPopBack(ListNode* pHead)
{
assert(pHead);
assert(pHead->next != pHead);
ListNode* tail = pHead->prev;
tail->prev->next = pHead;
pHead->prev = tail->prev;
free(tail);
}
void ListPushFront(ListNode* pHead, LTDataType x)
{
assert(pHead);
ListNode* newnode = Buynode(x);
ListNode* head = pHead->next;
newnode->next = head;
head->prev = newnode;
pHead->next = newnode;
newnode->prev = pHead;
}
void ListPopFront(ListNode* pHead)
{
assert(pHead);
assert(pHead->next != pHead);
ListNode* head = pHead->next;
pHead->next = head->next;
head->next->prev = pHead;
free(head);
}
ListNode* ListFind(ListNode* pHead, LTDataType x)
{
assert(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)
{
assert(pos);
ListNode* newnode = Buynode(x);
ListNode* prev = pos->prev;
prev->next = newnode;
newnode->prev = prev;
newnode->next = pos;
}
void ListErase(ListNode* pos)
{
assert(pos);
ListNode* prev = pos->prev;
ListNode* next = pos->next;
prev->next = next;
next->prev = prev;
free(pos);
}
三.测试函数功能
#include"DList.h"
void test()
{
ListNode* head = ListCreate();//头结点
ListPushBack(head, 1);
ListPushBack(head, 2);
ListPushBack(head, 3);
//ListPopBack(head);
ListPushFront(head, 0);
ListPopFront(head);
ListInsert(ListFind(head, 3), 0);
ListErase(ListFind(head, 0));
ListPrint(head);
printf("%d", ListFind(head, 1)->data);
}
int main()
{
test();
return 0;
}