单项循环链表

头文件:

#ifndef __LOOP_LIST_H__
#define __LOOP_LIST_H__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct node
{
	int data;
	struct node *next;
}node,*node_p;
node_p create_loop();
node_p create_node(int data);
int empty_loop(node_p H);
void insert_head(node_p H,int data);
void show_loop(node_p H);
void insert_tail(node_p H,int data);
void delete_head(node_p H);
void delete_tail(node_p H);
void insert_location(node_p H,int data,int location);
void delete_location(node_p H,int location);
node_p delete_H(node_p H);
void show_newloop(node_p HH);
#endif

主函数:

#include "loop.h"
int main(int argc, const char *argv[])
{
	node_p H=create_loop();
	insert_head(H,1);
	insert_head(H,5);
	insert_head(H,8);
	insert_head(H,4);
	insert_head(H,22);
	insert_head(H,16);
	show_loop(H);
	putchar(10);
	printf("add 6 at the head\n");
	insert_head(H,6);
	show_loop(H);
	putchar(10);
	printf("add 66 at the end\n");
	insert_tail(H,66);
	show_loop(H);
	putchar(10);
	printf("delete the head\n");
	delete_head(H);
	show_loop(H);
	putchar(10);
	printf("delete the end\n");
	delete_tail(H);
	show_loop(H);
	putchar(10);
	printf("add 33 at the 3rd location\n");
	insert_location(H,33,3);
	show_loop(H);
	putchar(10);
	printf("delete the 4th location\n");
	delete_location(H,4);
	show_loop(H);
	putchar(10);
	printf("delete the H_node\n");
	node_p HH=delete_H(H);
	show_newloop(HH);
	return 0;
}
ub

自定义函数:

#include "loop.h"
node_p create_loop()
{
	node_p H=(node_p)malloc(sizeof(node));
	if(H==NULL)
	{
		printf("apply fail\n");
		return NULL;
	}
	H->data=0;
	H->next=H;
	return H;
}
node_p create_node(int data)
{
	node_p new=(node_p)malloc(sizeof(node));
	if(new==NULL)
	{
		printf("apply fail\n");
		return NULL;
	}
	new->data=data;
	return new;
}
int empty_loop(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;
	H->next=new;
	H->data++;
}
void show_loop(node_p H)
{
	if(H==NULL)
	{
		printf("apply fail");
		return;
	}
	if(empty_loop(H))
	{
		printf("loop is empty\n");
		return;
	}
	node_p p=H->next;
	printf("H->");
	while(p!=H)
	{
		printf("%d->",p->data);
		p=p->next;
	}
	printf("H");
	putchar(10);
}
void insert_tail(node_p H,int data)
{
	if(H==NULL)
	{
		printf("apply fail");
		return;
	}
	node_p new=create_node(data);
	node_p p=H->next;
	while(p->next!=H)
	{
		p=p->next;
	}
	new->next=p->next;
	p->next=new;
	H->data++;
}
void delete_head(node_p H)
{
	if(H==NULL)
	{
		printf("apply fail");
		return;
	}
	if(empty_loop(H))
	{
		printf("loop is empty\n");
		return;
	}
	node_p p=H->next;
	H->next=p->next;
	free(p);
	H->data--;
}
void delete_tail(node_p H)
{
	if(H==NULL)
	{
		printf("apply fail");
		return;
	}
	if(empty_loop(H))
	{
		printf("loop is empty\n");
		return;
	}
	node_p p=H->next;
	while(p->next->next!=H)
	{
		p=p->next;
	}
	node_p q=p->next;
	p->next=q->next;
	free(q);
	H->data--;
}
void insert_location(node_p H,int data,int location)
{
	if(H==NULL)
	{
		printf("apply fail");
		return;
	}
	if(location<=0||location>H->data+1)
	{
		printf("the location is wrong\n");
		return;
	}
	node_p new=create_node(data);
	node_p p=H;
	for(int i=0;i<location-1;i++)
	{
		p=p->next;
	}
	new->next=p->next;
	p->next=new;
	H->data++;
}
void delete_location(node_p H,int location)
{
	if(H==NULL)
	{
		printf("apply fail");
		return;
	}
	if(empty_loop(H))
	{
		printf("loop is empty\n");
		return;
	}
	if(location<0||location>H->data)
	{
		printf("location is wrong\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;
	free(q);
	H->data--;
}
node_p delete_H(node_p H)
{
	if(H==NULL)
	{
		printf("apply fail");
		return NULL;
	}
	node_p p=H->next;
	while(p->next!=H)
	{
		p=p->next;
	}
	p->next=H->next;
	free(H);
	return p->next;
}
void show_newloop(node_p HH)
{
	if(HH==NULL)
	{
		printf("ally is wrong\n");
		return;
	}
	node_p p=HH;
	while(p->next!=HH)
	{
		printf("%d->",p->data);
		p=p->next;
	}
	printf("%d",p->data);
	putchar(10);
}

实现:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值