C++ Program to Implement Direct Addressing Tables

This C++ Program demonstrates operations on Direct Addressing Tables

Here is source code of the C++ Program to demonstrate Direct Addressing Tables. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program To Implement Direct Addressing Tables
  3.  */
  4. #include<iostream>
  5. #include<cstdlib>
  6. #include<string>
  7. #include<cstdio>
  8. using namespace std;
  9.  
  10. /*
  11.  * Table Declaration
  12.  */
  13. struct table
  14. {
  15.     string english;
  16.     int key;
  17.     table()
  18.     {}
  19.     table( int k, string e )
  20.     {
  21.         english=e;
  22.         key = k;
  23.     }
  24. };
  25.  
  26. /*
  27.  * Insertion of element at a key
  28.  */
  29. void INSERT( table T[], table x )
  30. {
  31.     T[ x.key ] = x;
  32. }
  33.  
  34. /*
  35.  * Deletion of element at a key
  36.  */
  37. void DELETE( table T[], table x )
  38. {
  39.     T[ x.key ] = table(0, "");
  40. }
  41.  
  42. /*
  43.  * Searching of element at a key
  44.  */
  45. table SEARCH( table T[], int k )
  46. {
  47.     return T[ k ];
  48. }
  49.  
  50. /*
  51.  * Main Contains Menu
  52.  */
  53. int main()
  54. {
  55.     int i, key, ch;
  56.     string str;
  57.     table T[65536];
  58.     table x;
  59.     for(i = 0; i < 65536;i++)
  60.         T[i] = table(0,"");
  61.     while (1)
  62.     {
  63.         cout<<"\n-------------------------------------"<<endl;
  64.         cout<<"\nOperations on Direct Addressing Table"<<endl;
  65.         cout<<"\n-------------------------------------"<<endl;
  66.         cout<<"1.Insert element into the key"<<endl;
  67.         cout<<"2.Delete element from the table"<<endl;
  68.         cout<<"3.Search element into the table"<<endl;
  69.         cout<<"4.Exit"<<endl;
  70.         cout<<"Enter your Choice: ";
  71.         cin>>ch;
  72.         switch(ch)
  73.         {
  74.         case 1:
  75.         {
  76.             string str1 = "";
  77.             cout<<"Enter the key value: ";
  78.             cin>>key;
  79.             cout<<"Enter the string to be inserted: ";
  80.             cin.ignore();
  81.             getline(cin, str);
  82.             INSERT(T, table(key, str));
  83.             break;
  84.         }
  85.         case 2:
  86.             cout<<"Enter the key of element to be deleted: ";
  87.             cin>>key;
  88.             x = SEARCH(T, key);
  89.             DELETE(T, x);
  90.             break;
  91.         case 3:
  92. 	    cout<<"Enter the key of element to be searched: ";
  93.             cin>>key;
  94.             x = SEARCH(T, key);
  95.             if (x.key == 0)
  96.             {
  97.                 cout<<"No element inserted at the key"<<endl;
  98.                 continue;
  99.             }
  100.             cout<<"Element at key "<<key<<" is-> ";
  101.             cout<<"\""<<x.english<<"\""<<endl;
  102.             break;
  103.         case 4:
  104.             exit(1);
  105.         default:
  106.             cout<<"Wrong Choice"<<endl;
  107.         }
  108.     }
  109.     return 0;
  110. }

$ g++ direct_addressing.cpp
$ a.out
-------------------------------------
 
Operations on Direct Addressing Table
 
-------------------------------------
1.Insert element into the key
2.Delete element from the table
3.Search element into the table
4.Exit
Enter your Choice: 1
Enter the key value: 1
Enter the string to be inserted: one
 
-------------------------------------
 
Operations on Direct Addressing Table
 
-------------------------------------
1.Insert element into the key
2.Delete element from the table
3.Search element into the table
4.Exit
Enter your Choice: 1
Enter the key value: 2
Enter the string to be inserted: two
 
-------------------------------------
 
Operations on Direct Addressing Table
 
-------------------------------------
1.Insert element into the key
2.Delete element from the table
3.Search element into the table
4.Exit
Enter your Choice: 1
Enter the key value: 3
Enter the string to be inserted: three
 
-------------------------------------
 
Operations on Direct Addressing Table
 
-------------------------------------
1.Insert element into the key
2.Delete element from the table
3.Search element into the table
4.Exit
Enter your Choice: 1
Enter the key value: 7
Enter the string to be inserted: seven
 
-------------------------------------
 
Operations on Direct Addressing Table
 
-------------------------------------
1.Insert element into the key
2.Delete element from the table
3.Search element into the table
4.Exit
Enter your Choice: 1
Enter the key value: 8 eight
Enter the string to be inserted: 
-------------------------------------
 
Operations on Direct Addressing Table
 
-------------------------------------
1.Insert element into the key
2.Delete element from the table
3.Search element into the table
4.Exit
Enter your Choice: 3
Enter the key of element to be searched: 7
Element at key 7 is-> "seven"
 
-------------------------------------
 
Operations on Direct Addressing Table
 
-------------------------------------
1.Insert element into the key
2.Delete element from the table
3.Search element into the table
4.Exit
Enter your Choice: 3
Enter the key of element to be searched: 2
Element at key 2 is-> "two"
 
-------------------------------------
 
Operations on Direct Addressing Table
 
-------------------------------------
1.Insert element into the key
2.Delete element from the table
3.Search element into the table
4.Exit
Enter your Choice: 2
Enter the key of element to be deleted: 8
 
-------------------------------------
 
Operations on Direct Addressing Table
 
-------------------------------------
1.Insert element into the key
2.Delete element from the table
3.Search element into the table
4.Exit
Enter your Choice: 3
Enter the key of element to be searched: 8
No element inserted at the key
 
-------------------------------------
 
Operations on Direct Addressing Table
 
-------------------------------------
1.Insert element into the key
2.Delete element from the table
3.Search element into the table
4.Exit
Enter your Choice: 4
 
 
------------------
(program exited with code: 1)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.