您的位置:首页 > 理论基础 > 数据结构算法

SDUT 2135 数据结构实验之队列一:排队买饭

2015-01-21 19:49 381 查看

数据结构实验之队列一:排队买饭


Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^

数据结构实验之队列一:排队买饭


Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^

题目描述

中午买饭的人特多,食堂真是太拥挤了,买个饭费劲,理工大的小孩还是很聪明的,直接奔政通超市,哈哈,确实,政通超市里面也卖饭,有好几种菜,做的比食堂好吃多了,价格也不比食堂贵,并且买菜就送豆浆,吸引了不少童鞋。所以有时吧,人还是很多的,排队是免不了的,悲剧的是超市只有两个收银窗口。
问题是这样的:开始有两队人在排队,现在咱们只研究第一队,现在我们给每个人一个编号,保证编号各不相同,排在前面的人买完饭就走了,有些人挑完饭就排在后面等待付款,还有一些人比较聪明,看到另一个队人比较少,直接离开这个队到另一个队去了。我要问的是队的总人数和某个位置上人的编号。

输入

首先输入一个整数m(m<10000),代表当前有m个人,第二行输入m个数,代表每个人的编号,第三行输入一个整数n(n<10000),代表队列变动和询问一共n次,以后n行,JOIN X表示编号为X(保证与以前的编号不同)的人加入;LEAVE Y表示第Y(Y小于当前队列长度)个位置 上的人离队 ;ASK Z(Z小于当前队列长度)表示询问第Z个位置上的人的编号;FINISH D表示有D个人买完饭离开了;LENGTH表示询问队列的长度 。保证所有数据在int 范围内.

输出

对每个询问输出相应的答案,每个答案占一行。


示例输入

3
1 2 3
6
JOIN 4
ASK 2
LEAVE 2
LENGTH
FINISH 2
LENGTH



示例输出

2
3
1



提示

队列的练习,没什么好说了,建立队列的时候注意好细节就行了。。。代码:

#include <iostream>
#include <cstdlib>

using namespace std;

typedef int anytype;

struct queues
{
int cont;
struct node	//链式
{
anytype data;
struct node *next;
}*head,*tail;
queues()	//构造函数
{
head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
tail=head;
cont=0;
}
bool empty()	//判断空队
{
if(cont)
return false;
return true;
}
void push(anytype n)	//入队
{
struct node *p=(struct node *)malloc(sizeof(struct node));
p->data=n;
p->next=NULL;
tail->next=p;
tail=p;
cont++;
}
void pop()	//出队
{
struct node *p=head->next;
head->next=p->next;
free(p);
cont--;
}
void leave(int n)	//任意位置出队
{
struct node *p=head->next,*q=head;
if(n<=cont)
{
for(int i=1;i<n;i++)
{
q=p;
p=p->next;
}
q->next=p->next;
free(p);
cont--;
}
}
anytype front()	//查询队头
{
return head->next->data;
}
anytype back()	//查询队尾
{
return tail->data;
}
anytype ask(int n)	//查询任意位置
{
struct node *p=head;
if(n<=cont)
{
for(int i=0;i<n;i++)
p=p->next;
return p->data;
}
return 0;
}
int count()	//返回对内元素数量
{
return cont;
}
};
int main()
{
ios::sync_with_stdio(false);
queues s;
int n,num;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>num;
s.push(num);
}
cin>>n;
while(n--)	//按照指令依次操作队列即可
{
string str;
cin>>str;
if(str=="JOIN")
{
cin>>num;
s.push(num);
}
else if(str=="LEAVE")
{
cin>>num;
s.leave(num);
}
else if(str=="ASK")
{
cin>>num;
cout<<s.ask(num)<<endl;
}
else if(str=="FINISH")
{
cin>>num;
while(num--)
s.pop();
}
else if(str=="LENGTH")
cout<<s.count()<<endl;
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: