今天对栈 队列 链表进行重温加固
首先是数组模拟栈
验证栈序列
题目描述
给出两个序列 pushed 和 poped 两个序列,其取值从 1 到 n(n\le100000)n(n≤100000)。已知入栈序列是 pushed,如果出栈序列有可能是 poped,则输出 Yes
,否则输出 No
。为了防止骗分,每个测试点有多组数据。
输入格式
第一行一个整数 qq,询问次数。
接下来 qq 个询问,对于每个询问:
第一行一个整数 nn 表示序列长度;
第二行 nn 个整数表示入栈序列;
第三行 nn 个整数表示出栈序列;
输出格式
对于每个询问输出答案。
输入输出样例
输入 #1复制
2 5 1 2 3 4 5 5 4 3 2 1 4 1 2 3 4 2 4 1 3
输出 #1复制
Yes No
栈的基本操作主要有:栈的初始化、判空、判满、取栈顶元素、在栈顶进行插入和删除。在栈顶插入元素称为入栈,在栈顶删除元素称为出栈。根据这些来对该题进行解决。
#include<stdio.h>
int main()
{
int push[100000],pop[100000],s[100000];
int q,flag,a,b,c;
scanf("%d",&q);
for(int i=1;i<=q;i++)
{
int n;
scanf("%d",&n);
for(int j=0;j<n;j++)
{
scanf("%d",&push[j]);
}
for(int j=0;j<n;j++)
{
scanf("%d",&pop[j]);
}
flag=1;
a=b=c=0;
while(a<n)
{
s[b]=push[c];
while(s[b]==pop[a]&&a!=n&&b>=0)
{
--b;
++a;
}
++b;
++c;
if(c==n&&a!=n)
{
flag=0;
break;
}
}
if(flag==1)
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
return 0;
}
以下是对于队列原理的进行刨析
#include<stdio.h>
int main()
{
int q[102]={0,6,3,1,7,5,8,9,2,4},head,tail;
//初始化队列
head=1;
tail=10;
while(head<tail)//当队列不为空的时候执行循环
{
//打印队首并将队首出队
printf("%d ",q[head]);
head++;
//先将新队首的数添加到队尾
q[tail]=q[head];
tail++;
//再将队首出队
head++;
}
getchar();
getchar();
return 0;
}
以下是加上结构体的队列,就有了广度搜索的雏形
#include<stdio.h>
struct queue
{
int data[100];
int head;
int tail;
};
int main()
{
struct queue q;
int i;
q.head=1;
q.tail=1;
for(i=1;i<=9;i++)
{
scanf("%d",&q.data[q.tail]);
q.tail++;
}
while(q.head<q.tail)
{
printf("%d ",q.data[q.head]);
q.head++;
q.data[q.tail]=q.data[q.head];
q.tail++;
q.head++;
}
getchar();
getchar();
return 0;
}
接下来是一个比较基础的队列题目
小猫钓鱼 (纸牌游戏)
代码实现
#include<stdio.h>
struct queue
{
int data[1000];
int tail;
int head;
};
struct stack
{
int data[10];
int top;
};
int main()
{
struct queue q1,q2;
struct stack s;
int book[10];
int i,t,lun=0;
q1.head=1;q1.tail=1;
q2.head=1;q2.tail=1;
s.top=0;
for(i=1;i<=9;i++)
{
book[i]=0;
}
for(i=1;i<=6;i++)
{
scanf("%d",&q1.data[q1.tail]);
q1.tail++;
}
for(i=1;i<=6;i++)
{
scanf("%d",&q2.data[q2.tail]);
q2.tail++;
}
while(q1.head<q1.tail&&q2.head<q2.tail)
{
t=q1.data[q1.head];
if(book[t]==0)
{
q1.head++;
book[t]=1;
s.top++;
s.data[s.top]=t;
}
else
{
q1.head++;
q1.data[q1.tail]=t;
q1.tail++;
while(s.data[s.top]!=t)
{
book[s.data[s.top]]=0;
q1.data[q1.tail]=s.data[s.top];
q1.tail++;
s.top--;
}
}
t=q2.data[q2.head];
if(book[t]==0)
{
q2.head++;
book[t]=1;
s.top++;
s.data[s.top]=t;
}
else
{
q2.head++;
q2.data[q2.tail]=t;
q2.tail++;
while(s.data[s.top]!=t)
{
book[s.data[s.top]]=0;
q2.data[q2.tail]=s.data[s.top];
q2.tail++;
s.top--;
}
}
lun++;
}
if(q2.head==q2.tail)
{
printf("dcy,win!\n");
printf("dcy当前手上的牌是:");
for(i=q1.head;i<q1.tail;i++)
{
printf(" %d",q1.data[i]);
}
if(s.top>0)
{
printf("\n桌上的牌是:");
for(i=1;i<=s.top;i++)
{
printf(" %d",s.data[i]);
}
}
else
{
printf("\n桌子上没有牌了");
}
}
else
{
printf("dyy,win!\n");
printf("dyy当前手上的牌是:");
for(i=q2.head;i<q2.tail;i++)
{
printf(" %d",q2.data[i]);
}
if(s.top>0)
{
printf("\n桌上的牌是:");
for(i=1;i<=s.top;i++)
{
printf(" %d",s.data[i]);
}
}
else
{
printf("\n桌子上没有牌了");
}
}
printf("%d",lun);
getchar();
getchar();
return 0;
}
该程序使用队列来实现玩家的手中的牌(玩家的牌只能前面出牌,赢得牌依次放后面),用栈实现桌子上的牌(出牌放在末端,赢牌也是从末端拿走)。
通过对栈,队列的重温,对于深度搜索有了进一步的理解