NB52053

nadira compiler lab 2

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