Advertisement
MBJ

Sorting linked list by label (strcmp)

MBJ
Jul 22nd, 2019
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.44 KB | None | 0 0
  1. void symbolTableOrder(){
  2.    // struct list_SymbolTable* cur_table = head_table;
  3.    struct list_SymbolEle* cur_ele = NULL;
  4.  
  5.    struct list_SymbolTable *top_table_cmp = head_table;
  6.    struct list_SymbolTable *cmp_table = top_table_cmp->next;
  7.  
  8.    int debugging = 0;
  9.    printf("\nORDERING   SYMBOL   TABLE...");
  10.    while(top_table_cmp->next != NULL && top_table_cmp != NULL){ // could be while top_table_cmp != NULL
  11.      
  12.       cmp_table = top_table_cmp->next; //
  13.  
  14.       cur_ele = top_table_cmp->tableLine_start;
  15.       printf("\nTop table being compared: ");
  16.       while(cur_ele->table_letter != ' '){
  17.          printf("%c",cur_ele->table_letter);
  18.          cur_ele = cur_ele->next;
  19.       }
  20.  
  21.       while(cmp_table != NULL){ // could be while cmp_table != NULL
  22.          char top_str[top_table_cmp->label_size + 1]; // 1 for '\0'
  23.          cur_ele = top_table_cmp->tableLine_start;
  24.          int count = 0;
  25.          printf("\ntop_str[i] being compared: "); // .. LoopCount
  26.          while(cur_ele->table_letter != ' '){
  27.             // printf("%c",cur_ele->table_letter);
  28.             top_str[count] = cur_ele->table_letter;
  29.             // printf("%i ",count);
  30.             printf("%c",top_str[count]);
  31.             count++;
  32.             cur_ele = cur_ele->next;
  33.          }
  34.          top_str[count] = '\0';
  35.          printf("%c",top_str[count]);
  36.  
  37.          char cmp_str[cmp_table->label_size + 1]; // 1 for '\0'
  38.          cur_ele = cmp_table->tableLine_start;
  39.          count = 0;
  40.          printf("\nCompare string: "); // .. Count
  41.          while(cur_ele->table_letter != ' '){
  42.             if((cmp_str[count] == '\0') || (cmp_str[count] == '\r') || (cmp_str[count] == '\e') || (cmp_str[count] == '\f'))
  43.                printf("\0");
  44.             cmp_str[count] = cur_ele->table_letter;
  45.             printf("%c", cmp_str[count]);
  46.             count++;
  47.             cur_ele = cur_ele->next;
  48.          }
  49.          cmp_str[count] = '\0';
  50.          printf("%c", cmp_str[count]);
  51.  
  52.          printf("\nCmp_table->next label: ");
  53.          if(cmp_table->next != NULL){
  54.             cur_ele = cmp_table->next->tableLine_start;
  55.             while(cur_ele->table_letter != ' '){
  56.                printf("%c", cur_ele->table_letter);
  57.                cmp_str[count] = cur_ele->table_letter;
  58.                count++;
  59.                cur_ele = cur_ele->next;
  60.             }
  61.          }
  62.          printf("\nstrcmp(top_str,cmp_str): %i",strcmp(top_str, cmp_str));
  63.          if(strcmp(top_str, cmp_str) > 0){
  64.             bool head_reset = false;
  65.             bool last_reset = false;
  66.             printf("\nTOP COMPARE SWITCHED");
  67.             struct list_SymbolTable *tmp1_table = (struct list_SymbolTable*) malloc(sizeof(struct list_SymbolTable));
  68.             // tmp1_table = top_table_cmp;
  69.             tmp1_table->tableLine_start = top_table_cmp->tableLine_start;
  70.             tmp1_table->label_size = top_table_cmp->label_size;
  71.             tmp1_table->line_number = top_table_cmp->line_number;
  72.  
  73.  
  74.             printf("\n\n COPYING NEXT AND PREVIOUS POINTERS..");
  75.  
  76.             printf("\ntmp1 label: ");
  77.             cur_ele = tmp1_table->tableLine_start;
  78.             while(cur_ele->table_letter != ' '){
  79.                printf("%c", cur_ele->table_letter);
  80.                cmp_str[count] = cur_ele->table_letter;
  81.                count++;
  82.                cur_ele = cur_ele->next;
  83.             }
  84.             tmp1_table->next = cmp_table->next;
  85.  
  86.             printf("\ntmp1->next label: ");
  87.             cur_ele = tmp1_table->next->tableLine_start;
  88.             while(cur_ele->table_letter != ' '){
  89.                printf("%c", cur_ele->table_letter);
  90.                cur_ele = cur_ele->next;
  91.             }
  92.             printf("\ncmp->next label: ");
  93.             cur_ele = cmp_table->next->tableLine_start;
  94.             while(cur_ele->table_letter != ' '){
  95.                printf("%c", cur_ele->table_letter);
  96.                cur_ele = cur_ele->next;
  97.             }
  98.  
  99.  
  100.             tmp1_table->prev = cmp_table->prev;
  101.  
  102.             printf("\ntmp1->prev label: ");
  103.             cur_ele = tmp1_table->prev->tableLine_start;
  104.             while(cur_ele->table_letter != ' '){
  105.                printf("%c", cur_ele->table_letter);
  106.                cur_ele = cur_ele->next;
  107.             }
  108.  
  109.             printf("\ncmp->prev label: ");
  110.             cur_ele = cmp_table->prev->tableLine_start;
  111.             while(cur_ele->table_letter != ' '){
  112.                printf("%c", cur_ele->table_letter);
  113.                cur_ele = cur_ele->next;
  114.             }
  115.  
  116.             cmp_table->next = top_table_cmp->next;
  117.             printf("\ntop_table->next label: ");
  118.             cur_ele = top_table_cmp->next->tableLine_start;
  119.             while(cur_ele->table_letter != ' '){
  120.                printf("%c", cur_ele->table_letter);
  121.                cur_ele = cur_ele->next;
  122.             }
  123.             printf("\n new cmp_table->next label: ");
  124.             cur_ele = cmp_table->next->tableLine_start;
  125.             while(cur_ele->table_letter != ' '){
  126.                printf("%c", cur_ele->table_letter);
  127.                cur_ele = cur_ele->next;
  128.             }
  129.  
  130.             cmp_table->prev = top_table_cmp->prev;
  131.  
  132.             // cmp table next and previous = top_cmp_table's next and previous
  133.  
  134.             printf("\t made it here too.");
  135.             printf(" x");
  136.             if(cmp_table != last_table)
  137.                tmp1_table->next->prev = cmp_table->next->prev;
  138.             else
  139.                last_reset = true; // reset last table BOOL
  140.             printf(" xx");
  141.             if(top_table_cmp != head_table)
  142.                cmp_table->prev->next = top_table_cmp->prev->next;
  143.             else
  144.                head_reset = true; // reset head table BOOL
  145.             printf(" xxx");
  146.             //printf("\n%i");
  147.             tmp1_table->prev->next = tmp1_table;//cmp_table->prev->next;
  148.  
  149.             printf("\t guess things got messed up over here.");
  150.  
  151.             top_table_cmp = cmp_table;
  152.             cmp_table = tmp1_table;
  153.             if(head_reset)
  154.                head_table = top_table_cmp;
  155.             else if(last_reset)
  156.                last_table = cmp_table;
  157.          }
  158.          cmp_table = cmp_table->next; // iterate thru symbol table
  159.       } // while cmp_table != NULL
  160.       debugging++;
  161.       printf("\nFinished %i round(s) of sorting",debugging);
  162.       // table_size--; // figured out which one is going to be the top_table_cmp;
  163.       top_table_cmp = top_table_cmp->next; // right now, says top_table_cmp->next === previous cmp_table->next
  164.    }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement