[ create a new paste ] login | about

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

C, pasted on Jul 20:
#include <stdio.h>
#include <stdlib.h>
typedef struct tag_node_t{ int data; struct tag_node_t *next; }node_t;
typedef struct tag_list_t{ node_t *first; }list_t;
void list_add(list_t *list, int data){
 node_t *node, *last;
 node=malloc(sizeof(node_t));
 node->data=data;
 if(list->first==NULL){
  list->first=node;
  node->next=node;
 }else{
  for(last=list->first;last->next!=list->first;last=last->next);
  node->next=last->next;
  last->next=node;
 }
}
int main(void){
 list_t list={0};
 node_t *node;
 list_add(&list, 1);
 list_add(&list, 2);
 list_add(&list, 3);
 list_add(&list, 4);
 for(node=list.first;;node=node->next){
  printf("%d\n", node->data);
  if(node->next==list.first) break;
 }
 return 0;
}


Output:
1
2
3
4
1
2
3
4


Create a new paste based on this one


Comments: