#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node {
char *data;
struct node *next;
struct node *prev;
};
void push_head(struct node **root, char *data) {
struct node *p;
if ((p
= malloc(sizeof(struct node
))) != 0) { p->data = data;
if (*root == 0) {
p->next = p;
p->prev = p;
} else {
p->next = *root;
p->prev = (*root)->prev;
p->prev->next = p;
p->next->prev = p;
}
*root = p;
}
}
void push_tail(struct node **root, char *data) {
struct node *p;
if ((p
= malloc(sizeof(struct node
))) != 0) { p->data = data;
if (*root == 0) {
p->next = p;
p->prev = p;
*root = p;
} else {
p->next = *root;
p->prev = (*root)->prev;
p->prev->next = p;
p->next->prev = p;
}
}
}
char *pop(struct node **root) {
char *r;
struct node *p;
if (*root == 0)
return 0;
p = *root;
r = p->data;
if (p->next == p) {
*root = 0;
} else {
p->next->prev = p->prev;
p->prev->next = p->next;
*root = p->next;
}
return r;
}
#define BUFFSIZE 1024
int main(int argc, char *argv[]) {
struct node *rootS = 0;
static char buff[BUFFSIZE];
char *p;
int flag_reverse;
int flagsig;
if (argc != 2) {
usage:
fprintf(stderr
, "usage: %s -r/f.\n option -r:reverse, -f:forward\n", argv
[0]); }
if (*argv[1] == '-') {
if (*(argv[1] + 1) == 'f')
flag_reverse = 0;
else if (*(argv[1] + 1) == 'r')
flag_reverse = 1;
else
goto usage;
} else {
goto usage;
}
rootS = 0;
for (;fgets(buff
, BUFFSIZE
, stdin
);) { if (flag_reverse)
push_head(&rootS, p);
else
push_tail(&rootS, p);
}
while ((p = pop(&rootS)) != 0) {
}
return 0;
}
/* end */
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKc3RydWN0IG5vZGUgewogIGNoYXIgKmRhdGE7CiAgc3RydWN0IG5vZGUgKm5leHQ7CiAgc3RydWN0IG5vZGUgKnByZXY7Cn07Cgp2b2lkIHB1c2hfaGVhZChzdHJ1Y3Qgbm9kZSAqKnJvb3QsIGNoYXIg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SAwKSB7CiAgICBwcmludGYoIiVzIiwgcCk7CiAgICBmcmVlKHApOwogIH0KICByZXR1cm4gMDsKfQovKiBlbmQgKi8K