Advertisement
NB52053

BBB

Oct 30th, 2018
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #include <stdbool.h>
  5.  
  6. #define MAX 61
  7. #define input_length 500
  8.  
  9. struct SymbolInfo{
  10.     char symbol[input_length];
  11.     char type[input_length];
  12.     struct SymbolInfo* next;
  13. };
  14.  
  15. struct SymbolInfo* SymbolTable[MAX];
  16. struct SymbolInfo* temp;
  17.  
  18. void reset_table(){
  19.     for(int i = 0; i < MAX; i++)
  20.         SymbolTable[i] = NULL;
  21. }
  22.  
  23. void print_message(){
  24.     printf("\nPlease Choose option from below:");
  25.     printf("\n1.Insert  2.Search  3.Delete  4.Show  0.EXIT");
  26. }
  27.  
  28. int getHashKey(char const str[]){
  29.     int length = strlen(str), sum = 0;
  30.     for(int i = 0; i < length; i++)
  31.         sum += str[i];
  32.     return (sum % MAX);
  33. }
  34.  
  35. void Insert(char const symbol[], char const type[]){
  36.     int hashKey = getHashKey(symbol);
  37.     temp = (struct SymbolInfo*) malloc(sizeof(struct SymbolInfo));
  38.     strcpy(temp->symbol, symbol);
  39.     strcpy(temp->type, type);
  40.     temp->next = SymbolTable[hashKey];
  41.     SymbolTable[hashKey] = temp;
  42. }
  43.  
  44. struct SymbolInfo* Search(char const symbol[], char const type[]){
  45.     int hashKey = getHashKey(symbol);
  46.     temp = SymbolTable[hashKey];
  47.     while(temp != NULL && (strcmp(temp->symbol, symbol) || strcmp(temp->type, type)))
  48.         temp = temp->next;
  49.     return temp;
  50. }
  51.  
  52. bool Delete(char const symbol[], char const type[]){
  53.     int hashKey = getHashKey(symbol);
  54.     if(SymbolTable[hashKey] != NULL){
  55.         temp = SymbolTable[hashKey];
  56.         if(strcmp(temp->symbol, symbol)==0 && strcmp(temp->type, type)==0){
  57.             SymbolTable[hashKey] = temp->next;
  58.             free(temp);
  59.         }
  60.         else{
  61.             while((temp->next != NULL) && (strcmp(temp->next->symbol, symbol) || strcmp(temp->next->type, type)))
  62.                 temp = temp->next;
  63.  
  64.             if((temp->next != NULL) && (strcmp(temp->next->symbol, symbol) == 0 && strcmp(temp->next->type, type) == 0)){
  65.                 struct SymbolInfo* temporary = temp->next;
  66.                 temp->next = temp->next->next;
  67.                 free(temporary);
  68.             }
  69.             else if((temp->next == NULL) && strcmp(temp->symbol, symbol) == 0 && strcmp(temp->type, type) == 0)
  70.                 free(temp);
  71.             else return 0;
  72.         }
  73.     }
  74.     else return 0;
  75.  
  76.     return 1;
  77. }
  78.  
  79. void Show(){
  80.     printf(" ______________________________________\n");
  81.     for(int i = 0; i < MAX; i++){
  82.         if(SymbolTable[i] != NULL){
  83.             printf("  SymbolTable[%d] = ", i);
  84.             temp = SymbolTable[i];
  85.             while(temp->next != NULL){
  86.                 printf("(%s, %s)->", temp->symbol, temp->type);
  87.                 temp = temp->next;
  88.             }
  89.             printf("(%s, %s)\n", temp->symbol, temp->type);
  90.         }
  91.     }
  92.     printf(" ______________________________________\n");
  93. }
  94.  
  95. int main(){
  96.     int number_of_element=0;
  97.  
  98.     print_message();
  99.     int op;
  100.     scanf("%d", &op);
  101.     while(1){
  102.         if(op == 0) return 0;
  103.         else if(op == 4){
  104.             if(number_of_element){
  105.                 printf("\nTotal number of token in SymbolTable: %d\n", number_of_element);
  106.                 Show();
  107.             }
  108.             else printf("SymbolTable is empty!!\n");
  109.         }
  110.         else{
  111.             char symbol[input_length], type[input_length];
  112.  
  113.             printf("Input Symbol: \n");
  114.             scanf("%s", symbol);
  115.             printf("Input Type: \n");
  116.             scanf("%s", type);
  117.  
  118.             if(op == 1){
  119.                 if(Search(symbol, type) == NULL){
  120.                     Insert(symbol, type);
  121.                     printf("Succesfully inserted token <%s, %s> in the SymbolTable\n",
  122.                            symbol, type);
  123.                     number_of_element++;
  124.                 }
  125.                 else printf("Token <%s, %s> already exists in SymbolTable", symbol, type);
  126.             }
  127.             else if(op == 2){
  128.                 if(Search(symbol, type) != NULL)
  129.                     printf("Token <%s, %s> exists in the SymbolTable\n", symbol, type);
  130.                 else printf("Token <%s, %s>  does not exist in the SymbolTable\n", symbol, type);
  131.             }
  132.             else if(op == 3){
  133.                 if(Delete(symbol, type)){
  134.                     printf("Succesfully deleted token <%s, %s> from the SymbolTable\n",
  135.                            symbol, type);
  136.                     number_of_element--;
  137.                 }
  138.                 else printf("Not possible to delete, token <%s, %s>  does not exist in the SymbolTable\n", symbol, type);
  139.             }
  140.             else printf("Wrong Keyword\n");
  141.         }
  142.  
  143.         print_message();
  144.         scanf("%d", &op);
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement