[ create a new paste ] login | about

Link: http://codepad.org/LYVVr9nU    [ 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;
};

int putlist(struct NAME **first, char *name) {
  char *p;
  struct NAME *node;
  if (!(p = malloc(strlen(name) + 1)))
    return 0;
  strcpy(p, name);
  if (!(node = malloc(sizeof(struct NAME)))) {
    free(p);
    return 0;
  }
  node->name = p;
  node->next = *first;
  *first = node;
  return 0;
}

char *getlist(struct NAME **first) {
  char *p;
  struct NAME *node;
  if (!*first)
    return NULL;
  node = *first;
  p = node->name;
  *first = node->next;
  free(node);
  return p;
}

int main() {
  struct NAME *first;
  char *p;
  first = NULL;

  putlist(&first, "山田");
  putlist(&first, "田中");
  putlist(&first, "佐藤");
  putlist(&first, "鈴木");
  putlist(&first, "村上");
  putlist(&first, "山本");
  putlist(&first, "裏山");
  putlist(&first, "田山");
  putlist(&first, "小島");
  putlist(&first, "島々");

  while (p = getlist(&first)) {
    printf("%s\n", p);    
    free(p);
  }
  return 0;
}
/* end */


Output:
1
2
3
4
5
6
7
8
9
10
島々
小島
田山
裏山
山本
村上
鈴木
佐藤
田中
山田


Create a new paste based on this one


Comments: