Advertisement
MBJ

Sorting table

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