Problem1823--【栈和队列】3-15 基于两端操作的循环队列的实现

1823: 【栈和队列】3-15 基于两端操作的循环队列的实现

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 1  Solved: 1
[Submit] [Status] [Web Board] [Creator:]

Description

如果允许在循环队列的两端都可以进行插入和删除操作。构造一个循环队列,实现从队头入队,从队尾出队并输出。约定从队头入队时向下标小的方向发展,从队尾入队时则向下标大的方向发展。

Input

多组数据,每组数据有两行。第一行为一个整数n,n表示入队序列A的长度(n个数依次连续入队,中间没有出队的情况),第二行为序列A(空格分隔的n个整数)。当n等于0时,输入结束。

Output

对应每组数据输出一行。依次输出队列中所有的整数,每两个整数之间用空格分隔。

Sample Input Copy

5
1 2 3 4 5
2
1 4
0

Sample Output Copy

1 2 3 4 5
1 4

HINT

#include<iostream>
using namespace std;
#define MAXSIZE 100
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef struct
{
    int *base;
    int front;
    int rear;
}SqQueue;
int InitQueue(SqQueue &Q)
{//构造一个空队列Q
    Q.base=new int[MAXSIZE];          //为队列分配一个最大容量为MAXSIZE的数组空间
    if(!Q.base) return OVERFLOW;            //存储分配失败
    Q.front=Q.rear=0;                  //头尾指针置零,队列为空
    return OK;
}
int EnQueue(SqQueue& Q,int e)
{//在Q的队头插入新元素e
/**************begin************/

    /**************end************/
}
int DeQueue(SqQueue &Q)
{//删除Q的队尾元素,用e返回其值
/**************begin************/


    /**************end************/
}
int main()
{
    int n;
    while(cin>>n&&n!=0)
    {
        SqQueue Q;
        InitQueue(Q);
        for(int i=0;i<n;i++)
        {
            int x;cin>>x;
            EnQueue(Q,x);
        }
        for(int i=0;i<n-1;i++)
            cout<<DeQueue(Q)<<" ";
        cout<<DeQueue(Q)<<endl;
    }
    return 0;
}

Source/Category