CHAINING - else...

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> #include <stdlib.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;    for(i=0;i<10;++i) hsh[i]=NULL;    clrscr();    do{ if(x>=0) {  clrscr();  printf("           HASH TABLE \n\n");
Background image of page 1

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

View Full DocumentRight Arrow Icon
 for(i=0;i<10;++i)  {printf("\n");   if(hsh[i]==NULL)printf("hsh[%d]--->NULL\n",i);
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: else {printf(&quot;hsh[%d]---&gt;&quot;,i); ptr=hsh[i]; while(ptr!=NULL) { printf(&quot;%d--&gt;&quot;,ptr-&gt;data); ptr=ptr-&gt;next; } printf(&quot;NULL\n&quot;); } } printf(&quot;Enter a number to stop enter -1\n&quot;); scanf(&quot;%d&quot;,&amp;x); hashsearch(hsh,x,10); } } while(x&gt;=0); printf(&quot;Element to be searched\n&quot;); scanf(&quot;%d&quot;,&amp;x); k=hashsearch(hsh,x,10); if(flag==1)printf(&quot;%d originally present at %u\n&quot;,x,k); else printf(&quot;%d is added into the chain at %u\n&quot;,x,k); getch(); } OUTPUT: HASH TABLE hsh[0]---&gt;NULL hsh[1]---&gt;NULL hsh[2]---&gt;2--&gt;12--&gt;NULL hsh[3]---&gt;NULL hsh[4]---&gt;4--&gt;NULL hsh[5]---&gt;NULL hsh[6]---&gt;6--&gt;46--&gt;NULL hsh[7]---&gt;NULL hsh[8]---&gt;8--&gt;NULL hsh[9]---&gt;NULL Enter a number to stop enter -1-1 Element to be searched 46 46 originally present at 2060...
View Full Document

Page1 / 3

CHAINING - else...

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