Advertisement
Guest User

lab

a guest
Nov 20th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.70 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. #define LIMIT 30
  5.  
  6. enum record_status {EMPTY, DELETED, OCCUPIED};
  7.  
  8. struct Student
  9. {
  10.       int student_id;
  11.       float marksOop345;
  12.       char student_name[30];
  13. };
  14.  
  15. struct Record
  16. {
  17.       struct Student info;
  18.       enum record_status status;
  19. }; 
  20.  
  21. int hash_function(int key)
  22. {
  23.       return (key % LIMIT);
  24. }
  25.  
  26. int search_records(int key, struct Record hash_table[])
  27. {
  28.       int count, temp, position;
  29.       temp = hash_function(key);               
  30.       position = temp;
  31.       for(count = 1; count != LIMIT - 1; count++)
  32.       {
  33.             if(hash_table[position].status == EMPTY)
  34.             {
  35.                   return -1;
  36.             }
  37.             if(hash_table[position].info.student_id == key)
  38.             {
  39.                   return position;
  40.             }
  41.             position = (temp + count) % LIMIT;                             
  42.       }
  43.       return -1;
  44. }
  45.  
  46. void insert_records(struct Student sturec, struct Record hash_table[])
  47. {
  48.       int count, position, temp;
  49.       int key = sturec.student_id;
  50.       temp = hash_function(key);               
  51.       position = temp; 
  52.       for(count = 1; count != LIMIT - 1; count++)
  53.       {
  54.             if(hash_table[position].status == EMPTY || hash_table[position].status == DELETED)
  55.             {
  56.                   hash_table[position].info = sturec;
  57.                   hash_table[position].status = OCCUPIED;  
  58.                   printf("\nRecord Inserted into Hash Table\n");
  59.                   return;
  60.             }
  61.             if(hash_table[position].info.student_id == key)
  62.             {
  63.                   printf("\nDuplicate Record cannot be Inserted\n");
  64.                   return;
  65.             }
  66.             position = (temp + count) % LIMIT;             
  67.       }
  68.       printf("\nHash Table Limit Exceeded\n");
  69. }
  70.  
  71. void display_records(struct Record hash_table[])
  72. {
  73.       int count;
  74.       printf("\nHash Table\n");
  75.       for(count = 0; count < LIMIT; count++)
  76.       {
  77.             printf("[%d]:\t", count);
  78.             if(hash_table[count].status == OCCUPIED)
  79.             {
  80.                   printf("Occupied - ID: %d Name: %s Marks: %f",hash_table[count].info.student_id, hash_table[count].info.student_name, hash_table[count].info.marksOop345);
  81.             }
  82.             else if(hash_table[count].status == DELETED)
  83.             {
  84.                   printf("\nRecord is Deleted\n");
  85.             }
  86.             else
  87.             {
  88.                   printf("\nHash Table is Empty\n");
  89.             }
  90.       }
  91. }
  92.  
  93. void delete_records(int key, struct Record hash_table[])
  94. {
  95.       int position = search_records(key, hash_table);
  96.       if(position == -1)
  97.       {
  98.             printf("\nKey Not Found\n");
  99.       }
  100.       else
  101.       {
  102.             hash_table[position].status = DELETED;
  103.       }
  104. }
  105.  
  106. int main()
  107. {
  108.       int count, key, option;
  109.       struct Record hash_table[LIMIT];
  110.       struct Student sturec;
  111.       for(count = 0; count <= LIMIT - 1; count++)
  112.       {
  113.             hash_table[count].status = EMPTY;
  114.       }
  115.       while(1)
  116.       {
  117.             printf("1. Insert a Record\n");
  118.             printf("2. Delete a Record\n");
  119.             printf("3. Search a Record\n");
  120.             printf("4. Display Records\n");
  121.             printf("5. Exit\n");
  122.             printf("Enter Your Option:\t");
  123.             scanf("%d", &option);
  124.             switch(option)
  125.             {
  126.                   case 1: printf("\nEnter Student ID:\t");
  127.                           scanf("%d", &sturec.student_id);         
  128.                           printf("Enter Student Name:\t");
  129.                           scanf("%s", sturec.student_name);
  130.                           printf("Enter Marks:\t");
  131.                           scanf("%d", &sturec.marksOop345);
  132.                           insert_records(sturec, hash_table);
  133.                           break;
  134.  
  135.                   case 2: printf("\nEnter the Key to Delete:\t");
  136.                           scanf("%d", &key);
  137.                           delete_records(key, hash_table);
  138.                           break;
  139.  
  140.                   case 3: printf("\nEnter the Key to Search:\t");
  141.                           scanf("%d", &key);
  142.                           count = search_records(key, hash_table);
  143.                           if(count == -1)
  144.                           {
  145.                                 printf("\nRecord Not Found\n");
  146.                           }
  147.                           else
  148.                           {
  149.                                 printf("\nRecord Found at Index Position:\t%d\n", count);
  150.                           }
  151.                           break;
  152.  
  153.                   case 4: display_records(hash_table);
  154.                           break;
  155.  
  156.                   case 5: exit(1);
  157.             }
  158.       }
  159.       return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement