forked from NicoAN42/Stock-Offer-List
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHashing
More file actions
148 lines (134 loc) · 2.74 KB
/
Hashing
File metadata and controls
148 lines (134 loc) · 2.74 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 5
struct Data{
char name[100];
int age;
struct Data *prev, *next;
}*curr;
struct LL{
struct Data *head, *tail;
};
LL table[5];
int hash(char name[]){
return strlen(name) % SIZE;
}
struct Data *newData(char name[], int age){
struct Data *temp = (struct Data*)malloc(sizeof(struct Data));
strcpy(temp->name, name);
temp->age = age;
temp->prev = temp->next = NULL;
return temp;
}
void push(char name[], int age){
int index = hash(name);
struct Data *temp = newData(name, age);
if(!table[index].head){
table[index].head = table[index].tail = temp;
}
else{
table[index].tail->next = temp;
temp->prev = table[index].tail;
table[index].tail = temp;
}
}
struct Data* search(char name[]){
int index = hash(name);
curr = table[index].head;
while(curr){
if(strcmp(curr->name, name) == 0){
return curr;
}
curr = curr->next;
}
return NULL;
}
void pop(char name[]){
int index = hash(name);
struct Data *popData = search(name);
if(!popData){
printf("Data is not found!\n");
return;
}
struct Data deleted = *popData;
//data di head
if(!table[index].head->next){
free(table[index].head);
table[index].head = table[index].tail = NULL;
}
else if(table[index].head == popData){
table[index].head = table[index].head->next;
free(table[index].head->prev);
table[index].head->prev = NULL;
}
else if(table[index].tail == popData){
table[index].tail = table[index].tail->prev;
free(table[index].tail->next);
table[index].tail->next = NULL;
}
else{
//prev popData next
popData->prev->next = popData->next;
popData->next->prev = popData->prev;
free(popData);
}
}
void update(char name[], int age){
struct Data *temp = search(name);
if(!temp){
printf("Data is not found!\n");
return;
}
temp->age = age;
}
void view(){
for (int i = 0; i < SIZE; i++){
printf("%d -> ", i);
curr = table[i].head;
while(curr){
printf("\t%s %d\n", curr->name, curr->age);
curr = curr->next;
}
puts("");
}
}
int main(){
char name[100];
int menu;
int age;
do{
system("cls");
view();
printf ("1. Push\n"
"2. Pop\n"
"3. Update\n"
"4. Exit\n");
do{
printf(">> ");
scanf("%d", &menu); getchar();
}while(menu<1||menu>4);
switch(menu){
case 1:
printf("Insert name : ");
scanf("%[^\n]", &name); getchar();
printf("Insert age : ");
scanf("%d", &age); getchar();
push(name, age);
break;
case 2:
printf("Insert name : ");
scanf("%[^\n]", &name); getchar();
pop(name);
break;
case 3:
printf("Insert name : ");
scanf("%[^\n]", &name); getchar();
printf("Insert age : ");
scanf("%d", &age); getchar();
update(name, age);
break;
}
}while(menu!=4);
return 0;
}