Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct line_t line_t;
- struct line_t {
- char *line;
- line_t *next;
- };
- static void reverse(char *s) {
- char *start, *end, tmp;
- if (!s || !*s) return;
- start = s;
- end = s + strlen(s) - 1;
- while (start < end) {
- tmp = *start;
- *start = *end;
- *end = tmp;
- start++;
- end--;
- }
- }
- static line_t *new_line(char *line) {
- line_t *new = (line_t *) malloc(sizeof(line_t));
- new->line = (char *) malloc(BUFSIZ);
- strcpy(new->line, line);
- reverse(new->line);
- new->next = NULL;
- return new;
- }
- static void push(line_t *top, char *line) {
- line_t *new = new_line(line);
- if (!top->next) {
- top->next = new;
- return;
- }
- new->next = top->next;
- top->next = new;
- }
- static char *pop(line_t *top) {
- char *line;
- if (!top->next) return NULL;
- line = top->next->line;
- top->next = top->next->next;
- return line;
- }
- int main(const int argc, const char **argv) {
- FILE *in = fopen(argv[1], "r");
- char line[BUFSIZ], *p;
- line_t top;
- top.next = NULL;
- while (fgets(line, BUFSIZ, in)) {
- line[strlen(line) - 1] = '\0';
- push(&top, line);
- }
- while ((p = pop(&top)))
- printf("%s\n", p);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement