Advertisement
NB52053

ffff

Oct 23rd, 2018
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define numbOfList 4
  6. #define numbOfChars 50
  7.  
  8.  
  9. struct symbolInfo{
  10.     char name[numbOfChars];
  11.     char ID[numbOfChars];
  12.     char CGPA[numbOfChars];
  13.    
  14.     struct symbolInfo *next;
  15. }*symbolTable[numbOfList];
  16.  
  17.  
  18. void displayAll(){
  19.  
  20.     printf("Displaying the list :- \n");
  21.     struct symbolInfo *isNull;
  22.  
  23.     int i;
  24.     for(i=0; i<numbOfList;i++){
  25.         isNull=symbolTable[i];
  26.  
  27.         printf("\n%d No. list :  ", i+1);
  28.         while(isNull!=NULL){
  29.             printf("<%s,%s, %s>  ",isNull->name, isNull->ID);
  30.             isNull=isNull->next;
  31.         }
  32.     }
  33.  
  34.     printf("\n");
  35. }
  36.  
  37.  
  38. void insert(char setName[numbOfChars], char setID[numbOfChars], int hashKey){
  39.     struct symbolInfo *isNull;
  40.     struct symbolInfo *temp;
  41.     isNull = symbolTable[hashKey];
  42.     temp = malloc(sizeof(struct symbolInfo));
  43.     temp -> next = NULL;
  44.     strcpy(temp->name, setName);
  45.     strcpy(temp->ID, setID);
  46.     symbolTable[hashKey] = temp;
  47.     symbolTable[hashKey] ->next = isNull;       //assume, symbolTable[hashKey] == head
  48. }
  49.  
  50.  
  51.  
  52.  
  53.  
  54. void deleteFromList(char getName[numbOfChars], char getID[numbOfChars]){
  55.     struct symbolInfo *isNull;
  56.     struct symbolInfo *prev;
  57.  
  58.     int listNumber = getHashKey(getName);
  59.     isNull = symbolTable[listNumber];
  60.  
  61.     if((strcmp(symbolTable[listNumber]->name,getName) == 0)&& (strcmp(symbolTable[listNumber]->ID,getID)== 0)){
  62.         symbolTable[listNumber] = isNull->next;
  63.     }
  64.     else{
  65.         while(isNull!=NULL){
  66.             if((strcmp(isNull->name,getName) == 0) && (strcmp(isNull->ID,getID) == 0)){
  67.                 prev->next=isNull->next;
  68.                 break;
  69.             }
  70.             prev=isNull;
  71.             isNull=isNull->next;
  72.         }
  73.     }
  74. }
  75.  
  76.  
  77. int getHashKey(char value[numbOfChars]){
  78.     int total = 0;
  79.     int i;
  80.  
  81.     for(i=0; i<strlen(value); i++){
  82.         total = total + value[i];
  83.     }
  84.  
  85.     return total%numbOfList;
  86. }
  87.  
  88.  
  89. void menu(){
  90.  
  91.     int choice;
  92.  
  93.     while(1){
  94.         printf("-------Menu------\n");
  95.         printf("1.  Insert\n");
  96.         printf("3.  Delete\n");
  97.         printf("5.  Show lists\n");
  98.         printf("6.  Exit\n");
  99.         printf("-----------------\n\n");
  100.  
  101.         printf("Enter choice: ");
  102.         scanf("%d", &choice);
  103.         printf("\n");
  104.  
  105.         if(choice == 6){
  106.             break;
  107.         }
  108.         else if (choice == 1){
  109.  
  110.             printf("Keep inserting. Enter 0 in both cases to stop\n\n");
  111.  
  112.             while(1){
  113.                 char setName[numbOfChars];
  114.                 char setID[numbOfChars];
  115.                 int hashKey;
  116.  
  117.                 printf("Enter the name : ");
  118.                 scanf("%s", setName);
  119.                 printf("Enter class type : ");
  120.                 scanf("%s", setID);
  121.                 printf("\n");
  122.  
  123.                 if((strcmp(setName, "0") == 0) && (strcmp(setID, "0") == 0)){
  124.                     break;
  125.                 }
  126.  
  127.                 hashKey = getHashKey(setName);
  128.                 insert(setName, setID, hashKey);
  129.             }
  130.         }
  131.  
  132.         else if (choice == 3){
  133.  
  134.             char getName[numbOfChars];
  135.             char getID[numbOfChars];
  136.  
  137.             printf("Enter the name for deleting: ");
  138.             scanf("%s", getName);
  139.             printf("Enter class type for deleting: ");
  140.             scanf("%s", getID);
  141.             printf("\n");
  142.  
  143.             deleteFromList(getName, getID);
  144.         }
  145.  
  146.         else if (choice == 5){
  147.  
  148.             displayAll();
  149.         }
  150.  
  151.         printf("\n\n");
  152.     }
  153. }
  154.  
  155.  
  156. int main(){
  157.  
  158.     menu();
  159.  
  160.     printf("\n\n\n");
  161.     return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement