-
Notifications
You must be signed in to change notification settings - Fork 0
/
HASH.C
executable file
·126 lines (97 loc) · 1.94 KB
/
HASH.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
Program Purpose : To Perform the Hashing technique in the search
Techniques.
Programed By : Devendra Walanj
Subject : Analysis of Algorithms
Semister : IV
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define HASHVAL 23
typedef struct node
{
struct node *link;
int data;
}node;
node * getnode()
{
return (node*)malloc(sizeof(node));
}
void hashing (int data_arr[],node *h[],int n)
{
int i,v;
node *ptr,*p;
for (i=0;i<n;i++)
{
v = data_arr[i] % HASHVAL;
p = getnode();
p->data = data_arr[i];
p->link = NULL;
if(h[v]==NULL)
h[v] = p;
else
ptr = h[v];
while(ptr->link!=NULL)
ptr = ptr->link;
ptr->link = p;
}
}
int hashsear (node *h[],int no)
{
node *ptr;
int v;
int count = 0;
v = no % HASHVAL;
if(h[v]==NULL)
return -1;
for (ptr=h[v];ptr!=NULL;ptr=ptr->link)
{
count++;
if(ptr->data==no)
return count;
}
return -1;
}
void main()
{
int data_arr[] = {0,1,2,3,4,5,6,7,8,9,10,15,23,67,90,112,122,245,687,878};
node *h[HASHVAL] = {NULL};
int x,n,i;
char ch;
int sch = 0;
clrscr();
n = 20;
printf("Original Array Data : \n");
for(i=0;i<20;i++)
printf("%d ",data_arr[i]);
hashing (data_arr,h,n) ;
while(!(ch=='n'))
{
printf("\nEnter the Element for Search : ");
fflush(stdin);
scanf("%d",&sch);
x = hashsear(h,sch);
if(x!=-1)
printf("Data Found. \n\n ",x);
else
printf("Data Not Found. ");
printf("\nDo You want to continue (y,n)? : ");
fflush(stdin);
scanf("%c",&ch);
}
printf("\nPress Any Key to continue....");
getch();
}
/*
OUTPUT :
Original Array Data :
0 1 2 3 4 5 6 7 8 9 10 15 23 67 90 112 122 245 687 878
Enter the Element for Search : 12
Data Not Found.
Do You want to continue (y,n)? : y
Enter the Element for Search : 112
Data Found.
Do You want to continue (y,n)? : n
Press Any Key to continue....
*/