C语言-循环链表---@颜麓

循环链表

在这里插入图片描述

对循环链表的操作

1、Initialization();
2、Create_loop_list(head);//创建链表 
3、Delete_loop_list(head,300);//指定删除 
4、Head_delete_loop(head);//头删 
5、Tail_delete_loop(head);//尾删
6、Head_insert_loop(head,900);//头插 
7、Tail_insert_loop(head,800);//尾插 
8、Add_loop_list(head,200,111);//指定插入  200后插入111 
9、Length_loop_list(head)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<assert.h>


struct student
{
	int date;
	struct student *next;
 };
 
student *Initialization()
{
	student *head=(student*)malloc(sizeof(student));
	assert(head);
	if(head==NULL)
	{
		printf("申请内存失败!");
		return head;
	}
	head->date=0;
	head->next=head;
	return head;	
}

void Print_loop_list(student *head)//打印 
{
	student *p=head->next;
	while(p!=head)
	{	
		printf("%d ",p->date);	
		p=p->next;
	}
}

void Create_loop_list(student *head)//创建列表 
{
	student *p=head;
	int arr[7]={100,200,300,400,500,600,700};
	for(int i=0;i<7;i++)
	{
		student *new_node=(student*)malloc(sizeof(student));
		assert(new_node);
		new_node->date=arr[i];
		
		p->next=new_node;
		new_node->next=head;
		p=new_node;
	}
	printf("创建链表:"); 
	Print_loop_list(head);//打印 
	printf("\n");	
}

void Delete_loop_list(student *head,int num)//指定删除 
{
	student *p=head,*pre=head->next;
	while(pre->next!=head)
	{	
		p=p->next;
		pre=pre->next;
		if(pre->date==num)
		{
			break;
		}		
	}
	p->next=pre->next;
	printf("指定删除:"); 
	Print_loop_list(head);//打印 
	printf("\n");
 } 
 
 void Head_delete_loop(student *head)//头删 
 {
	head->next=head->next->next;
	printf("头删链表:"); 
	Print_loop_list(head);//打印 
	printf("\n");
 } 
 
 void Tail_delete_loop(student *head) //尾删 
 {
 	student *p=head,*pre=head->next;
 	while(pre->next!=head)
 	{
 		p=p->next;
 		pre=pre->next;
	 } 
	p->next=head;
	printf("尾删链表:");
	Print_loop_list(head);//打印 
	printf("\n");
 }
 
void Head_insert_loop(student *head,int num)//头插 
{
	student *p=head;
	student *New_head=(student *)malloc(sizeof(student));
	assert(New_head);
	New_head->next=p->next;
	p->next=New_head;
	New_head->date=num; 
	
	printf("头插链表:");
	Print_loop_list(head);//打印 
	printf("\n");
}

void Tail_insert_loop(student *head,int num)//尾插 
{
	student *p=head;
	student *New_head=(student *)malloc(sizeof(student));
	assert(New_head);
	while(p->next!=head)
	{
		p=p->next; 
	}
	p->next=New_head;
	New_head->next=head;
	New_head->date=num; 
	
	printf("尾插链表:");
	Print_loop_list(head);//打印 
	printf("\n");
}

void Add_loop_list(student *head,int num1,int num2)//指定插入 
{
	student *p=head;
	student *New_head=(student *)malloc(sizeof(student));
	assert(New_head);
	while(p->next!=head)
	{
		p=p->next; 
		if(p->date==num1)
		{
			break;
		}
	}
	New_head->next=p->next;
	p->next=New_head;
	New_head->date=num2;
	
	printf("指定插入:");
	Print_loop_list(head);//打印 
	printf("\n");
}

int Length_loop_list(student *head)//链表长度 
{
	student *p=head;
	int length=0;
	while(p->next!=head)
	{	
		p=p->next;
		length++;
	}
	return length;
}

int main()
{
	student *head=Initialization();
	Create_loop_list(head);//创建链表 
	Delete_loop_list(head,300);//指定删除 
	Head_delete_loop(head);//头删 
	Tail_delete_loop(head);//尾删
	Head_insert_loop(head,900);//头插 
	Tail_insert_loop(head,800);//尾插 
	Add_loop_list(head,200,111);//指定插入  200后插入111 
	printf("链接长度:%d\n",Length_loop_list(head));//链表长度
	
	return 0;
}

运行结果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值