双向队列
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
想想双向链表……双向队列的定义差不多,也就是说一个队列的队尾同时也是队首;两头都可以做出队,入队的操作。
现在给你一系列的操作,请输出最后队列的状态;
命令格式:
LIN X X表示一个整数,命令代表左边进队操作;
RIN X 表示右边进队操作;
ROUT
LOUT 表示出队操作;
Input
第一行包含一个整数M(M<=10000),表示有M个操作;
以下M行每行包含一条命令;
命令可能不合法,对于不合法的命令,请在输出中处理;
Output
输出的第一行包含队列进行了M次操作后的状态,从左往右输出,每两个之间用空格隔开;
以下若干行处理不合法的命令(如果存在);
对于不合法的命令,请输出一行X ERROR
其中X表示是第几条命令;
Sample Input
8 LIN 5 RIN 6 LIN 3 LOUT ROUT ROUT ROUT LIN 3
Sample Output
3 7 ERROR
Hint
Source
wanglin
本人较菜, 用链表解决的问题
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
struct node
{
int data;
struct node *next;
}*head, *p, *q, *t;
int main()
{
int n, i;
char st[5];
int a[10001];
int b = 0;
head = (struct node *)malloc(sizeof(struct node));
head -> next = NULL;
scanf("%d", &n);
int m = 0, j;
for(i = 1; i <= n; i++)
{
scanf("%s", st);
if(strcmp(st, "LIN") == 0)
{
p = (struct node *)malloc(sizeof(struct node));
p -> next = head -> next;
scanf("%d", &p -> data);
head -> next = p;
m++;
}
else if(strcmp(st, "RIN") == 0)
{
p = (struct node *)malloc(sizeof(struct node));
p -> next = NULL;
scanf("%d", &p -> data);
q = head;
for(j = 1; j <= m; j++)
q = q -> next;
q -> next = p;
m++;
}
else if(strcmp(st, "LOUT") == 0)
{
if(m == 0)
{
a[b] = i;
b++;
}
else
{
p = head -> next;
head -> next = p -> next;
m--;
}
}
else
{
if(m == 0)
{
a[b] = i;
b++;
}
else
{
p = head;
for(j = 1; j < m; j++)
{
p = p -> next;
}
p -> next = NULL;
m--;
}
}
}
p = head -> next;
while(p)
{
if(p -> next != NULL)
printf("%d ", p -> data);
else
printf("%d\n", p -> data);
p = p -> next;
}
for(i = 0; i < b; i++)
printf("%d ERROR\n", a[i]);
return 0;
}