Advertisement
B1KMusic

Linked list demo

Oct 14th, 2015
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. typedef struct thing {
  6.     int id;
  7.     int last_id;
  8.     /* last_id is only for the head. You _could_ make separate structs
  9.      * for the head and its child nodes, but you _should_ keep them of
  10.      * the same datatype to avoid unnecessary complexity...like having
  11.      * to make a "superclass" that contains metadata about the structs
  12.      * so that it's possible to use them interchangably. That would be
  13.      * silly and unnecessary. Also, hot damn, look how perfectly these
  14.      * lines are aligning at the right side. That was unintentional...
  15.      */
  16.     char *str;
  17.     struct thing *next; /* You can also have *prev, if you need it. */
  18. } thing;
  19.  
  20. thing *new_head(char *name){
  21.     thing *t = malloc(sizeof(thing));
  22.     t->id = 0;
  23.     t->last_id = 0;
  24.     t->next = 0;
  25.     t->str = name;
  26.     return t;
  27. }
  28.  
  29. void push_thing(thing *head, char *name){
  30.     thing *t = malloc(sizeof(thing)),
  31.           *p = head;
  32.  
  33.     t->id = p->last_id++;
  34.     t->next = 0;
  35.     t->str = name;
  36.  
  37.     while(p->next){
  38.         p = p->next;
  39.     }
  40.     p->next = t;
  41. }
  42.  
  43. thing *find_thing(thing *head, int id){
  44.     thing *p = head;
  45.     while(p){
  46.         if(p->id == id){
  47.             return p;
  48.         } else {
  49.             p = p->next;
  50.         }
  51.     }
  52.     return 0;
  53. }
  54.  
  55. void destroy_list(thing *head){
  56.     thing *p = head,
  57.           *n = 0;
  58.     while(p){
  59.         n = p->next;
  60.         free(p->str);
  61.         free(p);
  62.         p = n;
  63.     }
  64. }
  65.  
  66. char *randstr(){
  67.     char *s = malloc(sizeof(char) * 16);
  68.     int i = 0;
  69.     while(i < 15){
  70.         s[i++] = 0x61 + (rand() % 26);
  71.     }
  72.     s[i] = 0;
  73.     return s;
  74. }
  75.  
  76. void do_list_report(thing *head){
  77.     thing *t = find_thing(head, 15);
  78.     printf("object 15 is: %s\n", t->str);
  79.     t = head;
  80.     while(t){
  81.         printf("ID #%02i = %s\n", t->id, t->str);
  82.         t = t->next;
  83.     }
  84. }
  85.  
  86. int main(){
  87.     int i;
  88.     thing *heads[2]; // The tutorial I watched used a global list head and global id, which is crap. Avoid global variables unlesss you're either demonstrating something or writing a codegolf.
  89.  
  90.     srand(time(0));
  91.     heads[0] = new_head(randstr());
  92.     heads[1] = new_head(randstr());
  93.     i = 20; // This is only kept small so the output readable. Go ahread and crank it up to something huge like 10,000 and watch C chew it up in seconds
  94.     while(i--){
  95.         push_thing(heads[0], randstr());
  96.         push_thing(heads[1], randstr());
  97.     }
  98.     printf("List #1:\n");
  99.     do_list_report(heads[0]);
  100.     printf("\nList #2:\n");
  101.     do_list_report(heads[1]);
  102.     destroy_list(heads[0]);
  103.     destroy_list(heads[1]);
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement