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

来源:互联网 发布:替代 usleep windows 编辑:程序博客网 时间:2024/06/11 01:09

题目描述

中午买饭的人特多,食堂真是太拥挤了,买个饭费劲,理工大的小孩还是很聪明的,直接奔政通超市,哈哈,确实,政通超市里面也卖饭,有好几种菜,做的比食堂好吃多了,价格也不比食堂贵,并且买菜就送豆浆,吸引了不少童鞋。所以有时吧,人还是很多的,排队是免不了的,悲剧的是超市只有两个收银窗口。

问题是这样的:开始有两队人在排队,现在咱们只研究第一队,现在我们给每个人一个编号,保证编号各不相同,排在前面的人买完饭就走了,有些人挑完饭就排在后面等待付款,还有一些人比较聪明,看到另一个队人比较少,直接离开这个队到另一个队去了。我要问的是队的总人数和某个位置上人的编号。



输入

首先输入一个整数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 <stdio.h>
#include <stdlib.h>
#include<string.h>
typedef int QElemType;
typedef int Status;
typedef struct QNode
{
    QElemType data;
    QNode *next;
}*Queueptr;
typedef struct
{
    Queueptr front;
    Queueptr rear;
}LinkQueue;
Status InitQueue(LinkQueue &q)//初始化队
{
    q.front=q.rear=(Queueptr)malloc(sizeof(QNode));
    if(!q.front) exit(0);
    q.front->next=NULL;
    return 1;
}
Status EnQueuer(LinkQueue &q,QElemType &e)//进队;
{
    Queueptr p;
    p=(Queueptr)malloc(sizeof(QNode));
    if(!p) exit(0);
    p->data=e;
    p->next=NULL;
    q.rear->next=p;
    q.rear=p;
    return 1;
}
Status DeQueuer(LinkQueue &q)//出队;
{
    Queueptr p;
      if(q.front==q.rear)
      return 0;
      p=q.front->next;
      q.front->next=p->next;
      if(q.rear==p)
      q.rear=q.front;
      free(p);
      return 1;
}
int LinkQueuelength(LinkQueue q)//队的长度;
{
    Queueptr p;
    p=q.front;
    int i=0;
    while(p!=q.rear)
    {
        i++;
        p=p->next;
    }
    return i;
}
int main()
{
    int m,i,n,num;
    char c[7];
    LinkQueue q;
    InitQueue(q);
    scanf("%d",&m);
    for(i=0;i<m;i++)
    {
        scanf("%d",&num);
        EnQueuer(q,num);
    }
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        scanf("%s",c);
        if(strcmp(c,"JOIN")==0)
        {
            scanf("%d",&num);
            EnQueuer(q,num);
        }
        if(strcmp(c,"ASK")==0)//问第几个元素的编号;
        {
            scanf("%d",&num);
            Queueptr p;
            p=q.front;//指向队头;
            while(num--)
            p=p->next;
            printf("%d\n",p->data);
        }
        if(strcmp(c,"LEAVE")==0)
        {
              scanf("%d",&num);
              int t=num-1;
              Queueptr p=q.front;//两指针将对中第几个人删除;
              Queueptr h=p->next;
              while(t--)
              {
                  p=p->next;
                  h=h->next;
              }
              p->next=h->next;//相当于链表元素的删除;
        }
        if(strcmp(c,"FINISH")==0)
        {
            scanf("%d",&num);
            while(num--)
            DeQueuer(q);
        }
        if(strcmp(c,"LENGTH")==0)
        {
            printf("%d\n",LinkQueuelength(q));
        }
    }
    return 0;
}
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int main()
{
    int i,n,m,x,j,l,a[20000]={0};
    char str[10];
    scanf("%d",&n);
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    scanf("%d",&m);
    for(i=0;i<m;i++)
    {
        scanf("%s",str);
        if(str[0]=='J')
        {
            scanf("%d",&x);
            a[n++]=x;
        }
        else if(str[0]=='L')
        {
            if(str[2]=='A')
            {
                scanf("%d",&x);
                for(l=x-1;l<n;l++)
                    a[l]=a[l+1];
                n--;
            }
            else
            {
                printf("%d\n",n);
            }
        }
        else if(str[0]=='A')
        {
            scanf("%d",&x);
            printf("%d\n",a[x-1]);
        }
        else if(str[0]=='F')
        {
            scanf("%d",&x);
            if(x>=n)n=0;
            else
            {
                for(j=0;j<n-x;j++)
                a[j]=a[j+x];
                n=n-x;
            }


        }
    }
    return 0;
}*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int main()
{
    int i,n,m,x,j,l,a[100000]={0};
    char str[10];
    scanf("%d",&n);
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    scanf("%d",&m);
    for(i=0;i<m;i++)
    {
        scanf("%s",str);
        if(strcmp(str,"JOIN")==0)
        {
            scanf("%d",&x);
            a[n++]=x;
        }
        else if(strcmp(str,"LEAVE")==0)
        {
                scanf("%d",&x);
                for(l=x-1;l<n;l++)
                    a[l]=a[l+1];
                n--;
        }
        else if(strcmp(str,"LENGTH")==0)
            {
                printf("%d\n",n);
            }
        else if(strcmp(str,"ASK")==0)
        {
            scanf("%d",&x);
            printf("%d\n",a[x-1]);
        }
        else
           // if(strcmp(str,"FINISH")==0)
        {
            scanf("%d",&x);
            if(x>=n)n=0;
            else
            {
                for(j=0;j<n-x;j++)
                a[j]=a[j+x];
                n=n-x;
            }


        }
    }
    return 0;
}

0 0