先定义一个结构体
#include<iostream>
#include<cassert>
using namespace std;
typedef int ElementDate;
typedef struct ListNode
{
ElementDate date;
struct ListNode* next;//指向下一个
struct ListNode* prev;//指向前一个
}ListNode;
初始化
ListNode* ListInit()
{
ListNode* phead = (ListNode*)malloc(sizeof(ListNode));//哨兵位
phead->next = phead;
phead->prev = phead;
return phead;
}
查找功能,返回的是节点指针,给下面的指定位置增删服务
ListNode* ListFind(ListNode* phead,ElementDate x)
{
assert(phead);
ListNode* cur = phead->next;
while (cur != phead)
{
if (cur->date == x)
return cur;
else cur = cur->next;
}
return NULL;
}
申请一个新节点
ListNode* buyListNode(ElementDate x)
{
ListNode* phead = (ListNode*)malloc(sizeof(ListNode));
phead->date = x;
phead->next = NULL;
phead->prev = NULL;
return phead;
}
尾插
void ListPushBack(ListNode* phead, ElementDate x)
{//改变结构体不需要二级指针
//或者直接ListInsert(phead, x);
assert(phead);
ListNode* tail = phead->prev;//尾节点
ListNode* newnode = buyListNode(x);
tail->next = newnode;
newnode->prev = tail;
phead->prev = newnode;
newnode->next = phead;
}
尾删
void ListPopBack(ListNode* phead)
{
assert(phead);
if (phead->next == phead)return;
//assert(phead->next!=phead);
ListNode* tail = phead->prev;
ListNode* prevtail = tail->prev;
prevtail->next = phead;
phead->prev = prevtail;
free(tail);
}
头插
void ListPushFront(ListNode* phead, ElementDate x)
{
//或ListInsert(phead->next, x);
assert(phead);
ListNode* newnode = buyListNode(x);
ListNode* next = phead->next;
phead->next = newnode;
newnode->prev = phead;
newnode->next = next;
next->prev = newnode;
}
头删
void ListPopFront(ListNode* phead)
{
assert(phead);
assert(phead->next != phead);
ListNode* head = phead->next;
phead->next = head->next;
head->prev = phead;
free(head);
}
指定位置前插入,前面的尾插,头插都可以调用这个简单很多
void ListInsert(ListNode* pos, ElementDate x)//pos之前插入
{
assert(pos);
ListNode* newnode = buyListNode(x);
ListNode* pospre = pos->prev;
pos->prev = newnode;
newnode->next = pos;
newnode->prev = pospre;
pospre->next = newnode;
}
指定位置删除,形参是一个节点的指针,前面头删尾删可以用
void ListErase(ListNode* pos)//删除pos
{
assert(pos);
ListNode* pospre = pos->prev;
pospre->next = pos->next;
pos->next->prev = pospre;
free(pos);
}
清空链表
void ListDestroy(ListNode* phead)
{
assert(phead);
ListNode* cur = phead->next;
ListNode* next = cur;
while (next != phead)
{
cur = cur->next;
free(next);
next = cur;
}
//一级指针不能改变形参,在外面置空 free(phead);
phead = NULL;
}