2024.2.3 作业

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

#include
#include
#include
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

2024.2.3 作业_第1张图片

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

#include
#include
#include
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;inext;
		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

2024.2.3 作业_第2张图片

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

#include
#include
#include
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;idata>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

2024.2.3 作业_第3张图片

你可能感兴趣的:(c语言,算法,开发语言)