本题重新定义队列出队的操作:队首出队的数字重新在队尾入队。
例:队列中有1 2 3三个数字,现要求队首出队,则1从队首出队,同时1从队尾入队,队列变成2 3 1。
入队的顺序为1,2,3,4.n,同时给一个二进制字符串,1代表出队操作,0代表入队操作。
输入格式:
在第一行有两个数字n,m(n<=100,n
接下来m行,每行一个数字,1或者0,代表不同的操作
输出格式:
输出操作后队列的每个数字,数字间以空格分隔,最后一个数字后没有空格
输入样例:
5 8
0
0
1
0
1
0
1
0
输出样例:
3 2 4 1 5
代码长度限制
16 KB
时间限制
2000 ms
内存限制
1 MB
#include
using namespace std;
typedef struct Qnode
{
int data;
Qnode *next;
}Qnode, *Queueptr;
typedef struct
{
Queueptr font;
Queueptr rear;
}Linkqueue;
void init ( Linkqueue &Q )
{
Q.font = Q.rear = new Qnode;
Q.font->next = NULL;
}
void push ( Linkqueue &Q, int e )
{
Queueptr p;
p = new Qnode;
p->next = NULL;
p->data = e;
Q.rear->next = p;
Q.rear = p;
}
int pop ( Linkqueue &Q )
{
Queueptr p;
p = Q.font->next;
int e;
e = p->data;
Q.font->next = p->next;
delete p;
return e;
}
void print ( Linkqueue Q )
{
while ( 1 )
{
if ( Q.font->next == NULL )
{
return;
}
else
{
cout << Q.font->next->data;
if ( Q.font->next->next != NULL )
{
cout << " ";
}
}
Q.font->next = Q.font->next->next;
}
}
int main()
{
int n, m;
int a, t;
int e = 0;
cin >> n >> m;
Linkqueue Q;
init (Q);
for ( int i = 0; i < m; i ++ )
{
cin >> a;
if ( a == 0 )
{
e = e + 1;
push (Q, e);
}
else
{
t = pop (Q);
push (Q, t);
}
}
print (Q);
return 0;
}