算法2:队列、栈、链表

本文介绍了数据结构中的队列、栈和链表。通过实例展示了队列在解密QQ号问题上的应用,栈用于判断字符串是否为回文,以及链表的操作,包括指针的概念和数组模拟链表的实现。这些基础知识在程序设计中扮演着重要角色。
摘要由CSDN通过智能技术生成

·

一、队列

解密QQ号

小哈给小哼一串加密后的数字,并告知解密规则:首先将第一个数删除,紧接着将第二个数放到这串数的末尾,再将第三个数删除并将第四个数放到这串数的末尾,再将第五个数删除……直到剩下最后一个数,将最后一个数也删除。按照刚才删除的顺序,把这些删除的数连在一起就是小哈的QQ号。

#include<stdio.h>
#include<stdlib.h>
int main()
{
	int q[100], n, i, head, tail;
	printf("Please enter the number of the numbers : "); //接下来将输入n个数
	scanf_s("%d", &n);
	printf("Please enter the number you want : ");
	for (i = 0; i < n; i++) //循环读入n个数到队列
	{
		scanf_s("%d", &q[i]);
	}
	head = 0; //队首
	tail = n ; //队尾的下一个位置(避免队首队尾重合)
	while (head < tail) //当队列不为空的时候执行循环
	{
		//打印队首并将队首出队
		printf("%d ", q[head]);
		head++;
		//将新队首的数添加到队尾
		q[tail] = q[head];
		tail++;
		//再将队首出队
		head++;
	}
	system("pause");
	return 0;
}

运行结果:
Please enter the number of the numbers : 9
Please enter the number you want : 6 3 1 7 5 8 9 2 4
6 1 5 9 4 7 2 8 3 请按任意键继续. . .

队列是先进先出,首部出队,尾部入队。
·
·
·

二、栈

判断字符串是否为回文

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
	char a[101], s[101];
	int i, len, mid, next, top;
	printf("Please enter the
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值