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

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 4.28 KB  |  hits: 10  |  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 <stdbool.h>
  4.  
  5. // some help func
  6. void
  7. debug(char* func, char* string)
  8. {
  9.         printf("in -> [%s] :: %s",func,string);
  10. }
  11. // define Linked list
  12. typedef struct linked_list llist;
  13. typedef struct linked_list_node llnode;
  14.  
  15. struct
  16. linked_list
  17. {
  18.         int size;
  19.        
  20.         struct linked_list_node* first;
  21.         struct linked_list_node* last;
  22. };
  23.  
  24. struct
  25. linked_list_node
  26. {
  27.         void* value;
  28.         struct linked_list_node* next;
  29. };
  30.  
  31. // Linked list functions
  32.  
  33. // init new linked list
  34. llist*
  35. ll_init(void)
  36. {
  37.         llist* new = malloc(sizeof(llist));
  38.         new -> size = 0;
  39.         new -> last = 0;
  40.         new -> first = 0;
  41.         debug((char*)__func__,"llist created\n");
  42.         return new;
  43. }
  44.  
  45. // append new node to linked list
  46. llnode*
  47. ll_append(llist* base, char* string)
  48. {
  49.         /*
  50.         if last : last->next = new
  51.          */
  52.        
  53.         // allocate new list node
  54.         llnode* new = malloc(sizeof(llnode));
  55.        
  56.         // if last node NULL and llist.size is zero
  57.         // list is clear
  58.         if ( base -> last == 0 && base -> size == 0)
  59.         {
  60.                 base -> first = new;
  61.                 base -> last = new;
  62.                 base -> size += 1;
  63.                 new -> value = (char*) string;
  64.                 debug((char*)__func__,"list node created in empty list\n");
  65.                 return new;
  66.         }
  67.         else
  68.         {
  69.                 // if last exists
  70.                 if ( base -> last != NULL )
  71.                 {
  72.                         // check for error
  73.                         if ( base -> size == 0 )
  74.                         {
  75.                                 printf("trying to append in end but llist size is zero\n");
  76.                                 free(new);
  77.                                 return 0;
  78.                         }
  79.  
  80.                         base -> last -> next = new;
  81.                         base -> last = new;
  82.                         base -> size += 1;
  83.                         new -> value = (char*)string;
  84.                         debug((char*)__func__,"list allocated in list with other childs\n");
  85.                         return new;
  86.                 }
  87.  
  88.                 // if we have first, but don't have last
  89.                 if ( base -> first && base -> last == NULL)
  90.                 {
  91.                         printf("you have first element, but doesnt have last\n");
  92.                         llnode* current = base -> first;
  93.                         bool finded = false;
  94.                         while (finded == false)
  95.                         {
  96.                                 if (current -> next == NULL)
  97.                                 {
  98.                                         current -> next = new;
  99.                                         base -> last = new;
  100.                                         base -> size += 1;
  101.                                         new -> value = (char*)string;
  102.                                         printf("llnode appended to llist with some errors\n");
  103.                                         finded = true;
  104.                                         return new;
  105.                                 }
  106.                                 else
  107.                                 {
  108.                                         current = current -> next;             
  109.                                 }
  110.  
  111.                         }
  112.                 }
  113.                 return 0;
  114.         }
  115. }
  116.  
  117. void
  118. ll_print_all(base)
  119.         llist* base;
  120. {
  121.         if (base -> first == NULL)
  122.         {
  123.                 printf("sorry, we cant find first element in your list\n");
  124.         }
  125.         else
  126.         {
  127.                 llnode* current = base -> first;
  128.                 int i = 0;
  129.                 for(i; i < (base -> size); i++)
  130.                 {
  131.                         printf("-> %s\n",(char*)current -> value);
  132.                         current = current -> next;
  133.                 }
  134.                 printf("size is %d\n",i);
  135.         }
  136. }
  137.  
  138. bool
  139. ll_lookup(llist* base, char* keyword)
  140. {
  141.         if (base -> first == 0) {
  142.                 printf("Sorry, this llist is empty\n");
  143.                 return false;
  144.         }
  145.  
  146.         llnode* current = base -> first;
  147.        
  148.         bool finded = false;
  149.         int i = 0;
  150.         while (finded == false)
  151.         {
  152.                 if (current -> value == keyword)
  153.                 {
  154.                         printf("value %s finded at position %d\n",keyword,i);
  155.                         return true;
  156.                 }
  157.                 else
  158.                 {
  159.                         i++;
  160.                         if (current -> next == 0)
  161.                         {
  162.                                 printf("Sorry, we cant find this keyword in list\n");
  163.                                 return false;
  164.                         }
  165.                         else
  166.                         {
  167.                                 current = current -> next;
  168.                         }
  169.                 }
  170.         }
  171.         return false;
  172. }
  173.  
  174. void
  175. ll_full_destroy(llist* base)
  176. {
  177.         if (base -> first == NULL)
  178.         {
  179.                 free(base);
  180.                 return;
  181.         }
  182.  
  183.         llnode* current = base -> first;
  184.         bool has_next = true;
  185.         while (has_next == true)
  186.         {
  187.                 if (current -> next)
  188.                 {
  189.                         llnode *next_current = current -> next;
  190.                         free(current);
  191.                         current = next_current;
  192.                 }
  193.                 else
  194.                 {
  195.                         free(current);
  196.                         has_next = false;
  197.                 }
  198.         }
  199.  
  200.         free(base);
  201. }
  202.  
  203. char*
  204. ll_get_at(llist* base,int position)
  205. {
  206.         int i = 0;
  207.        
  208.         if (base -> size < position)
  209.         {
  210.                 printf("Wrong index\n");
  211.                 return 0;
  212.         }
  213.  
  214.         if (base -> first)
  215.         {
  216.                 llnode* current = base -> first;
  217.                 if_equal:
  218.                 if ( i == position) {
  219.                         return (char*) current -> value;
  220.                 }
  221.                 else
  222.                 {
  223.                         current = current -> next;
  224.                         i++;
  225.                         goto if_equal;
  226.                 }
  227.         }
  228.         else
  229.         {
  230.                 printf("list is nulled\n");
  231.                 return 0;
  232.         }
  233. }
  234.  
  235. int main(void) {
  236.         llist* ll_base = ll_init();
  237.         llnode* a = ll_append(ll_base,"First");
  238.         llnode* b = ll_append(ll_base,"Second");
  239.         llnode* c = ll_append(ll_base,"Third");
  240.  
  241.     ll_print_all(ll_base);
  242.  
  243.         ll_lookup(ll_base,"ddd");
  244. //      ll_full_destroy(ll_base);
  245.         printf("%s at position %d",ll_get_at(ll_base,4),4);
  246.  
  247. }