Friday 24 November 2017

C++ Program to Implement Direct Addressing Tables


Code:

#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-------------------------------------"<
        cout<<"\nOperations on Direct Addressing Table"<
        cout<<"\n-------------------------------------"<
        cout<<"1.Insert element into the key"<
        cout<<"2.Delete element from the table"<
        cout<<"3.Search element into the table"<
        cout<<"4.Exit"<
        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"<
                continue;
            }
            cout<<"Element at key "< ";
            cout<<"\""<
            break;
        case 4:
            exit(1);
        default:
            cout<<"Wrong Choice"<
        }
    }
    return 0;
}


Output:

-------------------------------------

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



More C++ Programs:















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...