数据结构双向循环链表

主程序

#include "fun.h"

int main(int argc, const char *argv\[\])

{

double_p H=create_head();

insert_head(H,10);

insert_head(H,20);

insert_head(H,30);

insert_head(H,40);

insert_tail(H,50);

show_link(H);

del_tail(H);

show_link(H);

free_link(H);

H=NULL;

show_link(H);

return 0;

}

源程序

#include "fun.h"

//创建头节点

double_p create_head()

{

double_p H=(double_p)malloc(sizeof(node));

if(H==NULL)

{

printf("节点申请失败\n");

return NULL;

}

H->prior=H;

H->next=H;

H->len=0;

return H;

}

//创建新节点

double_p create_new(typdata data)

{

double_p new=(double_p)malloc(sizeof(node));

if(new==NULL)

{

printf("节点申请失败\n");

return NULL;

}

new->prior=NULL;

new->next=NULL;

new->data=data;

return new;

}

//判空

int empty_link(double_p H)

{

if(H==NULL)

{

printf("入参为空,请检查\n");

return -1;

}

return H->len==0;

}

//头插

void insert_head(double_p H,typdata data)

{

if(H==NULL)

{

printf("入参为空,请检查\n");

return;

}

double_p new=create_new(data);

new->next=H->next;

H->next=new;

new->next->prior=new;

new->prior=H;

H->len++;

}

//遍历

void show_link(double_p H)

{

if(H==NULL)

{

printf("入参为空,请检查\n");

return;

}

if(empty_link(H))

{

printf("链表已空,无需遍历\n");

return;

}

double_p p=H;

while(p->next!=H)

{

p=p->next;

printf("<-%d->",p->data);

}

putchar(10);

}

//尾插

void insert_tail(double_p H,typdata data)

{

if(H==NULL)

{

printf("入参为空,请检查\n");

return;

}

double_p p=H;

while(p->next!=H)

{

p=p->next;

}

double_p new=create_new(data);

new->next=p->next;

p->next=new;

new->prior=p;

H->prior=new;

H->len++;

}

//尾删

void del_tail(double_p H)

{

if(H==NULL)

{

printf("入参为空,请检查\n");

return;

}

if(empty_link(H))

{

printf("链表已空,无需删除\n");

return;

}

double_p p=H;

while(p->next->next!=H)

{

p=p->next;

}

free(p->next);

p->next=NULL;

p->next=H;

H->prior=p;

H->len--;

}

//释放链表

void free_link(double_p H)

{

if(H==NULL)

{

printf("入参为空,请检查\n");

}

while(H->next != H)

{

del_tail(H);

}

free(H);

H=NULL;

}

头文件

#ifndef _FUN_H

#define _FUN_H

#include <myhead.h>

typedef int typdata;

typedef struct node

{

union

{

typdata data;

int len;

};

struct node * prior;

struct node *next;

}node,*double_p;

double_p create_head();//创建头节点

double_p create_new(typdata data);//创建新节点

int empty_link(double_p H);//判空

void insert_head(double_p H,typdata data);//头插

void show_link(double_p);//遍历

void insert_tail(double_p H,typdata data);//尾插

void del_tail(double_p H);//尾删

void free_link(double_p H);//释放链表

#endif

相关推荐
袋鼠云数栈10 小时前
数栈 V7.0 多模态数据智能平台:打造 AI-Ready 的企业数据底座
大数据·数据结构·数据库·人工智能·数据治理·多模态
迈巴赫车主11 小时前
优先队列(PriorityQueue)
数据结构·算法
Boom_Shu11 小时前
构造函数程序
数据结构·算法
Lucky_ldy11 小时前
数据结构从入门到精通:链表
数据结构·链表
邪修king12 小时前
C++map_set封装 : 红黑树底层迭代器以及仿函数的运用
android·c语言·数据结构·c++·b树
醉颜凉12 小时前
Elasticsearch 核心数据结构:FST 原理与应用场景全解析
数据结构·elasticsearch·jenkins
love_muming12 小时前
从 ArrayList 到 LinkedList:Java 集合中数组与链表的深度对比
java·数据结构·链表
05候补工程师12 小时前
【408数据结构】核心考点:图(Graph)精炼笔记与算法直觉
数据结构·经验分享·笔记·考研·算法·图论
并不喜欢吃鱼12 小时前
从零开始 C++------ 十四【C++ 数据结构】unordered_map/unordered_set 全解析:从使用到底层模拟实现
开发语言·数据结构·c++
小欣加油12 小时前
leetcode3633 最早完成陆地和水上游乐设施的时间I
数据结构·c++·算法·leetcode