Chaining - printf"hsh%d>",i ptr=hsh[i...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
#include <stdio.h> #include <conio.h> /* RISHI AGARWAL         HASHING : CHAINING    ROLL NO 02    SE CMPN A */ typedef struct node {  int data;  struct node *next; }NODE; int flag; NODE * hashsearch( NODE * a[],int x,int n) {   NODE  * ptr,*prev,*p;   int index;   flag=0;   index=x%n;   ptr=a[index];prev=NULL;   while(ptr!=NULL)   {    if(x==ptr->data)    {flag =1;return ptr;}    else    {     prev=ptr;     ptr=ptr->next;    }   }   p=(NODE   *)malloc(sizeof(NODE));   p->data=x;   p->next=NULL;   if(prev==NULL)    a[index]=p;   else    prev->next=p;   return p;  }  void main()  {    NODE  * hsh[10],*ptr,*k;    int i,x;    clrscr();    for(i=0;i<10;++i)     hsh[i]=NULL;    do    {
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
    if(x>=0)     {      clrscr();      printf("  HASH TABLE \n");      for(i=0;i<10;++i)      {       if(hsh[i]==NULL)        printf("hsh[%d]--->NULL\n",i);       else
Background image of page 2
Background image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: { printf("hsh[%d]--->",i); ptr=hsh[i]; while(ptr!=NULL) { printf("%d-->",ptr->data); ptr=ptr->next; } printf("NULL\n"); } } printf("Enter a number to stop enter -1\n"); scanf("%d",&x); hashsearch(hsh,x,10); } }while(x>=0); printf("Element to be searched:"); scanf("%d",&x); k=hashsearch(hsh,x,10); if(flag==1) printf("%d is present\n",x); else printf("%d is added into the chain\n",x); getch(); } OUTPUT: HASH TABLE hsh[0]--->NULL hsh[1]--->NULL hsh[2]--->12-->42-->NULL hsh[3]--->NULL hsh[4]--->34-->NULL hsh[5]--->65-->95-->NULL hsh[6]--->76-->NULL hsh[7]--->NULL hsh[8]--->NULL hsh[9]--->89-->NULL Enter a number to stop enter -1 -1 Element to be searched:76 76 is present...
View Full Document

{[ snackBarMessage ]}

Page1 / 3

Chaining - printf"hsh%d>",i ptr=hsh[i...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online