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.
/*
* C++ Program To Implement Direct Addressing Tables
*/
#include<iostream>
#include<cstdlib>
#include<string>
#include<cstdio>
using namespace std;
/*
* Table Declaration
*/
struct table
{
string english;
int key;
table()
{}
table( int k, string e )
{
english=e;
key = k;
}
};
/*
* Insertion of element at a key
*/
void INSERT( table T[], table x )
{
T[ x.key ] = x;
}
/*
* Deletion of element at a key
*/
void DELETE( table T[], table x )
{
T[ x.key ] = table(0, "");
}
/*
* Searching of element at a key
*/
table SEARCH( table T[], int k )
{
return T[ k ];
}
/*
* Main Contains Menu
*/
int main()
{
int i, key, ch;
string str;
table T[65536];
table x;
for(i = 0; i < 65536;i++)
T[i] = table(0,"");
while (1)
{
cout<<"\n-------------------------------------"<<endl;
cout<<"\nOperations on Direct Addressing Table"<<endl;
cout<<"\n-------------------------------------"<<endl;
cout<<"1.Insert element into the key"<<endl;
cout<<"2.Delete element from the table"<<endl;
cout<<"3.Search element into the table"<<endl;
cout<<"4.Exit"<<endl;
cout<<"Enter your Choice: ";
cin>>ch;
switch(ch)
{
case 1:
{
string str1 = "";
cout<<"Enter the key value: ";
cin>>key;
cout<<"Enter the string to be inserted: ";
cin.ignore();
getline(cin, str);
INSERT(T, table(key, str));
break;
}
case 2:
cout<<"Enter the key of element to be deleted: ";
cin>>key;
x = SEARCH(T, key);
DELETE(T, x);
break;
case 3:
cout<<"Enter the key of element to be searched: ";
cin>>key;
x = SEARCH(T, key);
if (x.key == 0)
{
cout<<"No element inserted at the key"<<endl;
continue;
}
cout<<"Element at key "<<key<<" is-> ";
cout<<"\""<<x.english<<"\""<<endl;
break;
case 4:
exit(1);
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
}
$ 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.
Related Posts:
- Practice Programming MCQs
- Practice Computer Science MCQs
- Practice Design & Analysis of Algorithms MCQ
- Apply for Computer Science Internship
- Check Programming Books