Thursday, May 17, 2012

Prog-11


#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<conio.h>

#include<fstream.h>

#include<iostream.h>

//Record specification

class node

{

 public:char name[15],usn[15];

 node *link;

};



node *h[29];//Array of record pointers

//equal to size of hash keys--29



void insert()

{

 char name[15],usn[15],buffer[50];



 fstream out;



 out.open("student.txt",ios::app);//opening student.txt in append mode



 if(!out)

 {

  cout<<"\nUnable to open the file in append mode";

  getch();

  return;

 }





 cout<<"\nEnter the name = ";

 cin>>name;



 cout<<"\nEnter the usn = ";

 cin>>usn;



 strcpy(buffer,name); //Packing the record onto the file using '|' as

 strcat(buffer,"|");  //a delimiter

 strcat(buffer,usn);

 strcat(buffer,"\n");//'\n' delimiter for the record



 out<<buffer;        // appending the packed record onto the file



 out.close();



}

//Insert record into the hash table

void hash_insert(char name1[],char usn1[],int hash_key)

{



 node *p,*prev,*curr;



 p=new node;           //dynamically allocate space using 'new'



 strcpy(p->name,name1);

 strcpy(p->usn,usn1);

 p->link=NULL;



 prev=NULL;

 curr=h[hash_key];       //getting the hash pointer location



 if(curr==NULL)          // Case: No collision

 {

  h[hash_key]=p;

  return;

 }



 while(curr!=NULL)       // Case : On collision -- Insert at rear end

 {

  prev=curr;

  curr=curr->link;

 }



 prev->link=p;



}



void retrive()

{

 fstream in;

 char name[15],usn[15];

 int j,count;

 node *curr;



 in.open("student.txt",ios::in);     // open the record file in input mode



 if(!in)

 {

  cout<<"\nUnable to open the file in input mode";

  getch();

  exit(0);

 }



 while(!in.eof())

 {

   in.getline(name,15,'|');          //unpacking the record

   in.getline(usn,15,'\n');



   count=0;

   for(j=0;j<strlen(usn);j++)        //Calculate sum of ascii values

   {                                 //of USN

    count=count+usn[j];

   }



   count=count%29;                   // Hash Key = ASCII count% 29



   hash_insert(name,usn,count);



 }



 cout<<"\nEnter the usn = ";

 cin>>usn;



 count=0;

 for(j=0;j<strlen(usn);j++)           // Calculating Hash Key

 count=count+usn[j];



 count=count%29;



 curr=h[count];



 if(curr==NULL)

 {

  cout<<"\nRecord not found";

  getch();

  return;

 }



 do

 {

  if(strcmp(curr->usn,usn)==0)                            //When the record is

  {                                                       //found, retrieve

   cout<<"\nRecord found : "<<curr->usn<<" "<<curr->name; //USN and name

   getch();

   return;

  }

  else

  {

   curr=curr->link;

  }

 }while(curr!=NULL);                 //Search till end of list



 if(curr==NULL)                      //End of list reached with no record found

 {

  cout<<"\nRecord not found";

  getch();

  return;

 }



}



void main()

{



 int choice;

 clrscr();

 fstream out;

 out.open("student.txt",ios::out);

 if(!out)

 {

  cout<<"\nUnable to open the file in out mode";

  getch();

  exit(0);

 }



 for(;;)

 {

  cout<<"\n1:insert";

  cout<<"\n2:retrive";

  cout<<"\n3:exit";

  cout<<"\nEnter the choice = ";

  cin>>choice;

  switch(choice)

  {

   case 1:insert();break;

   case 2:retrive();break;

   case 3:exit(0);

   default:cout<<"\nInvalid option";

  }

 }

}

No comments:

Post a Comment