#pragma once
#include <stdbool.h>
//带头节点的双向循环链表
//链表尾节点的next为NULL,头节点的prio为NULL
//
typedef struct DNod
{
int data; //数据
struct DNode *next; //下一个节点的地址
struct DNode *prio; //前趋节点的地址
}DNode,*DList; //List = Node *
//初始化
void InitList(DList plist);
//头插法
bool Insert_head(DList plist, int val);
//尾插法
bool Insert_tail(DList plist, int val);
//在pos下标插入数据val
bool Insert_pos(DList plist, int pos, int val);
//查找,找到返回节点地址,没有找到返回NULL
DNode *Search(DList plist, int key);
//删除第一个key对应的节点
bool Delete(DList plist, int key);
//删除第一个数据节点,并通过rtval获得删除的值
bool Delete_head(DList plist, int *rtval);
//删除最后一个数据节点,并通过rtval获得删除的值
bool Delete_tail(DList plist, int *rtval);
//获取长度,统计数据节点的个数
int GetLength(DList plist);
//判空
bool IsEmpty(DList plist);
//清除所有数据
void Clear(DList plist);
//销毁所有节点
void Destory(DList plist);
//打印
void Show(DList plist);
#include <stdio.h>
#include <stdlib.h>
#include "dlist.h"
#include <assert.h>
//带头节点的双向循环链表
//链表尾节点的next为NULL,头节点的prio为NULL
//
/*
typedef struct DNod
{
int data; //数据
struct DNode *next; //下一个节点的地址
struct Dnode *prio; //前趋节点的地址
}DNode,*DList; //List = Node *
*/
//初始化
void InitList(DList plist)
{
assert(plist != NULL);
if(plist == NULL)
{
return ;
}
plist->next = NULL;
plist->prio = NULL;
}
static DNode *BuyNode(int val)
{
DNode *p = (DNode *)malloc(sizeof(DNode));
p->data = val;
return p;
}
//头插法
bool Insert_head(DList plist, int val)
{
DNode *p = BuyNode(val);
p->next = plist->next;
if(plist->next != NULL)
{
plist->next->prio = p;
}
p->prio = plist;
plist->next = p;
return true;
}
//尾插法
bool Insert_tail(DList plist, int val)
{
DNode *p = BuyNode(val);
DNode *q;
for(q = plist; q->next != NULL; q = q->next)
{
//将p插入在q的后面
p->next = NULL;
p->prio = q;
q->next = p;
}
return true;
}
//在pos下标插入数据val
bool Insert_pos(DList plist, int pos, int val)
{
}
//查找,找到返回节点地址,没有找到返回NULL
Node *Search(DList plist, int key)
{
for(DNode *p = plist->next; p != NULL; p = p->next)
{
if(p->data == key)
{
return p;
}
}
return NULL;
}
//删除第一个key对应的节点
bool Delete(DList plist, int key)
{
DNode *p = Search(plist,key);
if(p == NULL)
{
return false;
}
p->prio->next = p->next;
if(p->next != NULL)
{
p->next->prio = p->prio;
}
free(p);
return true;
}
//删除第一个数据节点,并通过rtval获得删除的值
bool Delete_head(DList plist, int *rtval);
//删除最后一个数据节点,并通过rtval获得删除的值
bool Delete_tail(DList plist, int *rtval);
//获取长度,统计数据节点的个数
int GetLength(DList plist)
{
int count = 0;
for(DNode *p = plist->next; p != NULL; p = p->next)
{
count++;
}
return count;
}
//判空
bool IsEmpty(DList plist);
//清除所有数据
void Clear(DList plist);
//销毁所有节点
void Destory(DList plist);
//打印
void Show(DList plist)
{
for(DNode *p = plist->next; p != NULL; p = p->next)
{
printf("%d ",p->data);
}
printf("\n");
}