双向循环链表

文章详细介绍了如何使用C语言实现一个双向链表数据结构,包括创建链表、在头部、尾部以及指定位置插入节点,以及删除头部、尾部和指定位置的节点。示例代码展示了这些操作的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

头文件

#ifndef __DOUBLELOOP__
#define __DOUBLELOOP__
#include <stdio.h>
#include <stdlib.h>
typedef struct doubleloop
{
	int data;
	struct doubleloop *pri;
	struct doubleloop *next;
}node,*node_p;
node_p create_doubleloop();
node_p create_node(int data);
int empty_doubleloop(node_p H);
void insert_head(node_p H,int data);
void show(node_p H);
void delete_head(node_p H);
void insert_tail(node_p H,int data);
void delete_tail(node_p H);
void insert_location(node_p H,int location,int data);
void delete_location(node_p H,int location);

#endif

主函数

#include "doubleloop.h"
int main(int argc, const char *argv[])
{
	node_p H=create_doubleloop();
	insert_head(H,10);
	insert_head(H,8);
	insert_head(H,6);
	insert_head(H,4);
	insert_head(H,2);
	insert_head(H,0);
	show(H);
	printf("add -2 at the head\n");
	insert_head(H,-2);
	show(H);
	printf("delete the head\n");
	delete_head(H);
	show(H);
	printf("add 12 in the end\n");
	insert_tail(H,12);
	show(H);
	printf("delete the end\n");
	delete_tail(H);
	show(H);
	printf("add 3 at the location 3\n");
	insert_location(H,3,3);
	show(H);
	printf("delete location 4\n");
	delete_location(H,4);
	show(H);
	return 0;
}

自定义函数

#include "doubleloop.h"
node_p create_doubleloop()
{
	node_p H=(node_p)malloc(sizeof(node));
	if(H==NULL)
	{
		printf("apply fail\n");
		return NULL;
	}
	H->data=0;
	H->next=H;
	H->pri=H;
	return H;
}
node_p create_node(int data)
{
	node_p new=(node_p)malloc(sizeof(node));
	new->data=data;
	return new;
}
int empty_doubleloop(node_p H)
{
	if(H==NULL)
	{
		printf("apply fail\n");
		return -1;
	}
	return H->next==H?1:0;
}
void insert_head(node_p H,int data)
{
	if(H==NULL)
	{
		printf("apply fail\n");
		return;
	}
	node_p new=create_node(data);
	new->next=H->next;
	new->pri=H;
	H->next=new;
	new->next->pri=new;
	H->data++;
}
void show(node_p H)
{
	if(H==NULL)
	{
		printf("apply fail\n");
		return;
	}
	if(empty_doubleloop(H))
	{
		printf("void\n");
		return;
	}
	node_p p=H->next;
	printf("H->");
	while(p!=H)
	{
		printf("%d->",p->data);
		p=p->next;
	}
	printf("H");
	putchar(10);
	printf("upside down\n");
	node_p q=H->pri;
	printf("H->");
	while(q!=H)
	{
		printf("%d->",q->data);
		q=q->pri;
	}
	printf("H");
	putchar(10);
	putchar(10);
}
void delete_head(node_p H)
{
	if(H==NULL)
	{
		printf("apply fail\n");
		return;
	}
	if(empty_doubleloop(H))
	{
		printf("void\n");
		return;
	}
	node_p q=H->next;
	H->next=q->next;
	q->next->pri=H;
	free(q);
	H->data--;
}
void insert_tail(node_p H,int data)
{
	if(H==NULL)
	{
		printf("apply fail\n");
		return;
	}
	node_p p=H->pri;
	node_p new=create_node(data);
	new->next=H;
	new->pri=p;
	p->next=new;
	H->pri=new;
	H->data++;
}
void delete_tail(node_p H)
{
	if(H==NULL)
	{
		printf("apply fail\n");
		return;
	}
	if(empty_doubleloop(H))
	{
		printf("void\n");
		return;
	}
	node_p p=H->next;
	while(p->next->next!=H)
	{
		p=p->next;
	}
	node_p q=p->next;
	p->next=q->next;
	H->pri=p;
	free(q);
	H->data--;
}
void insert_location(node_p H,int location,int data)
{
	if(H==NULL)
	{
		printf("apply fail\n");
		return;
	}
	if(location<=0||location>data)
	{
		printf("wrong location\n");
		return;
	}
	node_p p=H;
	for(int i=0;i<location-1;i++)
	{
		p=p->next;
	}
	node_p new=create_node(data);
	new->next=p->next;
	new->pri=p;
	p->next=new;
	new->next->pri=new;
	H->data++;
}
void delete_location(node_p H,int location)
{
	if(H==NULL)
	{
		printf("apply fail\n");
		return;
	}
	if(empty_doubleloop(H))
	{
		printf("void\n");
		return;
	}
	if(location<=0||location>H->data)
	{
		printf("wrong location\n");
		return;
	}
	node_p p=H;
	for(int i=0;i<location-1;i++)
	{
		p=p->next;
	}
	node_p q=p->next;
	p->next=q->next;
	q->next->pri=p;
	free(q);
	H->data--;
}

实现

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值