2月3日作业

1.编程实现单向循环链表的头插,头删、尾插、尾删

尾插/头插,头删,尾删:

头文件:

复制代码
#ifndef __HEAD_H_
#define __HEAD_H_


#include<stdio.h>
#include<string.h>
#include<stdlib.h>

enum {FALSE=-1,SUCCESS};
typedef int datatype;
typedef struct node
{
	//数据域
	datatype data;
	//指针域
	struct node *next;
}*linklist;


linklist creat();
linklist insert_head(linklist head,datatype element);
void output(linklist head);
linklist insert_rear(linklist head,datatype element);
linklist delete_head(linklist head);
linklist delete_rear(linklist head);


#endif

主函数:

复制代码
#include"head.h"


int main(int argc, const char *argv[])
{
	//头插
	linklist head=NULL;
	int n;
	printf("please enter n:");
	scanf("%d",&n);
	datatype element;
	for(int i=0;i<n;i++)
	{
		printf("please enter %d element:",i+1);
		scanf("%d",&element);
		//头插
		head=insert_head(head,element);
		//尾插
//		head=insert_rear(head,element);

	}
	output(head);
	
	//头删
	head=delete_head(head);
	output(head);

	//尾删
	head=delete_rear(head);
	output(head);

	return 0;
}

自定义函数:

复制代码
#include"head.h"


int main(int argc, const char *argv[])
{
	//头插
	linklist head=NULL;
	int n;
	printf("please enter n:");
	scanf("%d",&n);
	datatype element;
	for(int i=0;i<n;i++)
	{
		printf("please enter %d element:",i+1);
		scanf("%d",&element);
		//头插
		head=insert_head(head,element);
		//尾插
//		head=insert_rear(head,element);

	}
	output(head);
	
	//头删
	head=delete_head(head);
	output(head);

	//尾删
	head=delete_rear(head);
	output(head);






	
	return 0;
}
ubuntu@ubuntu:~/寒假作业/2.3$ cat test.c 
#include"head.h"

linklist creat()
{
	linklist head=(linklist)malloc(sizeof(struct node));
	if(head==NULL)
		return NULL;
	head->data=0;
	head->next=head;
	return head;
}
//头插
linklist insert_head(linklist head,datatype element)
{
	linklist s=creat();
	s->data=element;
	if(head==NULL)
	{
		head=s;
		return head;	
	}
	linklist p=head;
	while(p->next!=head)
		p=p->next;
	s->next=head;
	head=s;
	p->next=head;
	return head;
}

void output(linklist head)
{
	linklist s=head;
	do
	{
		printf("%-4d",s->data);
		s=s->next;
	}while(s!=head);
	puts("");
}

//尾插
linklist insert_rear(linklist head,datatype element)
{
	linklist s=creat();
	s->data=element;
	linklist p=head;
	if(head==NULL)
	{
		head=s;
		return head;	
	}
	while(p->next!=head)
		p=p->next;
	p->next=s;
	s->next=head;

	return head;
}

//头删
linklist delete_head(linklist head)
{
	if(head==NULL)
		return head;
	linklist del=head;	
	linklist p=head;
	while(p->next!=head)
		p=p->next;
	head=head->next;
	p->next=head;
	free(del);
	del=NULL;
	return head;
}

//尾删
linklist delete_rear(linklist head)
{
	if(head==NULL)
		return head;
	linklist p=head;
	if(head->next==head)
	{
		free(head);
		head=NULL;
		return head;
	}
	while(p->next->next!=head)
		p=p->next;
	linklist del=p->next;
	p->next=head;
	free(del);
	del=NULL;
	return head;
}

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

约瑟夫环:用循环链表编程实现约瑟夫问题
n个人围成一圈,从某人开始报数1,2,.., m,数到m的人出圈,然后从出圈的下一个人(m+1)开始重复此过程
直到 全部人出圈,于是得到一个出圈人员的新序列
如当n=8,m=4时,若从第一个位置数起,则所得到的新的序列 为4,8,5,2,1,3,7,6

3.编程实现单向循环链表的排序

相关推荐
好奇龙猫1 小时前
【大学院-筆記試験練習:线性代数和数据结构(10)】
数据结构·线性代数
不穿格子的程序员1 小时前
从零开始写算法——二叉树篇7:从前序与中序遍历序列构造二叉树 + 二叉树的最近公共祖先
数据结构·算法
曹仙逸2 小时前
数据结构day06小项目
数据结构
开开心心就好2 小时前
内存清理工具显示内存,优化释放自动清理
java·linux·开发语言·网络·数据结构·算法·电脑
CodeByV3 小时前
【算法题】队列&广度优先搜索
数据结构·算法
学嵌入式的小杨同学3 小时前
【嵌入式 C 语言实战】手动实现字符串四大核心函数(strcpy/strcat/strlen/strcmp)
c语言·开发语言·前端·javascript·数据结构·数据库·算法
人工干智能3 小时前
Pandas核心数据结构:Series与DataFrame
数据结构·python·pandas
Yolo_TvT4 小时前
数据结构:初识“树”
数据结构
信奥卷王4 小时前
2025年9月GESPC++四级真题解析(含视频)
数据结构·c++·算法
jimy14 小时前
创建链表注意项(三):使用内存池减少malloc开销(用标志位或空闲链表)
数据结构·链表