-
Notifications
You must be signed in to change notification settings - Fork 0
/
week10-1.c
97 lines (91 loc) · 1.64 KB
/
week10-1.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
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node * next;
};
void printTable(struct node table[]){
int i;
for(i=0;i<10;i++){
if(table[i].data!=-1){
struct node * curr=&table[i];
while(curr){
printf("%d ",curr->data);
curr=curr->next;
}
}
printf("\n");
}
}
void Search(struct node table[],int value){
int hash_key=value%10;
if(table[hash_key].data==-1){
printf("element not found\n");
}
else{
struct node * curr=&table[hash_key];
while(curr){
if(curr->data==value){
printf("Element found\n");
return;
}
curr=curr->next;
}
if(curr==NULL){
printf("Element not found\n");
}
}
}
void Delete(struct node table[],int value){
int hash_key=value%10;
struct node * curr=&table[hash_key];
if(curr->data==value){
*curr=*(curr->next);
}
else{
while(curr){
if(curr->next->data==value){
curr->next=curr->next->next;
break;
}
curr=curr->next;
}
}
}
void Insert(struct node table[],int value){
int hash_key=value%10;
if(table[hash_key].data==-1){
table[hash_key].data=value;
}
else{
struct node * curr=&table[hash_key];
while(!(curr->next==NULL)){
curr=curr->next;
}
struct node * temp=(struct node * )malloc(sizeof(struct node));
temp->data=value;
temp->next=NULL;
curr->next=temp;
}
}
int main(){
struct node table[10];
int i;
for(i=0;i<10;i++){
table[i].data=-1;
table[i].next=NULL;
}
Insert(table,71);
Insert(table,23);
Insert(table,73);
Insert(table,99);
Insert(table,44);
Insert(table,19);
Insert(table,49);
Insert(table,93);
Insert(table,81);
Insert(table,39);
Delete(table,19);
//Search(table,44);
printTable(table);
}