Chained Hash Tables

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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <stdio.h>
#include <stdlib.h>
#include"list.h"
#include<string.h>
#include<stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
List * make_bucket(int *size);
void find_node(List *list,int size);
void auto_data(List* list,int size);
int list_menu(void) {
 
    int i;
 
    do {
 
        printf("\n\nLinked List Menu\n");
 
        printf("1. Insert node\n");
 
        printf("2. Display all node\n");
        
        printf("3. find node\n");
 
        printf("4. auto data\n");
    
        printf("0. Quit\n");
 
        printf("Input Operation : ");
 
        scanf("%d"&i);
 
    } while(i<0 || i>10);
 
    return i;
 
}
 
void start(List* hash,int size) {
 
    int i;
 
    void *data;
 
 
    while((i=list_menu())!=0) {
 
        switch(i) {
 
 
            case 1 :
                isert_node(hash,size);
            break;
 
            case 2 :
                for(i=0;i<size;i++)
                {
                    printf("\n>>hash[%d]>> ",i);
                    if((hash+i)->head==NULL)
                    {
                        printf("- empty",i);
                        continue;
                    }
                
                    print_list(hash+i);    
                }
            break;
            case 3 :
                find_node(hash,size);
            break;
            case 4:
                auto_data(hash,size);
                break;
        }
 
    }
    exit(0);
 
}
 
 
void print_list(List *list) {
    
    ListElmt *element;
    element=list_head(list);
 
    while(element){
        
        printf("%s  ",list_data(element));
 
        element=element->next;
    }
}
 
void main(void) {
 
 
    List *hash;
    int size=0;
    
    hash=make_bucket(&size);
    start(hash,size);
 
}
List * make_bucket(int *size)
{
    int i;
    printf("#버킷 크기 입력 :");
    fflush(stdin);
    scanf("%d",size);
    
    List * n_hash=(List *)malloc(sizeof(List)*(*size));
    
    for(i=0;i<*size;i++)
    {
        list_init(n_hash+i,free);
    }
    printf("\n#Bucket[%d]\n",*size);
    return n_hash;
    
}
 
void isert_node(List* list,int size)
{
    char temp[100];
    char * str;
 
        printf(">Input data : ");
        fflush(stdin);
        gets(temp); 
        str = (char *)malloc(sizeof(temp)+1);
        strcpy(str,temp);
    
    
    int num = rand()%size;
    
    if(list[num].tail==NULL)
    {
        if(list_ins_next(list+num, NULL, str)==-1)
        {
            printf("no data\n");
            return;
        }
    }
    else
    {
            if(list_ins_next(list+num, list[num].tail, str)==-1)
        {
            printf("no data\n");
            return;
        }
    }
    return;    
}
 
void auto_data(List* list,int size)
{
    char temp[100];
    char * str;
    int input,i;
    int num;
        printf(">Input data 개수 : ");
        fflush(stdin);
        scanf("%d",&input);
 
 
        for(i=0;i<input;i++)
        { 
            str = (char *)malloc(sizeof(temp)+1);
            strcpy(str,itoa(rand()%1000,temp,10));
        
        num = atoi(str)%size;
 
    
    if(list[num].tail==NULL)
    {
        if(list_ins_next(list+num, NULL, str)==-1)
        {
            printf("no data\n");
            return;
        }
    }
    else
    {
            if(list_ins_next(list+num, list[num].tail, str)==-1)
        {
            printf("no data\n");
            return;
        }
    }
}
    return;    
}
 
void find_node(List *list,int size){
    int key,count=1;
    
    char temp[100];
    char * str;
 
        printf(">Input data : ");
        fflush(stdin);
        gets(temp); 
        str = (char *)malloc(sizeof(temp)+1);
        strcpy(str,temp);
        
    key=str[0]%size;
    ListElmt *element=list[key].head;
    
    while(1){
        if(!strcmp((char*)element->data,str)){
            printf("# data는 hash[%d]의 %d번째 노드에 있습니다.\n\n",key,count);
            return ;    
        }
        if(element->next==NULL){
            printf("# not find...\n");
            return ;
        }
        element=list_next(element);
        count++;
    }
}
 
cs


'자료구조' 카테고리의 다른 글

[자료구조_Day5]biTree,bsTree  (0) 2018.11.06
[자료구조_Day3]Stack  (0) 2018.11.06
[자료구조_Day3]Set  (0) 2018.11.06
[자료구조_Day2]Double Circular Linked List  (0) 2018.10.28
[자료구조_Day1]Double Linked List  (0) 2018.10.28

+ Recent posts