読者です 読者をやめる 読者になる 読者になる

Linked list in C

#include <stdio.h>
#include <stdlib.h>

struct List;
struct List {
  int val;
  struct List * next;
};
typedef struct List List;

List * allocate_node( void ) {
  return (List*) malloc( sizeof(List) );
}

int main(void) {
  int i;
  List *head, *list, *next;

  head = allocate_node();
  list = head;

  for ( i = 0; i < 100; i++ ) {
    list->val = i;
    list->next = allocate_node();
    list = list->next;
  }
  list->next = NULL;

  list = head;
  while ( list->next != NULL) {
    printf("%d\n", list->val);
    next = list->next;
    free( list );
    list = next;
  }
  free( list );
}