双链表的实现:
#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;
int main()
{
TestList2();
return 0;
}
void TestList1()
{
LTNode* plist=ListInit();
ListPushBack(plist,1);
ListPushBack(plist,2);
ListPushBack(plist,3);
ListPushBack(plist,4);
ListPrint(plist);
}
void TestList2()
{
LTNode* plist=ListInit();
ListPushFront(plist,1);
ListPushFront(plist,2);
ListPushFront(plist,3);
ListPushFront(plist,4);
ListPrint(plist);
ListPopBack(plist);
LTNode* pos=ListFind(plist,2);
if (pos)
{
ListErase(pos);
}
ListPrint(plist);
}
LTNode* ListInit(); //初始化
{
//哨兵位头节点
LTNode* phead=(LTNode*)malloc(sizeof(LTNode));
phead->next=phead;
phead->prev=phead;
return phead;
}
void ListDestry(LTNode* phead);
{
assert(phead);
LTNode* cur =phead->next;
while(cur!=phead)
{
LTNode* next=cur->next;
free(cur);
cur=next;
}
free(phead);
phead=NULL;
}
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;
}
printf("\n");
}
void ListPushBack(LTNode* phead,LTDateType x);//尾插
{
assert(phead);
/*LTNode* tail=phead->prev;
LTNode* newnode=BuyListNode(x);
//phead tail newnode
tail->next=newnode;
newnode->prev=tail;
newnode->next=phead;
phead->prev=newnode;*/
ListInsert(phead,x);
}
void ListPopBack(LTNode* phead)
{
assert(phead);
assert(phead->next != phead);
LTNode* tail =phead->prev;
LTNode* tailPrev=tail->prev;
free(tail);
tailPrev->next=phead;
phead->prev=tailPrev;
}
void ListPopFront(LTNode* phead,LTDateType x)//头插
{
assert(phead);
/*LTNode* newnode=BuyListNode(x);
LTNode* next =phead->next;
phead->next=newnode;
newnode->prev=phead;
newnode->next=next;
next->prev=newnode;*/
ListInsert(phead->next,x);
}
void ListPopFront(LTNode* phead) //头删
{
asssert(phead);
//链表为空
assert(phead->next !=phead);
LTNode* next=phead->next;
LTNode* nextNext=next->next;
phead->next=nextNext;
nextNext->prev=phead;
free(next);
}
LTNode* ListFind(LTNode* phead,LTDateType x)
{
assert(phead);
LTNode* cur=phead->next;
while(cur !=phead)
{
if (cur->data==x)
{
return cur;
}
cur=cur->next;
}
return NULL;
}
//pos位置之前插入
void ListInsert(LTNode* pos,LTDateType x)
{
assert(pos);
LTNode* posPrev=pos->prev;
LTNode* newnode=BuyListNode(x);
//posPrev newnode pos
posPrev->next=newnode;
newnode->prev=posPrev;
newnode->next=pos;
pos->prev=newnode;
}
//删除pos位置
void ListErase(LTNode* pos)
{
assert(pos);
LTNode* posPrev=pos->prev;
LTNode* posNext=pos->next;
posPrev->next=posNext;
posNext->prev=posPrev;
free(pos);
pos=NULL;
}
void TestList1()
{
LTNode* plist=ListInit();
ListPushBack(plist,1);
ListPushBack(plist,2);
ListPushBack(plist,3);
ListPushBack(plist,4);
}
LTNode* ListInit(); //初始化
{
//哨兵位头节点
LTNode* 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=(LTNode*)malloc(sizeof(LTNode));
newnode->data=x;
//phead tail newnode
tail->next=newnode;
newnode->prev=tail;
newnode->next=phead;
phead->prev=newnode;
}