栈和队列的基本操作

文章目录​​​

1.栈

2.队列

 一、简单介绍栈和队列,链表

栈:一种后进先出(像把球放桶里又把球取出来的操作)的数据结构,栈只能指定一边进行插入和删除操作。

对列:一种先进先出的数据操作,对列可以指定开头进行删除操作。left和right两端。

二、题目练习

1.

现在有n个元素分别是1,2,3,...,n,我们想知道通过一个栈,在n次push/pop后,出栈序列可能是什么样的。例如n是5,那么入栈次序就是1,2,3,4,5,如果我们希望出栈次序同样是1,2,3,4,5,那只要每push一个数,就立即pop一个数。如果我们希望出栈次序是3,2,4,5,1,那么我们先让1,2,3入栈,然后pop出来3和2,接着4入栈后马上pop,再就是5入栈后马上pop,最后再把栈里的1pop出。再例如,如果我们希望出栈次序是5,4,1,2,3,这是办不到的,如果要让5最先出栈,那么出栈次序只可能是5,4,3,2,1

Input

输入由多块输入构成,每块的第一行是n,(1≤n≤1000),接着是多组测试数据,每组一行,每行由n个1到n的整数组成,直到某一行第一个数是0表示此输入块结束,然后是下一块数据。
当某一块的第一行的n是0的时候结束所有输入

Output

对每组数据,只要输出Yes或No,Yes表示能产生那样的出栈序列,No表示不能。 输入块与输入块之间要多输出一个空行分隔。注意对于最后一个n为0的输入块不要多输出空行。

Sample Input

5
1 2 3 4 5
5 4 1 2 3
0
6
6 5 4 3 2 1
0
0

Sample Output

Yes
No

Yes

代码如下

#include <stdio.h>
int main()
{
    int n;
    while (~scanf("%d", &n) && n != 0)
    {
        int a[1010] = {0};
        int b[1010] = {0};
        int i;
        int k;
        int top;
        while (~scanf("%d", &a[1]))
        {
            if (a[1] == 0)
            {
                break;
            }
            k = 1;
            top = 1;
            for (i = 2; i <= n; i++)
            {
                scanf("%d", &a[i]);
            }
            for (i = 1; i <= n; i++)
            {
                b[top] = i;
                while (b[top] == a[k] && top > 0)
                {
                    top--;
                    k++;
                }
                top++;
            }
            if (k == n + 1)
                printf("Yes\n");
            else
                printf("No\n");
        }
        printf("\n");
    }
    return 0;
}

 2.

You are given a string consisting of parentheses ( ) and [ ]. A string of this type is said to be correct:

  • if it is the empty string
  • if A and B are correct, AB is correct,
  • if A is correct, (A) and [A] is correct.

Write a program that takes a sequence of strings of this type and check their correctness. Your program can assume that the maximum string length is 128.

Input

The first line contains the number of test cases n. Each of the next n lines contains the string of parentheses ( ) and [ ].

Output

For each test case print in a separate line "Yes" if the expression is correct or "No" otherwise.

Example 1

Input example #1 content_copy

3
([])
(([()])))
([()[]()])()

Output example #1 content_copy

Yes
No
Yes

Example 2

Input example #10 content_copy

22
((()

())))
([)]

