栈与队列的简单理解(ACboy needs your help again!)

一、队列:
队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。队列中没有元素时,称为空队列。
二、栈:
栈(stack)又名堆栈,它是一种运算受限的线性表。其限制是仅允许在表的一端进行插入和删除运算。这一端被称为栈顶,相对地,把另一端称为栈底。向一个栈插入新元素又称作进栈、入栈或压栈,它是把新元素放到栈顶元素的上面,使之成为新的栈顶元素;从一个栈删除元素又称作出栈或退栈,它是把栈顶元素删除掉,使其相邻的元素成为新的栈顶元素。

三、对于栈和队列,我们简单理解就是队列是先进先出,栈是先进后出。

我们在生活中买票排队时类似,这种情况就是一个队列。排在前面可以先买先走,排在后面的只能等前面的买完后再买,后买后走。也就是我们称为的FIFO(Fist in Fist out)

运用队列操作的时候,简单的情况下我们可以将其视为一个有顺序的数组。我们看重的是队列的思想,而不是具体的操作方法。

同样的,对于栈而言,生活中也有好多实例。例如我们喜欢吃的桶装薯片(如果我们想要吃掉最后一片,就必须把上面的全吃完),满满的羽毛球桶(如果想用最后的球,就必须把上面的球打坏,虽然这样够残暴的 …), 还有各种枪械的弹夹…
我们在实现栈的操作时,需要一个数组和一个指向栈顶的变量即可。

这两个玄学名词听起来好厉害的样子,但是说白了,我们在简单运用队列和栈时,它们就是一维数组而已。(我也想指着他们的鼻子大声呵斥:猪鼻子插大葱,装象!!!)

四、这样说概念我也觉得没意思,来吧,直接上实例:
1、

ACboy needs your help again!
ACboy was kidnapped!!
he miss his mother very much and is very scare now.You can’t image how dark the room he was put into is, so poor ?.
As a smart ACMer, you want to get ACboy out of the monster’s labyrinth.But when you arrive at the gate of the maze, the monste say :" I have heard that you are very clever, but if can’t solve my problems, you will die with ACboy."
The problems of the monster is shown on the wall:
Each problem’s first line is a integer N(the number of commands), and a word “FIFO” or “FILO”.(you are very happy because you know “FIFO” stands for “First In First Out”, and “FILO” means “First In Last Out”).
and the following N lines, each line is “IN M” or “OUT”, (M represent a integer).
and the answer of a problem is a passowrd of a door, so if you want to rescue ACboy, answer the problem carefully!
Input
The input contains multiple test cases.
The first line has one integer,represent the number oftest cases.
And the input of each subproblem are described above
Output
For each command “OUT”, you should output a integer depend on the word is “FIFO” or “FILO”, or a word “None” if you don’t have any integer.
Sample Input
4
4 FIFO
IN 1
IN 2
OUT
OUT
4 FILO
IN 1
IN 2
OUT
OUT
5 FIFO
IN 1
IN 2
OUT
OUT
OUT
5 FILO
IN 1
IN 2
OUT
IN 3
OUT
Sample Output
1
2
2
1
1
2
None
2
3

附上链接:https://cn.vjudge.net/contest/280108#problem/L
这是我们寒假训练,看到这些英文,是不是感到头皮发麻,直接不想看的节奏,没错,我就是这样,弄英语来刁难我们,这也没谁了。好吧… 说这些没用,还不得看题。
我们大体扫描一遍,可能云里雾里,不知道啥意思,但是我们看到了FIFO和FILO的字样,其实这就是个队列和栈的简单应用,看到大佬们用C++的个种方法,心里很是羡慕,但奈何没怎么学 …
只好用数组来简单模拟了
话不多说,上代码:

#include<stdio.h>
#include<string.h>
int a[10001];
int main()
{
	int i,n,step,j,num,m,k;
	scanf("%d",&n);
	while(n--)
	{
		char brr[11]; 
		scanf("%d%s",&step,brr);//输入指令和步骤; 
		if(strcmp(brr,"FIFO")==0)//先进先出 (队列)
		{
			k=m=0;//从栈底开始存储数据; 
			while(step--)
			{
				scanf("%s",brr);//放入数据; 
				if(strcmp(brr,"IN")==0)
				{
					scanf("%d",&num);
					a[k++]=num;
				}
				else if(strcmp(brr,"OUT")==0)//拿出数据; 
				{
					if(m>=k) printf("None\n");//(已经超过了范围)
					else printf("%d\n",a[m++]); 
				}
			}
		}
		else if(strcmp(brr,"FILO")==0)//先进后出;(栈)
		{
			k=0;
			while(step--)
			{
				scanf("%s",brr);
				if(strcmp(brr,"IN")==0)
				{
					scanf("%d",&num);
					a[k++]=num;
				}
				else if(strcmp(brr,"OUT")==0)
				{
					if(k==0) printf("None\n");//(数据已经没有l)
					else printf("%d\n",a[--k]);
				}
			}
		}
	}
	return 0;
}

自我感觉还是挺清晰的,不过主要还是队列的先进先出和栈的后进后出的理解。

队列的起源很早很早,数学的应用中就有了它的影子,还是数学家们厉害!!!

栈最早由艾伦 图灵于1994年提出,当时是为了解决程序的调用与返回问题,果然大神还是大神!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值