Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- /* Nome: Seu nome aqui
- * RA: Seu RA aqui
- * Laboratorio 09 - Hash */
- /* STRUCS, DEFINIÇÕES E CABEÇALHOS DAS SUAS FUNÇÕES DA TAD */
- typedef struct Aluno{
- int ra;
- char name[21];
- } aluno;
- typedef struct No{
- struct Aluno *info;
- struct No *next;
- } lista;
- // assinatura de outras funções
- int index(char name[]);
- int busca(lista hashtable[], char name[]);
- void insert(lista hashtable[], int ra, char name[]);
- void remove(lista hashtable[], char name[]);
- /* SEU PROGRAMA - MAIN */
- int main() {
- int ra, i, index, searchAux;
- char input[9], name[21];
- lista hashtable[13];
- for(i=0; i<13; i++){
- hashtable[i].next = NULL;
- hashtable[i].info = NULL;
- }
- while(1) {
- scanf("%s", input);
- if(!strcmp(input,"sair"))
- break;
- else if(!strcmp(input,"insere")){
- scanf("%d \"%[^\n\"]s", &ra, name);
- if( busca(hashtable, name) )
- printf("Nome repetido: \"%s\"\n", name);
- else {
- printf("Insere: %d \"%s\"\n", ra, name);
- insert(hashtable, ra, name);
- }
- }
- else if(!strcmp(input,"remove")){
- scanf("\"%[^\n\"]s", name);
- printf("Remove: \"%s\"\n", name);
- if( !busca(hashtable, name) )
- printf("Nome inexistente: \"%s\"\n", name);
- else {
- printf("Remove: \"%s\"\n", name);
- remove(hashtable, name);
- }
- }
- else if(!strcmp(input,"consulta")){
- scanf("\"%[^\n\"]s", name);
- printf("Consulta: \"%s\"\n", name);
- searchAux = busca(hashtable, name);
- if(searchAux)
- printf("%d \"%s\"\n", searchAux, name);
- else
- printf("Nome inexistente: \"%s\"\n", name);
- }
- else if(!strcmp(input,"imprime")){
- printf("%s %d\n", name);
- }
- }
- return 0;
- }
- /* IMPLEMENTAÇÃO DAS FUNÇÕES DE SUA TAD */
- int hash(char name[]){
- int XOR, i;
- XOR = name[0];
- for(i=1; name[i] != '\0'; i++)
- XOR ^= name[i];
- return XOR%13;
- }
- int busca(lista hashtable[], char name[]){
- int i, index;
- index = hash(name);
- lista *aux = &hashtable[index];
- printf("Busca iniciada\n");
- if(aux->info){
- printf("Existe Informacao...\n");
- if(!strcmp(aux->info->name, name))
- return aux->info->ra;
- }
- while(aux->next){
- if(aux->info){
- printf("Existe Informacao...\n");
- if(!strcmp(aux->info->name, name))
- return aux->info->ra;
- }
- else
- return 0;
- aux = aux->next;
- }
- return 0;
- }
- void insert(lista hashtable[], int ra, char name[]){
- int index;
- index = hash(name);
- lista *aux = &hashtable[index];
- lista *novoAluno = NULL;
- printf("Iniciando insercao\n");
- if(!(aux->info)){
- printf("Nao existia informacao nesse XOR\n");
- aux->info = (aluno*)malloc(sizeof(aluno));
- aux->info->ra = ra;
- strcpy(aux->info->name, name);
- printf("O slot %d agora tem o nome %s\n", index-1, hashtable[index].info->name);
- return;
- }
- while(aux->next){
- if(strcmp(aux->next->info->name, name)<0)
- aux = aux->next;
- }
- novoAluno = (lista*)malloc(sizeof(lista));
- novoAluno->next = aux->next;
- aux->next = novoAluno;
- novoAluno->info = (aluno*)malloc(sizeof(aluno));
- novoAluno->info->ra = ra;
- strcpy(novoAluno->info->name, name);
- return;
- }
- void remove(lista hashtable[], char name[]){
- int index;
- index = hash(name);
- lista *aux = hashtable[index];
- if(!strcmp(aux->info->name, name){
- }
- else{
- while(strcmp(aux->info->name, name)){
- aux = aux-> next;
- printf("iterando...\n");
- }
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement