test.c文件
#define _CRT_SECURE_NO_WARNINGS
#include "sl.h"
void test()
{
SL* ptr = NULL;
SLInit(&ptr);
Addbehind(ptr, 1);
Addbehind(ptr, 2);
Addbehind(ptr, 3);
Addbehind(ptr, 4);
Addbehind(ptr, 5);
Addhead(ptr, 7);
Addhead(ptr, 8);
Addhead(ptr, 9);
Addhead(ptr, 10);
Print(ptr);
Popbehind(ptr);
Print(ptr);
Popbehind(ptr);
Print(ptr);
Pophead(ptr);
Print(ptr);
SL* find = Find(ptr, 9);
if (find)
{
printf("找到了\n");
}
else
{
printf("没找到\n");
}
Popposbehind(find);
Print(ptr);
Addposbehind(find, 99);
Print(ptr);
Destroy(ptr);
}
int main()
{
test();
return 0;
}
sl.h文件
#pragma once
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
typedef int date;
typedef struct sqlist
{
date x;
struct sqlist* prew;
struct sqlist* nest;
}SL;
//初始化
void SLInit(SL** pphead);
//申请节点
SL* Byenode(date x);
//尾插
void Addbehind(SL* phead, date x);
//打印
void Print(SL* phead);
//头插
void Addhead(SL* phead, date x);
//尾删
void Popbehind(SL* phead);
//头删
void Pophead(SL* phead);
//查找
SL* Find(SL* phead, date x);
//pos后插入
void Addposbehind(SL* pos, date x);
//删除pos后数据
void Popposbehind(SL* pos);
//销毁
void Destroy(SL* phead);
sl.c文件
#define _CRT_SECURE_NO_WARNINGS
#include "sl.h"
SL* Byenode(date x)
{
SL* newnode = (SL*)malloc(sizeof(SL));
if (newnode == NULL)
{
perror("Byenode()::malloc");
exit(1);
}
newnode->x = x;
newnode->nest = newnode->prew = newnode;
return newnode;
}
void SLInit(SL** pphead)
{
*pphead= Byenode(-1);
}
void Addbehind(SL* phead, date x)
{
assert(phead);
SL* new = Byenode(x);
new->nest = phead;
new->prew = phead->prew;
phead->prew->nest = new;
phead->prew = new;
}
void Print(SL* phead)
{
assert(phead);
SL* pcur = phead->nest;
while (pcur!= phead)
{
printf("%d->", pcur->x);
pcur = pcur->nest;
}
printf("\n");
}
void Addhead(SL* phead, date x)
{
assert(phead);
SL* new = Byenode(x);
new->nest = phead->nest;
new->prew = phead;
phead->nest->prew = new;
phead->nest = new;
}
void Popbehind(SL* phead)
{
assert(phead);
SL* pcur = phead;
while (pcur->nest != phead)
{
pcur = pcur->nest;
}
pcur->prew->nest = phead;
phead->prew = pcur->prew;
free(pcur);
pcur = NULL;
}
void Pophead(SL* phead)
{
assert(phead);
SL* pcur = phead->nest;
phead->nest->nest->prew = phead;
phead->nest = phead->nest->nest;
free(pcur);
pcur = NULL;
}
SL* Find(SL* phead, date x)
{
assert(phead->nest);
SL* tamp = phead->nest;
while (tamp!=phead)
{
if (tamp->x == x)
return tamp;
tamp = tamp->nest;
}
return NULL;
}
void Addposbehind(SL* pos, date x)
{
assert(pos);
SL* new = Byenode(x);
new->nest = pos->nest;
new->prew = pos;
pos->nest->prew = new;
pos->nest = new;
}
void Popposbehind(SL* pos)
{
assert(pos);
SL* tamp = pos->nest;
pos->nest->nest->prew = pos;
pos->nest = pos->nest->nest;
free(tamp);
tamp = NULL;
}
void Destroy(SL* phead)
{
assert(phead);
SL* tamp = phead->nest;
while (tamp != phead)
{
SL* pcur = tamp;
tamp = tamp->nest;
free(pcur);
pcur = NULL;
}
free(phead);
phead = NULL;
}