1.创建循环双向链表
2.双向循环链表的头插
3.双向循环链表的遍历
4.双向循环链表的尾插
5.双向循环链表的位置插入
6.双向循环链表判空
7.双向循环链表头删
8.双向循环链表尾删
9.双向循环链表位置删
10.双向循环链根据位置查询数据
11.双向循环链根据位置更新数据
代码:
dploop.h
#ifndef __DPLOOP_H__
#define __DPLOOP_H_
#include <stdio.h>
#include <stdlib.h>
#define datatype int
typedef struct node
{
struct node *pre;
struct node *next;
datatype data;
}dploop_t;
/*
*功能:创建一个循环链表
*参数:
* @::无
*返回值:dploop_t*
*/
dploop_t* DPLoopCreate(void);
/*
*功能:循环双链表头插
*参数:
* @:h:头节点
*返回值:成功返回 0 失败 -1
*/
int DPLoopInsertHead(dploop_t *h,datatype data);
/*
*功能:循环双链表尾插
*参数:
* @:h:头节点
*返回值:成功返回 0 失败 -2
*/
int DPLoopInsertTail(dploop_t *h,datatype data);
/*
*功能:任意位置插入
*参数:
* @:h:头节点
data:插入的数据
*返回值: 成功返回 0 失败-3
*/
int DPLoopInsertByPos(dploop_t *h,int pos,datatype data);
/*
*功能:双向遍历链表
*参数:
* @:h:头节点
*返回值:无
*/
void DPLoopShow(dploop_t *h);
/*
*功能:判空
*参数:
* @:h:头节点
*返回值:空返回 1 非空返回 0
*/
int DPLoopIsEmpty(dploop_t *h);
/*
*功能:循环双链表头删
*参数:
* @:h:头节点
*返回值: 成功返回所删除的值 失败返回 -4
*/
datatype DPLoopDeleteHead(dploop_t *h);
/*
*功能:循环双链表尾删
*参数:
* @:h:头节点
*返回值:成功返回 删除的值 失败返回 -5
*/
datatype DPLoopDeleteTail(dploop_t *h);
/*
*功能: 循环双链表任意位置删除
*参数:
* @:h: 头节点
top:删除所在位置
*返回值: 成功返回 被删除的值 失败返回 -6
*/
datatype DPLoopDeleteByPos(dploop_t *h,int pos);
/*
*功能:循环双链表按位查找
*参数:
* @:h:头节点
pos:查询位置
*返回值: 成功返回查询的值 失败返回-7
*/
datatype DPLoopCheckDataByPos(dploop_t *h,int pos);
/*
*功能:循环双链表按位置更新值
*参数:
* @:h:头节点
pos:位置
data:更改的值
*返回值:成功返回 0 失败返回 -8
*/
int DPLoopUpdateDataByPos(dploop_t *h,int pos, datatype data);
#endif
dploop.c
#include "loop.h"
dploop_t* DPLoopCreate(void)
{
dploop_t *h;
h = (dploop_t *)malloc(sizeof(*h));
if(h == NULL) {
printf("%s malloc is fail!\n",__func__);
return NULL;
}
h->data = 0;
h->next = h;
h->pre = h;
return h;
}
int DPLoopInsertHead(dploop_t *h,datatype data)
{
dploop_t *temp;
temp = (dploop_t *)malloc(sizeof(*temp));
if(h == NULL) {
printf("%s malloc is fail!\n",__func__);
return -1;
}
temp->data = data;
temp ->next = h->next;
h->next->pre = temp;
h->next = temp;
temp->pre = h;
return 0;
}
int DPLoopInsertTail(dploop_t *h,datatype data)
{
dploop_t *temp;
temp = (dploop_t *)malloc(sizeof(*temp));
if(h == NULL) {
printf("%s malloc is fail!\n",__func__);
return -2;
}
temp->data = data;
temp->pre = h->pre;
h->pre->next = temp;
temp->next = h;
h->pre = temp;
return 0;
}
int DPLoopInsertByPos(dploop_t *h,int pos,datatype data)
{
if(pos < 0) {
printf("%s pos left over\n",__func__);
return -3;
}
dploop_t *temp;
temp = (dploop_t *)malloc(sizeof(*temp));
if(h == NULL) {
printf("%s malloc is fail!\n",__func__);
return -1;
}
dploop_t *th = h;
while (pos--)
{
h = h->next;
if(h == th) {
printf("%s pos right over\n",__func__);
return -3;
}
}
temp->data = data;
temp ->next = h->next;
h->next->pre = temp;
h->next = temp;
temp->pre = h;
return 0;
}
void DPLoopShow(dploop_t *h)
{
dploop_t *th = h;
while (h->next != th)
{
h = h->next;
printf("-%d",h->data);
}
printf("-\n");
while (h != th)
{
printf("-%d",h->data);
h = h->pre;
}
printf("-\n");
}
int DPLoopIsEmpty(dploop_t *h)
{
return h->next == h;
}
datatype DPLoopDeleteHead(dploop_t *h)
{
if(DPLoopIsEmpty(h)) {
printf("%s dploop is Empty\n",__func__);
return (datatype)-4;
}
int data ;
dploop_t *temp;
temp = h->next;
data = temp->data;
temp->next->pre = h;
h->next = temp->next;
free(temp);
temp = NULL;
return data;
}
datatype DPLoopDeleteTail(dploop_t *h)
{
if(DPLoopIsEmpty(h)) {
printf("%s dploop is Empty\n",__func__);
return (datatype)-5;
}
int data ;
dploop_t *temp;
temp = h->pre;
data = temp->data;
h->pre = temp->pre;
temp->pre->next = h;
free(temp);
temp = NULL;
return data;
}
datatype DPLoopDeleteByPos(dploop_t *h,int pos)
{
if(DPLoopIsEmpty(h)) {
printf("%s dploop is Empty\n",__func__);
return (datatype)-6;
}
if(pos < 0) {
printf("%s pos left over\n",__func__);
return -6;
}
dploop_t *th = h;
while (pos--)
{
h = h->next;
if(h->next == th) {
printf("%s pos right over\n",__func__);
return -6;
}
}
dploop_t *temp;
temp = h->next;
int data = temp->data;
h->next = temp->next;
temp->next->pre = h;
return data;
}
datatype DPLoopCheckDataByPos(dploop_t *h,int pos)
{
if(DPLoopIsEmpty(h)) {
printf("%s dploop is Empty\n",__func__);
return (datatype)-7;
}
if(pos < 0) {
printf("%s pos left over\n",__func__);
return -7;
}
dploop_t *th = h;
while (pos--)
{
h = h->next;
if(h->next == th) {
printf("%s pos right over\n",__func__);
return -7;
}
}
int data = h->next->data;
return data;
}
int DPLoopUpdateDataByPos(dploop_t *h,int pos, datatype data)
{
if(DPLoopIsEmpty(h)) {
printf("%s dploop is Empty\n",__func__);
return (datatype)-8;
}
if(pos < 0) {
printf("%s pos left over\n",__func__);
return -8;
}
dploop_t *th = h;
while (pos--)
{
h = h->next;
if(h->next == th) {
printf("%s pos right over\n",__func__);
return -8;
}
}
h->next->data = data;
return 0;
}
main.c(主要是测试用的)
#include "loop.h"
int main(int argc, char const *argv[])
{
dploop_t *h;
h = DPLoopCreate();
DPLoopInsertHead(h,111);
DPLoopInsertHead(h,222);
DPLoopInsertHead(h,333);
DPLoopInsertTail(h,555);
DPLoopInsertTail(h,666);
DPLoopInsertByPos(h,0,234);
DPLoopShow(h);
DPLoopInsertByPos(h,6,777);
DPLoopShow(h);
DPLoopDeleteHead(h);
DPLoopShow(h);
DPLoopDeleteTail(h);
DPLoopShow(h);
DPLoopDeleteByPos(h,4);
DPLoopShow(h);
printf("%d\n",DPLoopCheckDataByPos(h,3));
DPLoopUpdateDataByPos(h,4, 256);
DPLoopShow(h);
return 0;
}