Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define COMANDO 2
- #define NOME 1024
- #define EMAIL 512
- #define TELEFONE 64
- char comando[COMANDO];
- char nome[NOME];
- char email[EMAIL];
- char telefone[TELEFONE];
- char dominio[EMAIL];
- /*ESTRUTURAS */
- typedef struct node_t {
- char *pnome;
- char *pemail;
- char *ptelefone;
- char *pdominio;
- struct node_t *next;
- struct node_t *prev;
- } Node;
- typedef struct limites {
- Node * head;
- Node * tail;
- }Limites;
- /* hashTable */
- typedef struct el
- {
- char *name;
- Node *enderecolista;
- struct el * next;
- }node_hash;
- node_hash *hashTable[10000]={NULL};
- int hashTableSize=1083;
- int stringToKey(char name[]) /*funcao de hash */
- {
- unsigned int i=0;
- int c,s =0;
- for(i=0;i<strlen(name);i++)
- {
- c=name[i];
- s = s+c;
- }
- return s;
- }
- void addNode(char nome[],Limites * limites)
- {
- int ascii,key;
- node_hash *n, *nl;
- n=(node_hash *)malloc(sizeof(node_hash));
- n->name = malloc(strlen(nome)+1);
- strcpy(n->name,nome);
- n->enderecolista=limites->head;
- n->next = NULL;
- ascii = stringToKey(n->name);
- key = ascii % hashTableSize;
- if(hashTable[key] == NULL)
- {
- hashTable[key]=n;
- }
- else
- {
- for(nl=hashTable[key]; nl->next != NULL; nl=nl->next);
- nl -> next =n;
- }
- }
- Node* searchNode(char nName[])
- {
- int ascii = stringToKey(nName);
- int key = ascii % hashTableSize;
- node_hash *n;
- for (n=hashTable[key];n!=NULL;n=n->next)
- {
- if(strcmp(n->name, nName)==0)
- {
- return n->enderecolista;
- }
- }
- return NULL; /*Retorna null se n existir*/
- }
- void free_hash(node_hash *n)
- {
- free(n->name);
- free(n->enderecolista);
- free(n);
- }
- void deleteNode(char nName[])
- {
- int ascii=stringToKey(nName);
- int key = ascii % hashTableSize;
- node_hash *n, *nl;
- if(strcmp(hashTable[key]->name,nName)==0)
- {
- n = hashTable[key];
- if(hashTable[key]->next!=NULL){
- hashTable[key]=hashTable[key]->next;
- }
- free_hash(n);
- return;
- }
- for(n=hashTable[key];n->next != NULL;n=n->next)
- {
- nl= n->next;
- if(strcmp(nl->name,nName)==0)
- {
- n->next = nl->next;
- free_hash(nl);
- break;
- }
- }
- }
- /* */
- void free_node(Node* node)
- {
- free(node->pnome);
- free(node->pemail);
- free(node->ptelefone);
- free(node->pdominio);
- free(node);
- }
- Limites * add(char nome[], char email[], char telefone[], char dominio[], Limites *limites) {
- Node *new_node;
- new_node =(Node *) malloc(sizeof(Node));
- new_node->pnome=malloc(strlen(nome)+1); /* strlen da me o tamanho do char tb? */
- new_node->pemail=malloc(strlen(email)+1);
- new_node->ptelefone=malloc(strlen(telefone)+1);
- new_node->pdominio=malloc(strlen(dominio)+1);
- strcpy(new_node->pnome,nome);
- strcpy(new_node->pemail,email);
- strcpy(new_node->ptelefone,telefone);
- strcpy(new_node->pdominio,dominio);
- new_node->next= limites->head;
- new_node->prev= NULL;
- if(limites->head != NULL) limites->head->prev= new_node;
- limites->head = new_node;
- if(limites->tail==NULL) limites->tail = new_node;
- printf("head:%s\ntail:%s\n",limites->head->pnome,limites->tail->pnome);
- printf("Phead:%p\nPtail:%p\n",(void*)limites->head,(void*)limites->tail );
- return limites;
- }
- Limites* removen(Limites* limites,Node* del)
- {
- if (limites->head == NULL || del == NULL)
- return limites;
- if(limites->tail==del && del->prev != NULL){
- limites->tail=del->prev;
- limites->tail->next=NULL;
- free_node(del);
- printf("head:%s\ntail:%s\n",limites->head->pnome,limites->tail->pnome);
- printf("Phead:%p\nPtail:%p\n",(void*)limites->head,(void*)limites->tail );
- }
- else if(limites->tail==del && del->prev == NULL){
- limites->head=NULL;
- limites->tail=NULL;
- free_node(del);
- printf("beep\n");
- printf("Phead:%p\nPtail:%p\n",(void*)limites->head,(void*)limites->tail );
- }
- else if(limites->head==del){
- limites->head=del->next;
- limites->head->prev=NULL;
- free_node(del);
- printf("head:%s\ntail:%s\n",limites->head->pnome,limites->tail->pnome);
- printf("Phead:%p\nPtail:%p\n",(void*)limites->head,(void*)limites->tail );
- }
- else{
- del->next->prev=del->prev;
- free_node(del);
- printf("head:%s\ntail:%s\n",limites->head->pnome,limites->tail->pnome);
- printf("Phead:%p\nPtail:%p\n",(void*)limites->head,(void*)limites->tail );
- }
- /*
- if (limites->head == del && del->next != NULL){
- limites->head = del->next;
- printf("1head:%s\ntail:%s\n",limites->head->pnome,limites->tail->pnome);
- }
- if (limites->head == del && del->next == NULL){
- limites->head = NULL;
- printf("2head:%s\ntail:%s\n",limites->head->pnome,limites->tail->pnome);
- }
- if (del->next != NULL){
- del->next->prev = del->prev;
- printf("3head:%s\ntail:%s\n",limites->head->pnome,limites->tail->pnome);
- }
- if (del->prev != NULL){
- del->prev->next = del->next;
- printf("4head:%s\ntail:%s\n",limites->head->pnome,limites->tail->pnome);
- }
- if(del->next == NULL){
- limites->tail->next = NULL;
- limites->tail=limites->tail->prev;
- printf("5head:%s\ntail:%s\n",limites->head->pnome,limites->tail->pnome);
- }*/
- /*free_node(del);*/
- /*printf("6head:%s\ntail:%s\n",limites->head->pnome,limites->tail->pnome);*/
- return limites;
- }
- /*
- Node * removen(char nome[], Node *head) {
- Node *current_node = head;
- Node *prev_node;
- while ( current_node != NULL) {
- if (strcmp(current_node->pnome,nome) == 0) {
- if (current_node == head) {
- head = current_node->next;
- free_node(current_node);
- } else {
- prev_node->next = current_node->next;
- }
- }
- prev_node = current_node;
- current_node = current_node->next;
- }
- return(head);
- } */
- Node* muda_mail(Node* mudar,char email[],char dominio[])
- {
- strcpy(mudar->pemail,email);
- strcpy(mudar->pdominio,dominio);
- return mudar;
- }
- void print(Limites *limite) {
- Node *current_node=limite->tail;
- while ( current_node != NULL) {
- printf("%s ", current_node->pnome);
- printf("%s ", current_node->pemail);
- printf("%s\n", current_node->ptelefone);
- current_node = current_node->prev;
- }
- }
- void print_single(Node* node){
- printf("%s ",node->pnome);
- printf("%s ",node->pemail);
- printf("%s\n",node->ptelefone);
- }
- void conta_dominios(Limites * limite, char dominio[]){
- Node *current_node=limite->tail;
- long int contador=0;
- while(current_node !=NULL){
- if (strcmp(current_node->pdominio,dominio)==0)
- contador++;
- current_node= current_node->prev;
- }
- printf("%s:%ld\n",dominio,contador);
- }
- /* INPUT */
- void ler()
- {
- int i=0;
- int c;
- int len;
- int l=0;
- int k=0;
- int controlo;
- char input_aux[3][NOME];
- char input[COMANDO+NOME+EMAIL+TELEFONE];
- memset(input_aux[0],0,strlen(input_aux[0]));
- memset(input_aux[1],0,strlen(input_aux[1]));
- memset(input_aux[2],0,strlen(input_aux[2]));
- memset(input,0,strlen(input));
- c =getchar();
- while(c!='\n')
- {
- input[i] = c;
- c = getchar();
- i++;
- }
- input[i]='\0';
- len = strlen(input);
- comando[0]=input[0];
- for (i=2;i<len;i++)
- {
- if (input[i]==' ')
- {
- k++;
- l=0;
- }
- else
- {
- input_aux[k][l]=input[i];
- l++;
- }
- }
- strcpy(nome,input_aux[0]);
- strcpy(email,input_aux[1]);
- strcpy(telefone,input_aux[2]);
- if(comando[0]!='e')
- {
- len = strlen(email);
- controlo=0;
- k=0;
- for(i=0;i<len;i++)
- {
- if(controlo)
- {
- dominio[k]=email[i];
- k++;
- }
- if(email[i]=='@') controlo=1;
- }
- }
- else
- {
- len = strlen(nome);
- controlo=0;
- k=0;
- for(i=0;i<len;i++)
- {
- if(controlo)
- {
- dominio[k]=nome[i];
- k++;
- }
- if(nome[i]=='@') controlo=1;
- }
- }
- }
- void limpa_input()
- {
- memset(comando,0,strlen(comando));
- memset(nome,0,strlen(nome));
- memset(email,0,strlen(email));
- memset(telefone,0,strlen(telefone));
- memset(dominio,0,strlen(dominio));
- }
- /* MAIN */
- int main(){
- Limites *limites = (Limites*)malloc(sizeof(Limites));
- int control=1;
- limites->head = NULL;
- limites->tail= NULL;
- printf("Phead:%p\nPtail:%p\n",(void*)limites->head,(void*)limites->tail );
- while(control){
- limpa_input();
- ler();
- switch(comando[0]){
- case 'a':
- if(searchNode(nome)==NULL){
- limites=add(nome,email,telefone,dominio,limites);
- addNode(nome,limites);
- }
- else{
- printf("Nome existente.\n");
- }
- break;
- case 'l':
- print(limites);
- break;
- case 'p':
- if(searchNode(nome)!=NULL)
- {
- print_single(searchNode(nome));
- }
- else{
- printf("Nome inexistente.\n");
- }
- break;
- case 'r':
- if(searchNode(nome)!=NULL)
- {
- removen(limites,searchNode(nome));
- deleteNode(nome);
- }
- else{
- printf("Nome inexistente.\n");
- }
- break;
- case 'e':
- if(searchNode(nome)!=NULL)
- {
- muda_mail(searchNode(nome),email,dominio);
- }
- else{
- printf("Nome inexistente.\n");
- }
- break;
- case 'c':
- conta_dominios(limites,nome);
- break;
- case 'x':
- control=0;
- break;
- }
- }
- return 0;
- }
- /*ERROS:
- quando dou 5 removes da erro na hashtable double free corruption...(problema hash table confirmado)
- quando meto a 0 e adiciono 2 aquilo mete infinito a 2 (head e tail e no meio)(problema da lista???)
- a ppppppppp falha???????????????
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement