2024.2.3 作业

1、实现单向循环链表的头插头删尾插尾删

cs 复制代码
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef int datatype;
typedef struct node
{
	//数据域
	int data;
	//指针域
	struct node *next;
}*Linklist;
Linklist create()
{
	Linklist s=(Linklist)malloc(sizeof(struct node));
	if(s==NULL)
		return NULL;
	s->data=0;
	s->next=s;
	return s;
}
Linklist head_insert(Linklist head,datatype element)
{
	Linklist s=create();
	s->data=element;
	if(NULL==head)
	{
		head=s;
	}
	else
	{
		Linklist p=head;
		while(p->next!=head)
			p=p->next;
		s->next=head;
		head=s;
		p->next=head;
	}
	return head;
}
void output(Linklist head)
{
	if(head==NULL)
	{
		puts("empty!");
		return;
	}
	Linklist p=head;
	do
	{
		printf("%-5d",p->data);
		p=p->next;
	}while(p!=head);
	puts("");
}
//头删
Linklist head_delete(Linklist head)
{
	if(head==NULL)
		return NULL;
	else
	{
		Linklist p=head;
		while(p->next!=head)
			p=p->next;
		Linklist del=head;
		head=head->next;
		free(del);
		p->next=head;
		del=NULL;
	}
	return head;
}
Linklist rear_insert(Linklist head,datatype element)
{
	Linklist p=head;
	Linklist s=create();
	s->data=element;
	if(head==NULL)
	{
		head=s;
	}
	else
	{
		while(p->next!=head)
		{
			p=p->next;
		}
		p->next=s;
		s->next=head;
	}
	return head;

}
Linklist rear_delete(Linklist head)
{
	Linklist p=head;
	if(head==NULL)
		return NULL;
	else
	{
		while(p->next==head)
		{
			free(head);
			head=NULL;
			return head;
		}
		while(p->next->next!=head)
		{
			p=p->next;
		}
		Linklist del=p->next;
		free(del);
		p->next=head;
	}
	return head;
}
int main(int argc, const char *argv[])
{
	Linklist head=NULL;
	datatype element;
	int n;
	printf("please enter the len of linklist:");
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{	
		printf("please enter the %dth num:",i+1);
		scanf("%d",&element);
		//头插
		head=head_insert(head,element);
		//尾插
		//head=rear_insert(head,element);
	}
	output(head);
	//头删
	head=head_delete(head);
	printf("the new linklist is:");
	output(head);
	//尾删
	//head=rear_delete(head);
	//printf("the new linklist is:");
	//output(head);
	return 0;
}

2、编程实现单向循环链表的约瑟夫环

cs 复制代码
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef int datatype;
typedef struct node
{
	//数据域
	int data;
	//指针域
	struct node *next;
}*Linklist;
Linklist create()
{
	Linklist s=(Linklist)malloc(sizeof(struct node));
	if(s==NULL)
		return NULL;
	s->data=0;
	s->next=s;
	return s;
}
Linklist head_insert(Linklist head,datatype element)
{
	Linklist s=create();
	s->data=element;
	if(NULL==head)
	{
		head=s;
	}
	else
	{
		Linklist p=head;
		while(p->next!=head)
			p=p->next;
		s->next=head;
		head=s;
		p->next=head;
	}
	return head;
}
void output(Linklist head)
{
	if(head==NULL)
	{
		puts("empty!");
		return;
	}
	Linklist p=head;
	do
	{
		printf("%-5d",p->data);
		p=p->next;
	}while(p!=head);
	puts("");
}
//头删
Linklist head_delete(Linklist head)
{
	if(head==NULL)
		return NULL;
	else
	{
		Linklist p=head;
		while(p->next!=head)
			p=p->next;
		Linklist del=head;
		head=head->next;
		free(del);
		p->next=head;
		del=NULL;
	}
	return head;
}
Linklist rear_insert(Linklist head,datatype element)
{
	Linklist p=head;
	Linklist s=create();
	s->data=element;
	if(head==NULL)
	{
		head=s;
	}
	else
	{
		while(p->next!=head)
		{
			p=p->next;
		}
		p->next=s;
		s->next=head;
	}
	return head;

}
Linklist rear_delete(Linklist head)
{
	Linklist p=head;
	if(head==NULL)
		return NULL;
	else
	{
		while(p->next==head)
		{
			free(head);
			head=NULL;
			return head;
		}
		while(p->next->next!=head)
		{
			p=p->next;
		}
		Linklist del=p->next;
		free(del);
		p->next=head;
	}
	return head;
}
Linklist josefh(Linklist head,int n,int m)
{
	if(head==NULL)
		return head;
	Linklist p=head;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m-2;j++)
			p=p->next;
		Linklist del=p->next;
		printf("%-5d",del->data);
		p->next=del->next;
		free(del);
		del=NULL;
		p=p->next;
	}
	puts("");
	return head;
}
int main(int argc, const char *argv[])
{
	Linklist head=NULL;
	datatype element;
	int n;
	printf("please enter the len of linklist:");
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{	
		printf("please enter the %dth num:",i+1);
		scanf("%d",&element);
		//头插
		//head=head_insert(head,element);
		//尾插
		head=rear_insert(head,element);
	}
	//output(head);
	//头删
	//head=head_delete(head);
	//printf("the new linklist is:");
	//output(head);
	//尾删
	//head=rear_delete(head);
	//printf("the new linklist is:");
	//output(head);
	//约瑟夫环
	datatype m;
	printf("please enter the m:");
	scanf("%d",&m);
	head=josefh(head,n,m);
	return 0;
}

