Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 25th, 2012  |  syntax: None  |  size: 0.82 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <time.h>
  5.  
  6. struct node {
  7.   int value;
  8.   struct node *next;
  9. };
  10.  
  11. struct node *head;
  12.  
  13. struct node *create_node(int value){
  14.   struct node *n = (struct node*)malloc(sizeof(struct node));
  15.   n->next = NULL;
  16.   n->value = value;
  17.   return n;
  18. }
  19.  
  20. int insert_node(int value){
  21.   struct node *n;
  22.   for(n = head; n != NULL; n = n->next){
  23.     if(n->value == value) break;
  24.     if(n->next == NULL){
  25.        n->next = create_node(value);
  26.        break;
  27.      }
  28.   }
  29. }
  30.  
  31. int print(){
  32.   struct node *n = head;
  33.   while(n->next != NULL){
  34.     n = n->next;
  35.     printf("%d\n", n->value);
  36.   }
  37. }
  38.  
  39. int main(void){
  40.   srand((unsigned)time(NULL));
  41.   int i = 0;
  42.   int LIMIT = 10;
  43.   int RANGE = 100;
  44.   head = create_node(0);
  45.   while(i<LIMIT){
  46.     insert_node(rand()%RANGE+1);
  47.     i++;
  48.   }
  49.   print();
  50.   return 0;
  51. }