Tuesday, September 3, 2013

A.Implement Direct Access File Using Linear Probing Method Of Collision Resolution


/*****************************************************
  NAME                      :  JABIR DAUD PATHAN
  PROGRAM            :  Implement Direct Access File Using Linear
                                       Probing 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;
};

/*----------<<  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\n"<<i<<")"<<b.acc_no<<"    "<<b.name<<"    "<<setprecision(2)<<b.bal;
            }
            else
                        cout<<"\n\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;
            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,start;
            b1.status=0;
            start=hash(b1.acc_no);
            for(i=0;i<size;i++)
            {
                        j=(start+i)%size;
                        read(j);
                        if(b.status==1)
                        {
                                    b=b1;
                                    write(j);
                                    return;
                        }
            }
            cout<<"\nTable is Full..." ;
}

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

void linear_prob :: delet(int acc_no)
{
            bank b1;
            int rec;
            rec=search(acc_no);

            if(rec>=0)
            {
                        read(rec);
                        b.status=1;
                        write(rec);
            }
            else
            {
                        cout<<"\nRecord not found";
            }
}

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

int linear_prob :: search(int acc_no)
{
            int i,j,start;
            start=hash(acc_no);
            for(i=0;i<size;i++)
            {
                        j=(start+i)%size;
                        read(j);
                        if(b.status==0 && b.acc_no==acc_no)
                        {
                                    return(j);
                        }
            }
            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:10

Name:RITESH

Balance:20000

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

Insert the record:

Account No:11

Name:JABIR

Balance:30000

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

---------<< FILE >>-------

0)10    RITESH    20000

1)11    JABIR    30000

2)EMPTY

3)EMPTY

4)EMPTY

5)EMPTY

6)EMPTY

7)EMPTY

8)EMPTY

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

Insert the record:

Account No:21

Name:AKASH

Balance:25000

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

---------<< FILE >>-------

0)10    RITESH    20000

1)11    JABIR    30000

2)21    AKASH    25000

3)EMPTY

4)EMPTY

5)EMPTY

6)EMPTY

7)EMPTY

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 delete21

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

---------<< FILE >>-------

0)10    RITESH    20000

1)11    JABIR    30000

2)EMPTY

3)EMPTY

4)EMPTY

5)EMPTY

6)EMPTY

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 search11

1)11    JABIR    30000

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

No comments:

Post a Comment