该链表为带头循环双链表
头文件List.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int LTdatatype;
typedef struct ListNode
{
struct ListNode* next;
struct ListNode* pre;
LTdatatype val;
}LT;
LT* LTinit();//带头双向循环链表的初始化
void LTprint(LT* head);
LT* LTfind(LT* head, LTdatatype x);//查找值为x所在结点的位置
void LTinsert(LT* head, LT* pos, LTdatatype x);//在pos位置后添加一个值
void LTdelete(LT* head, LT* pos);//删除pos所在的结点
void LTpushback(LT* head, LTdatatype x);//尾插
void LTpushfront(LT* head, LTdatatype x);//首插
void LTpopback(LT* head);//尾删
void LTpopfront(LT* head);//头删
void LTdestroy(LT* head);//释放链表
LT* LTcreat(LTdatatype x);
源文件List.c
#include"List.h"
LT* LTinit()
{
LT* head = (LT*)malloc(sizeof(LT));
head->next = head;
head->pre = head;
return head;//哨兵位头结点
}
void LTprint(LT* head)
{
LT* cur = head->next;
while (cur != head)
{
printf("%d ", cur->val);
cur = cur->next;
}
printf("\n");
}
LT* LTfind(LT* head, LTdatatype x)
{
assert(head != head->next);
LT* cur = head->next;
while (cur != head)
{
if (cur->val == x)
return cur;
cur = cur->next;
}
return NULL;
}
void LTinsert(LT* head,LT* pos,LTdatatype x)
{
LT* next = (LT*)malloc(sizeof(LT));//开辟一个链表储存x的值
next->val = x;
next->next = pos->next;
pos->next->pre = next;
//上面两行代码与下面两行代码位置不能互换,否则不好找原来pos结点下一个结点的位置
pos->next = next;
next->pre = pos;
}
void LTdelete(LT* head, LT* pos)
{
LT* pre = pos->pre;
LT* next = pos->next;
pre->next = next;
next->pre = pre;
free(pos);
}
void LTpushback(LT* head, LTdatatype x)
{
LT* newnode = (LT*)malloc(sizeof(LT));
newnode->val = x;
LT* tail = head->pre;
newnode->next = head;
head->pre = newnode;
newnode->pre = tail;
tail->next = newnode;
//LT* tail=head->pre;
//LTinsert(head,tail,x);
//这里也可以看作pos为尾结点,在pos后插入
}
void LTpushfront(LT* head, LTdatatype x)
{
LT* newnode = (LT*)malloc(sizeof(LT));
newnode->val = x;
LT* next = head->next;
newnode->next = next;
next->pre = newnode;
newnode->pre = head;
head->next = newnode;
//LT* newnode = LTcreat(x);
//LT* tail=head->pre;
//LTinsert(head,head,x);
//这里同上可以看作pos为头结点,在pos后插入
}
void LTpopback(LT* head)
{
assert(head != head->next);
LT* tail = head->pre;
LT* newtail = tail->pre;
newtail->next = head;
head->pre = newtail;
free(tail);
//LT* newnode = LTcreat(x);
//LT* tail=head->pre;
//LTdelete(head,tail);
//这里可以先找到尾结点,令pos为尾结点,删除pos结点即是尾删
}
void LTpopfront(LT* head)
{
assert(head != head->next);
LT* next = head->next;
LT* newnext = next->next;
newnext->pre = head;
head->next = newnext;
free(next);
//LT* next=head->next;
//LTdelete(head,next);
//这里同上让pos结点为哨兵位头节点的下一个,删除pos结点即是头删
}
void LTdestroy(LT* head)
{
LT* cur = head->next;
while (cur != head)
{
LT* next = cur->next;
free(cur);
cur = next;
}
}
LT* LTcreat(LTdatatype x)
{
LT* newnode = (LT*)malloc(sizeof(LT));
newnode->val = x;
newnode->next = NULL;
newnode->pre = NULL;
}
test.c
#include"List.h"
void test1()
{
LT* plist = LTinit();
LTpushback(plist, 1);
LTpushback(plist, 2);
LTpushback(plist, 3);
LTpushback(plist, 4);
LTprint(plist);
LTpushfront(plist, 10);
LTpushfront(plist, 20);
LTpushfront(plist, 30);
LTpushfront(plist, 40);
LTprint(plist);
}
void test2()
{
LT* plist = LTinit();
LTpushback(plist, 1);
LTpushback(plist, 2);
LTpushback(plist, 3);
LTprint(plist);
LTpushfront(plist, 10);
LTpushfront(plist, 20);
LTpushfront(plist, 30);
LTprint(plist);
LTpopback(plist);
LTprint(plist);
LTpopfront(plist);
LTpopfront(plist);
LTprint(plist);
}
void test3()
{
LT* plist = LTinit();
LTpushback(plist, 1);
LTpushback(plist, 2);
LTpushback(plist, 3);
LTprint(plist);
LTpushfront(plist, 10);
LTpushfront(plist, 20);
LTpushfront(plist, 30);
LTprint(plist);
LT* pos=LTfind(plist, 2);
LTinsert(plist, pos, 6);
LTprint(plist);
LTdelete(plist, pos);
LTprint(plist);
}
void test4()
{
LT* plist = LTinit();
LTpushback(plist, 1);
LTpushback(plist, 2);
LTpushback(plist, 3);
LTprint(plist);
LTpushfront(plist, 10);
LTpushfront(plist, 20);
LTpushfront(plist, 30);
LTprint(plist);
LTpopback(plist);
LTprint(plist);
LTpopfront(plist);
LTpopfront(plist);
LTprint(plist);
LT* pos = LTfind(plist, 2);
LTinsert(plist, pos, 6);
LTprint(plist);
LTdelete(plist, pos);
LTprint(plist);
LTdestroy(plist);
}
void test5()
{
LT* plist = LTinit();
LTpopback(plist);
}
int main()
{
//test1();
//test2();
//test3();
test4();
//test5();
return 0;
}