[ create a new paste ] login | about

Link: http://codepad.org/5lf0QARe    [ raw code | output | fork ]

C, pasted on Apr 16:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct NAME {
  char *name;
  struct NAME *next;
};


#define N 5
#define BUFFSIZE 2048
int main() {
  struct NAME *p, *before, *first, *r;
  char buff[BUFFSIZE], *q;
  int i;

  first = before = NULL;
  for (i = 0; i < N; i++) {
    printf("> ");
    fgets(buff, BUFFSIZE, stdin);
    q = malloc(strlen(buff) + 1);
    if (q == NULL) {
      printf("error: cannot allocate enough memory, aborted.\n");
      exit(-1);
    }
    strcpy(q, buff);
    p = malloc(sizeof(struct NAME));
    if (p == NULL) {
      printf("error: cannot allocate enough memory, aborted.\n");
      exit(-1);
    }
    p->name = q;
    p->next = NULL;
    if (before != NULL)
      before->next = p;
    before = p;
    if (i == 0)
      first = p;
  }
  for (p = first; p != NULL; p = p->next)
    printf("%s", p->name);
  p = first;
  while (p != NULL) {
    free(p->name);
    r = p;
    p = p->next;
    free(r);
  }
  return 0;
}
/* end */


Output:
1
> > > > > 


Create a new paste based on this one


Comments: