Tuesday, September 3, 2013

C. Implement Direct Access File Using Linear Probing Chaining Without Replacement Method Of Collision Resolution.


/*****************************************************
  NAME                      :  Jabir Daud Pathan
  PROGRAM            :  Implement Direct Access File Using Linear
                                       Probing Chaining Without Replacement Method
                                       Of Collision Resolution.
*****************************************************/


/*---------------<<  Header File  >>------------------*/

#include<iostream.h>
#include<conio.h>
#include<string.h>
#define size 10
#include<fstream.h>
#include<iomanip.h>
#define hash(key) key%size

typedef struct bank
{
            int acc_no;
            char name[size];
            int bal;
            int status;
            int link;
};

/*--------<<  Declaration of class linear_prob >>--------*/

class linear_prob
{
            char table[size];
            fstream table1;
            bank b;

            public:
                   linear_prob(char *a);
                   void display1(int);
                   void insert(bank);
                   void delet(int);
                   int search(int);
                   void display();
                   void read(int);
                   void write(int);
};

/*------<<  Function defination for display >>---------*/

void linear_prob :: display1(int rec)
{
            int i=rec;
            table1.open(table,ios::binary|ios::in|ios::nocreate);
            table1.seekg(rec*sizeof(bank),ios::beg);
            table1.read((char *)&b,sizeof(bank));
            if(b.status==0)
            {
                        cout<<"\n"<<i<<")"<<b.acc_no<<"    "<<b.name<<"    "<<setprecision(2)<<b.bal<<"   "<<b.link;
            }
            else
                        cout<<"\n"<<i<<")EMPTY";
            table1.close();
}

/*----------<<  Function defination for read >>------------*/

void linear_prob :: read(int rec)
{
            table1.open(table,ios::binary|ios::in);
            table1.seekg(rec*sizeof(bank),ios::beg);
            table1.read((char *)&b,sizeof(bank));
            table1.close();
}


/*----------<<  Function defination for write >>------------*/

void linear_prob :: write(int rec)
{
            table1.open(table,ios::binary|ios::nocreate|ios::out|ios::in);
            table1.seekg(rec*sizeof(bank),ios::beg);
            table1.write((char *)&b,sizeof(bank));
            table1.close();

}

/*----------<<  Function defination for linear probing >>------------*/

linear_prob :: linear_prob(char *a)
{
            int i;
            strcpy(table,a);
            b.status=1;
            b.link=-1;
            table1.open(table,ios::binary|ios::out);
            table1.close();
            for(i=0;i<size;i++)
            {
                        write(i);
            }
}

/*----------<<  Function defination for display >>------------*/

void linear_prob :: display()
{
            int i=1;
            cout<<"\n---------<< FILE >>-------\n\n";
            for(i=0;i<size;i++)
            {
                        display1(i);
            }
}

/*----------<<  Function defination for insert >>------------*/

void linear_prob :: insert(bank b1)
{
            int n,i,j,k,start,empty;
            b1.status=0;
            b1.link=-1;
            bank b2;
            start=hash(b1.acc_no);
            read(start);
            if(b.status==1)
            {
                        b=b1;
                        write(start);
                        return;
            }
            for(i=0;i<size;i++)
            {
                        empty=(start+i)%size;
                        read(empty);
                        if(b.status==1)
                        {
                          break;
                        }
            }
            if(i==size)
            {
                        cout<<"\n\nTable is empty...";
                        return;
            }
            j=start;
            read(j);

            if(hash(b.acc_no)==start)
            {
                        while(b.link!=-1)
                        {
                            j=b.link;
                            read(j);
                        }

                        b.link=empty;
                        write(j);
                        b=b1;
                        write(empty);
                        return;
            }

            b2=b;
            j=hash(b2.acc_no);
            read(j);

            while(b.link!=start)
            {
                         j=b.link;
                         read(j);
            }
            b.link=b2.link;
            write(j);

            while(b.link!=-1)
                        {
                            j=b.link;
                            read(j);
                        }
            b.link=empty;
            write(j);
            b=b2;
            b.link=-1;
            write(empty);
            b=b1;
            write(start);
}




/*----------<<  Function defination for delete >>------------*/

void linear_prob :: delet(int acc_no)
{
            bank b1;
            int rec,i,j,k,start;
            start=hash(acc_no);
            read(start);

            if(b.status==1)
            {
                        cout<<"\n\nRecord not found";
                        return;
            }
            if(b.acc_no==acc_no)
            {
                        if(b.link==-1)
                        {
                                    b.status=1;
                                    write(start);
                                    return;
                        }
                        else
                        {
                                    j=b.link;
                                    read(j);
                                    write(start);
                                    b.status=1;
                                    write(j);
                                    return;
                        }
            }
            else
            {
                        j=start;
                        read(j);
                        while(b.acc_no !=acc_no && b.link !=-1)
                        {
                                    k=j;
                                    j=b.link;
                                    read(j);
                        }
                        if(b.acc_no==acc_no)
                        {
                             int nextlink=b.link;
                             b.status=1;
                             write(j);
                             read(k);
                             b.link=nextlink;
                             write(k);
                        }
                        else
                        {
                                    cout<<"\n\nRecord not found";
                        }
            }
}

/*----------<<  Function defination for search >>------------*/

int linear_prob :: search(int acc_no)
{
            int i,j,start;
            start=hash(acc_no);
            read(start);

            if(hash(b.acc_no)!=start)
            {
                        return(-1);
            }
            j=start;
            while(j!=-1)
            {
                        read(j);
                        if(b.acc_no==acc_no)
                        {
                                    return(j);
                        }
                        j=b.link;
            }
            return(-1);
}

/*-------------<<  Main Function Starts  >>------------*/

void main()
{
            linear_prob lp("Bank.txt");
            int acc_no,ch,rec;
            bank b1;
            clrscr();
            do
            {
                        cout<<"\n\t--------<< MENU >>-------" ;
                        cout<<"\n\t1.Insert";
                        cout<<"\n\t2.Print";
                        cout<<"\n\t3.Delete";
                        cout<<"\n\t4.Search";
                        cout<<"\n\t5.Exit";
                        cout<<"\n\t--------------------------";
                        cout<<"\n\tEnter your choice:";
                        cin>>ch;
                        switch(ch)
                        {
                           case 1:
                                                cout<<"\n\nInsert the record:\n";
                                                cout<<"\nAccount No : ";
                                                cin>>b1.acc_no;
                                                cout<<"\nName : ";
                                                cin>>b1.name;
                                                cout<<"\nBalance : ";
                                                cin>>b1.bal;
                                                lp.insert(b1);
                                                break;

                           case 2:
                                                lp.display();
                                                break;

                           case 3:
                                                cout<<"\nEnter the acc no which want to delete";
                                                cin>>acc_no;
                                                lp.delet(acc_no);
                                                break;

                           case 4:
                                                cout<<"\nEnter the acc no which want to search";
                                                cin>>acc_no;
                                                rec=lp.search(acc_no);
                                                if(rec>=0)
                                                {
                                                            lp.display1(rec);
                                                }
                                                else
                                                {
                                                            cout<<"\nRecord not found";
                                                }
                                                break;

                        }
            }while(ch!=5);
}
/*-------------<<  End Of Main Function >>------------*/






/*-----------------<< OUTPUT SCREEN  >>-------------------*/



        --------<< MENU >>-------
        1.Insert
        2.Print
        3.Delete
        4.Search
        5.Exit
        --------------------------
        Enter your choice:1

Insert the record:

Account No : 14

Name : JABIR

Balance : 30000
        --------<< MENU >>-------
        1.Insert
        2.Print
        3.Delete
        4.Search
        5.Exit
        --------------------------
        Enter your choice:1

Insert the record:

Account No : 12

Name : MANOJ

Balance : 31000
        --------<< MENU >>-------
        1.Insert
        2.Print
        3.Delete
        4.Search
        5.Exit
        --------------------------
        Enter your choice:1

Insert the record:

Account No : 16

Name : RITESH

Balance : 25000
        --------<< MENU >>-------
        1.Insert
        2.Print
        3.Delete
        4.Search
        5.Exit
        --------------------------
        Enter your choice:1

Insert the record:

Account No : 17

Name : JUBER

Balance : 26000
        --------<< MENU >>-------
        1.Insert
        2.Print
        3.Delete
        4.Search
        5.Exit
        --------------------------
        Enter your choice:2

---------<< FILE >>-------
0)EMPTY
1)EMPTY
2)12    MANOJ    31000   -1
3)EMPTY
4)14    JABIR    30000   -1
5)EMPTY
6)16    RITESH    25000   -1
7)17    JUBER    26000   -1
8)EMPTY
9)EMPTY
        --------<< MENU >>-------
        1.Insert
        2.Print
        3.Delete
        4.Search
        5.Exit
        --------------------------
        Enter your choice:1

Insert the record:

Account No : 2

Name : AKSHAY

Balance : 29000
        -------<< MENU >>-------
        1.Insert
        2.Print
        3.Delete
        4.Search
        5.Exit
        --------------------------
        Enter your choice:1

Insert the record:

Account No : 3

Name : SANJAY

Balance : 27000
        --------<< MENU >>-------
        1.Insert
        2.Print
        3.Delete
        4.Search
        5.Exit
        --------------------------
        Enter your choice:2

---------<< FILE >>-------
0)EMPTY
1)EMPTY
2)12    MANOJ    31000   5
3)3    SANJAY    27000   -1
4)14    JABIR    30000   -1
5)2    AKSHAY    29000   -1
6)16    RITESH    25000   -1
7)17    JUBER    26000   -1
8)EMPTY
9)EMPTY
        --------<< MENU >>-------
        1.Insert
        2.Print
        3.Delete
        4.Search
        5.Exit
        --------------------------
        Enter your choice:3

Enter the acc no which want to delete17

        --------<< MENU >>-------
        1.Insert
        2.Print
        3.Delete
        4.Search
        5.Exit
        --------------------------
        Enter your choice:2

---------<< FILE >>-------
0)EMPTY
1)EMPTY
2)12    MANOJ    31000   5
3)3    SANJAY    27000   -1
4)14    JABIR    30000   -1
5)2    AKSHAY    29000   -1
6)16    RITESH    25000   -1
7)EMPTY
8)EMPTY
9)EMPTY
        --------<< MENU >>-------
        1.Insert
        2.Print
        3.Delete
        4.Search
        5.Exit
        --------------------------
        Enter your choice:4

Enter the acc no which want to search14

4)14    JABIR    30000   -1

        --------<< MENU >>-------
        1.Insert
        2.Print
        3.Delete
        4.Search
        5.Exit
        --------------------------
        Enter your choice:5

No comments:

Post a Comment