记录编号 |
116761 |
评测结果 |
AAAAAAAAAA |
题目名称 |
[USACO Open09] 奶牛队列 |
最终得分 |
100 |
用户昵称 |
Bokjan |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
0.080 s |
提交时间 |
2014-08-26 21:29:01 |
内存使用 |
0.31 MiB |
显示代码纯文本
#include <cstdio>
FILE *fi, *fo;
struct node{
int data;
node *prev, *next;
node(int value)
{
data = value;
prev = next = NULL;
}
};
class linklist{
public:
node *head, *tail;
int size;
linklist()
{
head = tail = NULL;
size = 0;
}
~linklist();
void AddLeft(int);
void AddRight(int);
void DelLeft(int);
void DelRight(int);
void Traverse(void);
};
linklist::~linklist()
{
node *p = head;
while(NULL != head){
head = head->next;
delete p;
p = head;
}
}
void linklist::AddLeft(int value)
{
node *p = new node(value);
p->next = head;
if(0 != size)
head->prev = p;
head = p;
if(0 == size)
tail = head;
size++;
}
void linklist::AddRight(int value)
{
node *p = new node(value);
p->prev = tail;
if(0 != size)
tail->next = p;
tail = p;
if(0 == size)
head = tail;
size++;
}
void linklist::DelLeft(int count)
{
node *p = head;
while(count--){
head = head->next;
delete p;
p = head;
}
head->prev = NULL;
}
void linklist::DelRight(int count)
{
node *p = tail;
while(count--){
tail = tail->prev;
delete p;
p = tail;
}
tail->next = NULL;
}
void linklist::Traverse(void)
{
node *p = head;
while(p != NULL){
fprintf(fo, "%d\n", p->data);
p = p->next;
}
}
int main(void)
{
fi = fopen("cline.in", "r");
fo = fopen("cline.out", "w");
int s, num = 1;
linklist cline;
fscanf(fi, "%d\n", &s);
while(s--){
char a, b;
int k;
fscanf(fi, "%c %c\n", &a, &b);
switch(a){
case 'A':
switch(b){
case 'L':
cline.AddLeft(num);
break;
case 'R':
cline.AddRight(num);
break;
}
num++;
break;
case 'D':
fscanf(fi, "%d\n", &k);
switch(b){
case 'L':
cline.DelLeft(k);
break;
case 'R':
cline.DelRight(k);
break;
}
break;
}
}
cline.Traverse();
fclose(fi);
fclose(fo);
return 0;
}