-
Notifications
You must be signed in to change notification settings - Fork 1
/
ohash.c
85 lines (82 loc) · 1.9 KB
/
ohash.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
#include<stdio.h>
#include<stdlib.h>
#define max 10
void insert(int n);
int search(int n);
int display();
typedef struct node{
int data;
struct node* link;
}node;
node* arr[max];
int main(void){
for(int i=0;i<max;i++) arr[i]=NULL;
int ty,k,t;
printf("\tMENU\n");
while(1){
printf("1.INSERT\n2.SEARCH\n3.TRAVERSE\n");
scanf("%d",&ty);
switch(ty){
case 1:{
k=0;
printf("Enter the element to be Inserted\n");
scanf("%d",&t);
if(search(t)!=-1) printf("The element already Exists\n");
else insert(t);
break;
}
case 2:{
printf("Enter the element to be searched\n");
scanf("%d",&t);
k=search(t);
if(k==-1) printf("Element not found\n");
else printf("Element found at index %d\n",k);
break;
}
case 3:{
k=display();
if(k==-1) printf("Array empty");
printf("\n");
break;
}
}
}
}
void insert(int n){
int mod=n%10;
node* newnode=malloc(sizeof(node*));
newnode->data=n;
newnode->link=NULL;
node* temp=arr[mod];
if(temp==NULL) arr[mod]=newnode;
else {
while(temp->link!=NULL) temp=temp->link;
temp->link=newnode;
}
}
int search(int n){
int mod=n%10;
node* temp=arr[mod];
while(temp!=NULL){
if(temp->data==n) return mod;
temp=temp->link;
}
return -1;
}
int display(){
int i=0,flag=0;
node* temp;
while(i<max){
temp=arr[i];
printf("[%d] ",i);
while(temp!=NULL){
printf("%d -->",temp->data);
flag=1;
temp=temp->link;
}
printf("\n");
i++;
}
if(flag==1) return 1;
else return -1;
}