ClavinJune

cv_linklist_template.c

Oct 2nd, 2017
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.31 KB | None | 0 0
  1. /**
  2. * Data Structure - Single Linklist Template
  3. *
  4. * Buat penggunaan, cukup ganti Attribute dari struct Template aja
  5. * int value, dipakai untuk sorting,
  6. * jadi misal butuh fungsi untuk sorting angka, tinggal ganti semua value ke attribute yang diinginkan
  7. *
  8. * char string[255], dipakai untuk sorting
  9. * jadi misal butuh fungsi untuk sorting berdasarkan nama, tinggal ganti semua string[255] jadi nama[255]
  10. *
  11. * untuk tambah attribute tinggal ganti dibagian struct & fungsi createNode();
  12. *
  13. * tinggal #include "cv_linklist_template.c" di program utama kalian
  14. *
  15. * contoh program utama
  16. * ____________________________________________________________________________________________________________________
  17. * #include "stdio.h"
  18. * #include "cv_linklist_template.c"
  19. *
  20. * size_t main(int argc, char **argv){
  21. *   Data *pertama = createNode(0x01, "contoh buat node baru");
  22. *   Data *kedua   = createNode(0x02, "tinggal panggil fungsi createNode");
  23. *   Data *ketiga  = createNode(0x03, "Masukkan parameter sesuai Struct");
  24. *
  25. *   pushSortAscByInt(kedua);
  26. *   pushSortAscByInt(ketiga);
  27. *   pushSortAscByInt(pertama);
  28. *   viewData();
  29. * }
  30. * ____________________________________________________________________________________________________________________
  31. * Tested in C85, C90, C99, C11 Standard
  32. */
  33.  
  34. #include "malloc.h"
  35. #include "string.h"
  36.  
  37. const int  FAILED  = 0;
  38. const int SUCCESS  = 1;
  39.  
  40. struct Template{
  41.   int value; /* sorting by number */
  42.   char string[255]; /* sorting by string */
  43.   struct Template *next;
  44. }*head, *tail, *current;
  45. typedef struct Template Data;
  46.  
  47.  
  48. Data *createNode(int value, char string[255]);
  49. void pushHead(Data *newNode);
  50. void pushTail(Data *newNode);
  51. int popHead();
  52. int popTail();
  53. void popAll();
  54. int viewData();
  55.  
  56. int pushFirstData(Data *newNode);
  57. int popFirstData();
  58.  
  59. int isFirstOrLastAscIntData(Data *newNode);
  60. int isFirstOrLastAscStrData(Data *newNode);
  61.  
  62. int isFirstOrLastDescIntData(Data *newNode);
  63. int isFirstOrLastDescStrData(Data *newNode);
  64.  
  65. void pushSortAscByInt(Data *newNode);
  66. void pushSortAscByStr(Data *newNode);
  67.  
  68. void pushSortDescByInt(Data *newNode);
  69. void pushSortDescByStr(Data *newNode);
  70.  
  71.  
  72. /*Single Linklist starts here*/
  73. Data *createNode(int value, char string[255]){
  74.   Data *newNode   = (Data*)malloc(sizeof(Data));
  75.   newNode->value  = value;
  76.   newNode->next   = NULL;
  77.   strcpy(newNode->string, string);
  78.  
  79.   return newNode;
  80. }
  81.  
  82. void pushHead(Data *newNode){
  83.  
  84.   if( !pushFirstData(newNode) ){
  85.     newNode->next = head;
  86.     head          = newNode;
  87.   }
  88. }
  89.  
  90. void pushTail(Data *newNode){
  91.  
  92.   if( !pushFirstData(newNode) ){
  93.     tail->next = newNode;
  94.     tail = newNode;
  95.   }
  96. }
  97.  
  98. int popHead(){
  99.   if( !head ) return FAILED;
  100.  
  101.   if( !popFirstData() ){
  102.     current = head;
  103.     head    = head->next;
  104.     free(current);
  105.     return SUCCESS;
  106.   }
  107. }
  108.  
  109. int popTail(){
  110.   if( !head ) return FAILED;
  111.  
  112.   if( !popFirstData() ){
  113.     current = head;
  114.  
  115.     while( current->next != tail ) current = current->next;
  116.    
  117.     free(tail);
  118.     tail       = current;
  119.     tail->next = NULL;
  120.     return SUCCESS;
  121.   }
  122. }
  123.  
  124. void popAll(){
  125.   while( head ) popHead();
  126. }
  127.  
  128. int viewData(){
  129.   if( !head ) return FAILED;
  130.  
  131.   current = head;
  132.   int i = 0;
  133.   while( current ){
  134.     printf("%02d. %5d %10s", ++i, current->value, current->string);
  135.     if( current == head ) printf(" <- head ");
  136.     if( current == tail ) printf(" <- tail ");
  137.     puts("");
  138.     current = current->next;
  139.   }
  140. }
  141.  
  142. int pushFirstData(Data *newNode){
  143.   if( !head ){
  144.     head = tail = newNode;
  145.     return SUCCESS;
  146.   }
  147.   return FAILED;
  148. }
  149.  
  150. int popFirstData(){
  151.   if( head == tail ){
  152.     head = tail = NULL;
  153.     return SUCCESS;
  154.   }
  155.   return FAILED;
  156. }
  157.  
  158. int isFirstOrLastAscIntData(Data *newNode){
  159.   if( newNode->value < head->value || newNode->value >= tail->value ){
  160.     if( newNode->value < head->value )
  161.       pushHead(newNode);
  162.     else if( newNode->value >= tail->value )
  163.       pushTail(newNode);
  164.     return SUCCESS;
  165.   }
  166.   return FAILED;
  167. }
  168.  
  169. int isFirstOrLastAscStrData(Data *newNode){
  170.   if( strcmp(head->string, newNode->string) > 0 || strcmp(tail->string, newNode->string) <= 0 ){
  171.     if( strcmp(head->string, newNode->string) > 0 )
  172.       pushHead(newNode);
  173.     else if( strcmp(tail->string, newNode->string) <= 0 )
  174.       pushTail(newNode);
  175.     return SUCCESS;
  176.   }
  177.   return FAILED;
  178. }
  179.  
  180. int isFirstOrLastDescIntData(Data *newNode){
  181.   if( newNode->value > head->value || newNode->value <= tail->value ){
  182.     if( newNode->value > head->value )
  183.       pushHead(newNode);
  184.     else if( newNode->value <= tail->value )
  185.       pushTail(newNode);
  186.     return SUCCESS;
  187.   }
  188.   return FAILED;
  189. }
  190.  
  191. int isFirstOrLastDescStrData(Data *newNode){
  192.   if( strcmp(head->string, newNode->string) < 0 || strcmp(tail->string, newNode->string) >= 0 ){
  193.     if( strcmp(head->string, newNode->string) < 0 )
  194.       pushHead(newNode);
  195.     else if( strcmp(tail->string, newNode->string) >= 0 )
  196.       pushTail(newNode);
  197.     return SUCCESS;
  198.   }
  199.   return FAILED;
  200. }
  201.  
  202. void pushSortAscByInt(Data *newNode){
  203.   if( pushFirstData(newNode) ) return;
  204.  
  205.   if( isFirstOrLastAscIntData(newNode) ) return;
  206.  
  207.   current = head;
  208.   while( current ){
  209.     if( current->next->value > newNode->value ) break;
  210.     current = current->next;
  211.   }
  212.  
  213.   newNode->next = current->next;
  214.   current->next = newNode;
  215. }
  216.  
  217. void pushSortAscByStr(Data *newNode){
  218.   if( pushFirstData(newNode) ) return;
  219.  
  220.   if( isFirstOrLastAscStrData(newNode) ) return;
  221.  
  222.   current = head;
  223.   while( current ){
  224.     if( strcmp(current->next->string, newNode->string) > 0 ) break;
  225.     current = current->next;
  226.   }
  227.  
  228.   newNode->next = current->next;
  229.   current->next = newNode;
  230. }
  231.  
  232. void pushSortDescByInt(Data *newNode){
  233.   if( pushFirstData(newNode) ) return;
  234.  
  235.   if( isFirstOrLastDescIntData(newNode) ) return;
  236.  
  237.   current = head;
  238.   while( current ){
  239.     if( current->next->value < newNode->value ) break;
  240.     current = current->next;
  241.   }
  242.  
  243.   newNode->next = current->next;
  244.   current->next = newNode;
  245. }
  246.  
  247. void pushSortDescByStr(Data *newNode){
  248.   if( pushFirstData(newNode) ) return;
  249.  
  250.   if( isFirstOrLastDescStrData(newNode) ) return;
  251.  
  252.   current = head;
  253.   while( current ){
  254.     if( strcmp(current->next->string, newNode->string) < 0 ) break;
  255.     current = current->next;
  256.   }
  257.  
  258.   newNode->next = current->next;
  259.   current->next = newNode;
  260. }
  261. /*Single Linklist ends here*/
Add Comment
Please, Sign In to add comment