3、实现单向循环链表的排序

cs 复制代码
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef int datatype;
typedef struct node
{
	//数据域
	int data;
	//指针域
	struct node *next;
}*Linklist;
Linklist create()
{
	Linklist s=(Linklist)malloc(sizeof(struct node));
	if(s==NULL)
		return NULL;
	s->data=0;
	s->next=s;
	return s;
}
void output(Linklist head)
{
	if(head==NULL)
	{
		puts("empty!");
		return;
	}
	Linklist p=head;
	do
	{
		printf("%-5d",p->data);
		p=p->next;
	}while(p!=head);
	puts("");
}

Linklist rear_insert(Linklist head,datatype element)
{
	Linklist p=head;
	Linklist s=create();
	s->data=element;
	if(head==NULL)
	{
		head=s;
	}
	else
	{
		while(p->next!=head)
		{
			p=p->next;
		}
		p->next=s;
		s->next=head;
	}
	return head;

}
datatype length(Linklist head)
{
	int len=0;
	Linklist p=head;

	do
	{
		p=p->next;
		len++;
	}while(p!=head);
	return len;

}
void Bubble(Linklist head)
{
	if(head==NULL)
		return;
	int len=length(head);
	for(int i=1;i<len;i++)
	{
		Linklist p=head;
		for(int j=0;j<len-i;j++)
		{
			if(p->data>p->next->data)
			{
				datatype t=p->data;
				p->data=p->next->data;
				p->next->data=t;
			}
				p=p->next;
		}

	}
}
int main(int argc, const char *argv[])
{
	Linklist head=NULL;
	datatype element;
	int n;
	printf("please enter n:");
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{	
		printf("please enter the %dth num:",i+1);
		scanf("%d",&element);
	
		//尾插
		head=rear_insert(head,element);
	}
	//冒泡排序
	Bubble(head);
	output(head);
	return 0;
}
相关推荐
消失的旧时光-19435 分钟前
ScheduledExecutorService
android·java·开发语言
CHANG_THE_WORLD6 分钟前
switch语句在汇编层面的几种优化方式 ,为什么能进行优化
汇编·算法·switch·汇编分析·switch case·switch case 汇编·switch case 语句
勇闯逆流河6 分钟前
【C++】用红黑树封装map与set
java·开发语言·数据结构·c++
山,离天三尺三6 分钟前
深度拷贝详解
开发语言·c++·算法
Blossom.11815 分钟前
把AI“撒”进农田:基于极值量化与状态机的1KB边缘灌溉决策树
人工智能·python·深度学习·算法·目标检测·决策树·机器学习
future_studio30 分钟前
聊聊 Unity(小白专享、C# 小程序 之 加密存储)
开发语言·小程序·c#
一只鱼^_32 分钟前
第 167 场双周赛 / 第 471 场周赛
数据结构·b树·算法·leetcode·深度优先·近邻算法·迭代加深
云知谷42 分钟前
【嵌入式基本功】单片机嵌入式学习路线
linux·c语言·c++·单片机·嵌入式硬件
m0_736927041 小时前
Spring Boot自动配置与“约定大于配置“机制详解
java·开发语言·后端·spring
被制作时长两年半的个人练习生1 小时前
近期的笔试和面试的复盘
算法·面试·职场和发展·算子