2021-08-23

数据结构学习(C语言)–单链表的基本操作


#include<stdio.h>
#include<malloc.h>
//结构体声明
typedef struct Node {
	int data;   //数据域
	struct Node * pNext;   //指针域
}*pNode, Node;
//函数声明
pNode createList();	//创建链表,返回头结点指针
void ergodicList(pNode);//遍历链表中数据
bool isEmpty(pNode);//判断链表是否为空
int getListLength(pNode);//获取链表的长度
bool insertByIndex(pNode, int, int);//插入元素(在第几个位置后)
bool deleteByIndex(pNode, int, int*);//删除元素(删除第几个元素)
bool deleteByValue(pNode, int);//删除元素(删除值为多少的元素)
bool serchByIndex(pNode, int);//查找(查看第几个元素的值)
bool serchByValue(pNode, int);//查找(查看某值的元素所在位置(第一个))
void selectSort(pNode);//排序(对链表中元素进行选择排序)
pNode reverseOrder(pNode);//倒序输出所有元素

//主函数
int main(void) {
	pNode pHead = NULL;
	int val;
	int len;
	pHead = createList();//创建链表,并将头指针赋给pHead
	ergodicList(pHead);//遍历链表中数据
	isEmpty(pHead);//判断链表是否为空
	len = getListLength(pHead);//获取链表的长度
	printf_s("链表的长度为:%d\n", len);
	insertByIndex(pHead, 3, 7);//在第3个结点(数据)后插入数据7
	ergodicList(pHead);
	printf_s("此时链表长度为:%d\n", getListLength(pHead));
	deleteByIndex(pHead, 3, &val);//将第3个结点删除
	ergodicList(pHead);
	deleteByValue(pHead, 7);//将值为7的结点删除
	ergodicList(pHead);
	serchByIndex(pHead, 3);//查找第3个结点的值是多少
	serchByValue(pHead, 12);//查找第一个值为12的结点位置
	selectSort(pHead);//排序
	ergodicList(pHead);
	ergodicList(reverseOrder(pHead));//倒置并输出
	return 0;
}
//函数
pNode createList() {		//创建申请空间
	int len;
	int val;
	int i;
	pNode pHead;	//创建头结点
	pHead = (pNode)malloc(sizeof(Node));	//动态分配一个结点内存
	pNode pTail = pHead;	//用于挂在当前尾结点上
	pTail->pNext = NULL;
	printf_s("请输入要创建链表的长度:");
	scanf_s("%d", &len);
	for (i = 0; i < len; i++) {
		printf_s("请输入第%d个结点的值:", i + 1);
		scanf_s("%d", &val);
		pNode pNew = (pNode)malloc(sizeof(Node));
		pNew->data = val;
		pTail->pNext = pNew;
		pNew->pNext = NULL;
		pTail = pNew;
	}
	return pHead;
}
void ergodicList(pNode pHead) {		//遍历链表
	pNode p = pHead->pNext;
	printf_s("链表中元素如下:\n");
	while (p != NULL) {
		printf_s("%d	", p->data);
		p = p->pNext;
	}
	putchar('\n');
	return;
}
bool isEmpty(pNode pHead) {//判断链表是否为空
	if (pHead->pNext == NULL) {
		printf_s("链表为空!\n");
		return true;
	}
	else {
		printf_s("链表不为空!\n");
		return false;
	}
}
int getListLength(pNode pHead) {	//获取链表的长度
	pNode p = pHead->pNext;
	int len = 0;
	while (p != NULL) {
		len++;
		p = p->pNext;
	}
	return len;
}
bool insertByIndex(pNode pHead, int pos, int val) {		//插入数据
	pNode p = pHead;
	int i = 0;
	if (pos<1 || pos>getListLength(pHead)) {
		printf_s("您选择的插入位置不合法!\n");
		return false;
	}
	while (i < pos) {
		p = p->pNext;
		i++;
	}
	pNode pNew = (pNode)malloc(sizeof(Node));
	pNew->data = val;
	pNew->pNext = p->pNext;
	p->pNext = pNew;
	printf_s("已将新结点插入到第%d个结点之后!\n", pos);
	return true;
}
bool deleteByIndex(pNode pHead, int pos, int *val) {		//删除第几个数据
	int i = 0;
	pNode p = pHead;
	if (pos<1 || pos>getListLength(pHead)) {
		printf_s("您选择的删除位置不合法!\n");
		return false;
	}
	while (i < pos - 1) {
		p = p->pNext;
		i++;
	}
	pNode q = p->pNext;
	*val = q->data;
	p->pNext = q->pNext;
	free(q);//释放q所指向的空间,但q指针仍是存在的
	q->pNext = NULL;
	printf_s("已将第%d个值为%d结点删除!\n", pos, *val);
	return true;
}
bool deleteByValue(pNode pHead, int val) {	//删除某特定值的数据(结点)
	int pos = 0;
	int i = 0;
	pNode p = pHead;
	while (p->data != val) {//这里的结果是p将挂在要删除的结点下面
		p = p->pNext;
		pos++;
	}
	if (pos<1 || pos>getListLength(pHead)) {
		printf_s("没有您要删除的数据\n");
		return false;
	}
	p = pHead;//将p重新挂回头结点下
	while (i < pos - 1) {//定位到要删除结点的前一个结点
		p = p->pNext;
		i++;
	}
	pNode q = p->pNext;
	p->pNext = q->pNext;
	free(q);
	q->pNext = NULL;
	printf_s("已将值为%d的结点删除,其为第%d个结点\n", val, pos);
	return true;
}
bool serchByIndex(pNode pHead, int pos) {	//查找第几个结点的值是
	int i = 0;
	if (pos<1 || pos>getListLength(pHead)) {
		printf_s("查找位置不合法!\n");
		return false;
	}
	pNode p = pHead;
	for (i; i < pos; i++) {
		p = p->pNext;
	}
	printf_s("第%d个结点的值是:%d\n", pos, p->data);
	return true;
}
bool serchByValue(pNode pHead, int val) {		//查找第一个值为结点的位置
	int pos = 0;
	pNode p = pHead;
	while (p->data != val) {
		p = p->pNext;
		pos++;
	}
	if (pos<1 || pos>getListLength(pHead)) {
		printf_s("没有要查找的值!\n");
		return false;
	}
	printf_s("第一个值为%d的结点为第%d个结点!\n", val, pos);
	return true;
}
void selectSort(pNode pHead) {//选择排序
	pNode p;
	pNode q;
	int  t;
	int i, j;
	for (i = 1, p = pHead->pNext; i < getListLength(pHead); i++, p = p->pNext) {
		for (j = i + 1, q = p->pNext; j < getListLength(pHead) + 1; j++, q = q->pNext) {
			if (p->data > q->data) {
				t = p->data;
				p->data = q->data;
				q->data = t;
			}
		}
	}
	printf_s("已完成排序!\n");
	return;
}
pNode reverseOrder(pNode pHead) {//倒置整个链表
	if (pHead == NULL || pHead->pNext == NULL)
		return pHead;
	pNode top = NULL;
	pNode mid = pHead->pNext;
	pNode end = mid->pNext;
	while (1) {
		mid->pNext = top;
		if (end == NULL) {
			break;
		}
		top = mid;
		mid = end;
		end = end->pNext;
	}
	pHead->pNext = mid;
	return pHead;
}

