#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int DataType;
typedef struct DoubleLinkList
{
struct DoubleLinkList* prev;
struct DoubleLinkList* next;
DataType data;
}DLL;
void init(DLL** ppl);//初始化
void clear(DLL** ppl);//清空
void destroy(DLL* pl);//销毁
void print(DLL* pl);//打印
DLL* node(DataType x, DLL* prev, DLL* next);//获取新节点
void addLast(DLL* pl, DataType x);//添加
void add(DLL* pl, int idx, DataType x);//索引添加
DLL* removeLast(DLL* pl);//删除
DLL* remove(DLL* pl, int idx);//索引删除
DLL* getLast(DLL* pl);//查询
DLL* get(DLL* pl, int idx);//索引查询
int find(DLL* pl, DataType x);//查询索引
void set(DLL* pl, int idx, DataType x);//修改
int isEmpty(DLL* pl);//判空
int size(DLL* pl);//获取大小
#include "DLlist.h"
void init(DLL** ppl)
{
assert(ppl);
DLL* new = (DLL*)malloc(sizeof(DLL));//获取头节点
if (!new)
{
printf("失败");
exit(-1);
}
new->next = new;
new->prev = new;
*ppl = new;
}
void clear(DLL** ppl)
{
assert(ppl);
init(ppl);
}
void destroy(DLL* pl)
{
assert(pl);
DLL* cur = pl->next;
DLL* next = cur->next;
//释放数据节点
while (cur != pl) {
next = cur->next;//获取下一个节点
free(cur);
cur = next;
}
free(pl);//释放头节点
}
void print(DLL* pl)
{
assert(pl);
DLL* cur = pl->next;
while (cur != pl)
{
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}
DLL* node(DataType x,DLL* prev, DLL* next)
{
DLL* new = (DLL*)malloc(sizeof(DLL));
new->data = x;
new->prev = prev;
new->next = next;
return new;
}
void addLast(DLL* pl, DataType x)
{
assert(pl);
DLL* tail = pl->prev;
DLL* new = node(x, tail, pl);
tail->next = new;
pl->prev = new;
}
void add(DLL* pl, int idx, DataType x)
{
assert(pl);
assert(idx >= 0);
DLL* cur = idx==0?pl:get(pl, idx-1);//找到idx-1位置的节点
//idx-1节点不存在
if (cur == NULL)
return;
DLL* new = node(x, cur, cur->next);
cur->next->prev = new;
cur ->next = new;
}
DLL* removeLast(DLL* pl)
{
assert(pl);
if (isEmpty(pl))
return NULL;
DLL* cur = pl->prev->prev;//找到倒数第二个节点
DLL* ans = cur->next;
cur->next = pl;
pl->prev = cur;
return ans;
}
DLL* remove(DLL* pl, int idx)
{
assert(pl);
assert(idx >= 0);
if (isEmpty(pl))
return NULL;
DLL* cur = idx == 0 ? pl : get(pl, idx - 1);//找到idx-1位置的节点
//idx-1或idx节点不存在
if (cur == NULL || cur->next == pl)
return NULL;
DLL* ans = cur->next;
cur->next = ans->next;
ans->next->prev = cur;
return ans;
}
DLL* getLast(DLL* pl)
{
assert(pl);
if (isEmpty(pl))//空表
return NULL;
DLL* cur = pl;
while (cur->next != pl)
{
cur = cur->next;
}
return cur;
}
DLL* get(DLL* pl, int idx)
{
assert(pl);
assert(idx >= 0);
DLL* cur = pl->next;
while (idx-- && cur != pl)
{
cur = cur->next;
}
if (idx > 0||cur==pl)//节点不存在
return NULL;
return cur;
}
int find(DLL* pl, DataType x)
{
assert(pl);
int idx = 0;
DLL* cur = pl->next;
while (cur != pl)
{
if (cur->data == x)
return idx;
cur = cur->next;
idx++;
}
return -1;
}
void set(DLL* pl, int idx, DataType x)
{
assert(pl);
assert(idx >= 0);
DLL * cur = get(pl, idx);
if (cur == NULL)
return;
cur->data = x;
}
int isEmpty(DLL* pl)
{
assert(pl);
return pl->next == pl ? 1 : 0;
}
int size(DLL* pl)
{
assert(pl);
if (pl->next == pl)
return 0;
DLL* cur = pl;
int size = 0;
while (cur->next != pl)
{
cur = cur->next;
size++;
}
return size;
}