This repository has been archived by the owner on Feb 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_functions.c
88 lines (80 loc) · 2.15 KB
/
main_functions.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
/*
* File: main_functions.c
* Author: Nelson Trindade
* IST Number: 93743
* Description: Principal functions used in the switch of main.c.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "main_functions.h"
#include "functions.h"
/*_______________/----- MAIN FUNCTIONS -----\_______________*/
/* Function that adds a contact, by creating a node in the linked list
and on the hashtable */
void command_a(char name[], char email[], char phone[]){
link node_aux = NULL;
int i=hashcode(name);
if (search_hash(i,name)==NULL){
node_aux = create_node(name,email,phone);
alloc_node(node_aux);
alloc_hash(i,node_aux);
}
else
puts("Nome existente.");
}
/* Prints all elements of the linked list */
void command_l(){
list_contact();
}
/* Searches and prints the contact that is associated with the name given */
void command_p(char name[]){
int i=hashcode(name);
hash element=search_hash(i, name);
if (element!=NULL)
print_contact(element->node->contact);
else
puts("Nome inexistente.");
}
/* Removes the contact associated with the name given */
void command_r(char name[]){
int i=hashcode(name);
hash element=search_hash(i, name);
if (element!=NULL){
decrease_domain(element->node->contact->domain);
deleteNode(element->node);
deleteHASH(i,element);
}
else
puts("Nome inexistente.");
}
/* Changes the email of a contact,
by giving the name of the contact and the new email */
void command_e(char name[],char email[]){
int i=hashcode(name);
hash element=search_hash(i, name);
if (element!=NULL){
decrease_domain(element->node->contact->domain);
change_email(element->node->contact,email);
}
else
puts("Nome inexistente.");
}
/* Shows how many specific domains exists in the all contacts */
void command_c(char email[]){
int i=hashcode(email);
Counter_domain count_aux=search_domain(i,email);
if (count_aux!=NULL)
printf("%s:%d\n",count_aux->domain,count_aux->count);
else
printf("%s:%d\n",email,0);
}
/* Exits the program and give free's to every malloc made */
void command_x(){
int i=0;
freeNODE();
for (;i<TABLESIZE;i++){
freeHASH(i);
freeDOM(i);
}
}