运行结果:
在这里插入图片描述
关于倒置(倒序输出)功能的说明:
在这里插入图片描述倒序输出函数的方法可以参考文章单链表反转详解(4种算法实现)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用 Java 中的 LocalDateTime 类来处理时间。 首先,我们需要将给定的时间段转换为 LocalDateTime 对象: ```java LocalDateTime start = LocalDateTime.of(2020, 11, 20, 0, 0, 0); LocalDateTime end = LocalDateTime.of(2021, 10, 9, 23, 59, 59); ``` 这里将结束时间设置为 23:59:59 是因为我们想要包含该日期的所有时间。 接下来,我们可以使用一个循环来遍历时间段中的每一天,并获取该天的开始时间和结束时间: ```java LocalDateTime current = start; while (!current.isAfter(end)) { LocalDateTime dayStart = current.withHour(0).withMinute(0).withSecond(0).withNano(0); LocalDateTime dayEnd = current.withHour(23).withMinute(59).withSecond(59).withNano(999999999); System.out.println("Day " + current.toLocalDate() + " starts at " + dayStart + " and ends at " + dayEnd); current = current.plusDays(1); } ``` 这里,我们使用 `withHour`、`withMinute`、`withSecond` 和 `withNano` 方法来设置每天的开始时间和结束时间。然后,我们可以使用 `plusDays` 方法来增加 `current` 对象的日期,以便遍历整个时间段。 完整代码如下: ```java import java.time.LocalDateTime; public class Main { public static void main(String[] args) { LocalDateTime start = LocalDateTime.of(2020, 11, 20, 0, 0, 0); LocalDateTime end = LocalDateTime.of(2021, 10, 9, 23, 59, 59); LocalDateTime current = start; while (!current.isAfter(end)) { LocalDateTime dayStart = current.withHour(0).withMinute(0).withSecond(0).withNano(0); LocalDateTime dayEnd = current.withHour(23).withMinute(59).withSecond(59).withNano(999999999); System.out.println("Day " + current.toLocalDate() + " starts at " + dayStart + " and ends at " + dayEnd); current = current.plusDays(1); } } } ``` 输出结果如下: ``` Day 2020-11-20 starts at 2020-11-20T00:00 and ends at 2020-11-20T23:59:59.999999999 Day 2020-11-21 starts at 2020-11-21T00:00 and ends at 2020-11-21T23:59:59.999999999 Day 2020-11-22 starts at 2020-11-22T00:00 and ends at 2020-11-22T23:59:59.999999999 ... Day 2021-10-07 starts at 2021-10-07T00:00 and ends at 2021-10-07T23:59:59.999999999 Day 2021-10-08 starts at 2021-10-08T00:00 and ends at 2021-10-08T23:59:59.999999999 Day 2021-10-09 starts at 2021-10-09T00:00 and ends at 2021-10-09T23:59:59.999999999 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Evekkan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值