((([[[]]])])
([])
(([()])))
([()[]()])()
[(])
(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((())))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
(])
[)]
[)))]
[[)]
([])
(([()])))
([()[]()])()
([)]
(
()(
[()

Output example #10 content_copy

No
Yes
No
No
Yes
No
Yes
No
Yes
No
Yes
No
No
No
No
Yes
No
Yes
No
No
No
No

代码如下

#include"stdio.h"
#include"string.h"
int main()
{
	char s[129],a[129];
	int n,m,t,top,sign;
	scanf("%d",&n);
	getchar();
	for(m=0;m<n;m++)
	{
		gets(s);
		top=0;sign=0;
		if(strlen(s)==0)
		printf("Yes\n");
		else if(strlen(s)%2!=0)
		printf("No\n");
		else if(strlen(s)%2==0)
		{
			for(t=0;t<strlen(s);t++)
			{
				if(s[t]=='('||s[t]=='[')
					a[++top]=s[t];
				else if((s[t]==']')||(s[t]==')'))
				{
				if(top==0)
				{
					sign=1;
					break;
				}
				else if(top!=0)
				{
					if((s[t]==')'&&a[top]=='(')||(s[t]==']'&&a[top]=='['))
					--top;
					else if((s[t]==']'&&a[top]=='(')||(s[t]==']'&&a[top]=='('))
					{
						sign=1;
						break;
					}
				}
				}
			}
			if((sign==1)||(top!=0))//配对完成后,栈中还有字符,或者s中还有字符 ,或者有字符不匹配 
			printf("No\n");
			else 
			printf("Yes\n"); 
		} 
	}
	return 0;
}

3.

宇神完成了栈的实验要求后,他又很是开心,刚要出去五排, 菌菌子突然问道老师让做的队列的那个实验你写完了么,宇神顿时大呼悲哉。。。。他给忘记了,怎么办。。明天就要上交实验报告了,你能帮他搞定么???

你需要完成三种操作1.enqueue x,将元素x插入队尾。2.dequeue,若队列非空,则删去队头元素,并输出该元素。3.query,从队头开始删除所有元素,并输出。

Input

本题有多组测试数据,每组数据首先输入一个T,接下来T行是T种对队列的操作。  (0< T < 100,0< x <= 500)

Output

每次执行dequeue操作时删除队头元素输出并换行,如果队列为空输出“this is empty!”并换行。

每次执行query操作时删除所有元素队列内所有元素并输出,每个元素占一行,如果栈为空输出“this is empty!”并换行。

每组数据后有一个空行。

Sample Input

10
enqueue 1
enqueue 2
enqueue 3
enqueue 4
query
dequeue
enqueue 1
dequeue
query
dequeue

Sample Output

1
2
3
4
this is empty!
1
this is empty!
this is empty!

代码如下

#include"stdio.h"
#include"string.h"
int main()
{
	int n;
	while(~scanf("%d",&n))
	{
	getchar();
	int m,b[100];
	int left=0,right=0,a=0,t,i;
	char s[50];
	for(m=0;m<n;m++)
	{
	gets(s);
	if(s[0]=='e')
	{
		if(strlen(s)==11)//求出最后的数字值 
		a=(s[8]-'0')*100+(s[9]-'0')*10+(s[10]-'0');
		else if(strlen(s)==10)
		a=(s[8]-'0')*10+(s[9]-'0');
		else if(strlen(s)==9)
		a=(s[8]-'0'); 
		b[right++]=a;//将这个数字移向数组的最后
	}
	else if(s[0]=='d')
	{
		if(left<right)
		{
		i=b[left];
		left++;//删除队首字符 	
		printf("%d\n",i); 
		}
		else if(left>=right)
		printf("this is empty!\n");
	}	
	else if(s[0]=='q')
	{
		if(left<right)
		{
			while(left<right)
			{
			i=b[left];
			left++;//删除队首字符 	
			printf("%d\n",i); 
			}
		}
		else if(left>=right)
		printf("this is empty!\n");
	}
	}
	printf("\n");
	}
	return 0;
}

4.

冰冰子最近新学习了队列和栈两种重要的数据结构,他知道它们具有push 和pop操作。

而冰冰子现在想同时研究栈和队列,于是,他开始了一个实验。

现在,假设队列和栈都是空的。给定一系列push k和pop操作之后,冰冰子想知道队列和栈中分别存的数字。若队列或栈已经空了,仍然接收到pop操作,则输出error。

Input

第一行为m,表示有m组测试输入,m<100。
每组第一行为n,表示下列有n行push k或pop操作。(n<150)
接下来n行,每行是push k或者pop,其中k是一个整数。
(输入保证同时在队列或栈中的数不会超过100个)

Output

对每组测试数据输出两行,第一行是队列情况,若在队列空时收到pop操作,则输出error。其余情况将队列按照对头至队尾顺序输出队列中所有元素,中间用空格隔开。第二行是栈的情况,若在栈空时收到pop操作,则输出error。其余情况下按照栈底至栈顶的顺序输出栈中所有元素。

Sample Input

2
4
push 1
push 3
pop
push 5
1
pop

Sample Output

3 5
1 5
error
error

代码如下

#include"stdio.h"
int main()
{
	char a[101];
	int n,m,i,j,num,b[100],c[100],t;
	scanf("%d",&n);
	for(m=0;m<n;m++)
	{
		scanf("%d",&i);
		int left=0,right=0,top=0,sign=0,sign1=0;
		for(j=0;j<i;j++)
		{
			scanf("%s",a);
			if(a[1]=='u')//push 
			{
				scanf("%d",&num);
				b[right++]=num;
				c[top++]=num;
			}
			else //pop
			{
				if(left>=right)
				{
				sign1=1;
				}
				else if(left<right)
				{
					left++;
				}
				if(top<=0)
				{
				sign=1;
				}
				else if(top>0)
				{
					top--;
				}
			}
		}
		if(sign==0)
			{
			for(t=left;t<right;t++)
			printf("%d ",b[t]);
			printf("\n");
			for(t=0;t<top;t++)
			printf("%d ",c[t]);
			printf("\n");
			}
		if(sign1==1)
			printf("error\n");
		if(sign==1)
			printf("error\n");
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值