#include #include #include #ifndef MAX_LINE_SIZE #define MAX_LINE_SIZE 1000 #endif #ifdef WITH_ARRAY #ifndef MAX_INPUT_SIZE #define MAX_INPUT_SIZE 2000 #endif #else struct line_list { struct line_list * prev; char line[MAX_LINE_SIZE]; }; #endif int main() { #ifdef WITH_ARRAY char lines[MAX_INPUT_SIZE][MAX_LINE_SIZE]; int count = 0; while(count < MAX_INPUT_SIZE; fgets(lines[count], MAX_LINE_SIZE, stdin) != 0) ++count; while(count > 0) { printf("%s", lines[count]); --count; } #else char buf[MAX_LINE_SIZE]; struct line_list * root = 0; while(fgets(buf, MAX_LINE_SIZE, stdin) != 0) { struct line_list * cur_line; cur_line = malloc(sizeof(struct line_list)); if (!cur_line) { fprintf(stderr, "no more memory!\n"); break; } strcpy(cur_line->line, buf); cur_line->prev = root; root = cur_line; } while(root) { struct line_list * tmp = root; printf("%s", root->line); root = root->prev; free(tmp); } #endif return 0; }