Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <math.h>
//this struct is the struct that describes an cell of information, 2 numeric and a string //field
typedef struct dataReg{
char nome[80];
long int nascimento;
int numero;
struct dataReg *prox;
struct dataReg *ant;
}datReg;
//here I have the main tab, with links to the first, actual, and last node of data
typedef struct dataTab{
datReg *prim;
datReg *atual;
datReg *ult;
}datTab;
//the table has 7 entries, (something mod 7)+1 = 7 possible values
datTab tabela[7];
int chave;
//initializes the table
int inicializa(tabela[]){
int i = 0;
for(i = 0; i<=7; i++);
tab[i]->prim = tab[i]->atual = tab[i]->ult = NULL;
}
/*inserts in the table, table = tabela, Ive used a switch of 7 elements cause (something mod 7) +1, will allwys generate a value between 0 and 6, and I have a table of 1 to 7, I know matrix starts at 0 it is easy to correct...*/
int insere(tabela[]){
datReg *aux;
int hashmod;
aux = (datReg *)malloc(sizeof(datReg));
printf("\nNome: ");
scanf("%s", aux->nome);
printf("\nData de Nascimento (DDMMAAAA): ");
scanf("%l", aux->nascimento);
printf("\nNumero: ");
scanf("%d", aux->numero);
aux->prox = aux->ant = NULL;
hashmod = int (fmod(aux->numero,7) + 1);
switch(hashmod){
case 1: {
if(tab[1].prim != NULL){
while(tab[1].atual->prox != NULL)
tab[1].atual = tab[1].atual->prox;
tab[1].atual->prox = aux;
aux->ant = tab[1].atual;
tab[1].ult = aux;
}
else{
tab[1].prim = aux;
tab[1].atual = aux;
tab[1].ult = aux;
}
break;
}
case 2: {
if(tab[2].prim != NULL){
while(tab[2].atual->prox != NULL)
tab[2].atual = tab[1].atual->prox;
tab[2].atual->prox = aux;
aux->ant = tab[2].atual;
tab[2].ult = aux;
}
else{
tab[2].prim = aux;
tab[2].atual = aux;
tab[2].ult = aux;
}
break;
}
case 3: {
if(tab[3].prim != NULL){
while(tab[3].atual->prox != NULL)
tab[3].atual = tab[1].atual->prox;
tab[3].atual->prox = aux;
aux->ant = tab[3].atual;
tab[3].ult = aux;
}
else{
tab[3].prim = aux;
tab[3].atual = aux;
tab[3].ult = aux;
}
break;
}
case 4: {
if(tab[4].prim != NULL){
while(tab[4].atual->prox != NULL)
tab[4].atual = tab[4].atual->prox;
tab[4].atual->prox = aux;
aux->ant = tab[4].atual;
tab[4].ult = aux;
}
else{
tab[4].prim = aux;
tab[4].atual = aux;
tab[4].ult = aux;
}
break;
}
case 5: {
if(tab[5].prim != NULL){
while(tab[5].atual->prox != NULL)
tab[5].atual = tab[5].atual->prox;
tab[5].atual->prox = aux;
aux->ant = tab[5].atual;
tab[5].ult = aux;
}
else{
tab[5].prim = aux;
tab[5].atual = aux;
tab[5].ult = aux;
}
break;
}
case 6: {
if(tab[6].prim != NULL){
while(tab[6].atual->prox != NULL)
tab[6].atual = tab[6].atual->prox;
tab[6].atual->prox = aux;
aux->ant = tab[6].atual;
tab[6].ult = aux;
}
else{
tab[6].prim = aux;
tab[6].atual = aux;
tab[6].ult = aux;
}
break;
}
case 7: {
if(tab[7].prim != NULL){
while(tab[7].atual->prox != NULL)
tab[7].atual = tab[7].atual->prox;
tab[7].atual->prox = aux;
aux->ant = tab[7].atual;
tab[7].ult = aux;
}
else{
tab[7].prim = aux;
tab[7].atual = aux;
tab[7].ult = aux;
}
break;
}
}
}
//removes from the table, chave = value to be removed
int remove(tabela[], int chave){
int hashmod;
datReg *aux;
aux = (datReg *)malloc(sizeof(datReg));
aux->numero = 0;
hashmod = int (fmod(chave,7) + 1);
switch(hashmod){
case 1: {
tab[1]->atual = tab[1]->prim;
aux = tab[1]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
printf("Elemento nao encontrado!!!\n\n\n");
return 0;
}
else
aux = aux->prox;
}
aux->ant->prox = aux->prox;
aux->prox->ant = aux->ant;
free(aux);
break;
}
case 2: {
tab[2]->atual = tab[2]->prim;
aux = tab[2]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
printf("Elemento nao encontrado!!!\n\n\n");
return 0;
}
else
aux = aux->prox;
}
aux->ant->prox = aux->prox;
aux->prox->ant = aux->ant;
free(aux);
break;
}
case 3: {
tab[3]->atual = tab[3]->prim;
aux = tab[3]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
printf("Elemento nao encontrado!!!\n\n\n");
return 0;
}
else
aux = aux->prox;
}
aux->ant->prox = aux->prox;
aux->prox->ant = aux->ant;
free(aux);
break;
}
case 4: {
tab[4]->atual = tab[4]->prim;
aux = tab[4]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
printf("Elemento nao encontrado!!!\n\n\n");
return 0;
}
else
aux = aux->prox;
}
aux->ant->prox = aux->prox;
aux->prox->ant = aux->ant;
free(aux);
break;
}
case 5: {
tab[5]->atual = tab[5]->prim;
aux = tab[5]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
printf("Elemento nao encontrado!!!\n\n\n");
return 0;
}
else
aux = aux->prox;
}
aux->ant->prox = aux->prox;
aux->prox->ant = aux->ant;
free(aux);
break;
}
case 6: {
tab[6]->atual = tab[6]->prim;
aux = tab[6]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
printf("Elemento nao encontrado!!!\n\n\n");
return 0;
}
else
aux = aux->prox;
}
aux->ant->prox = aux->prox;
aux->prox->ant = aux->ant;
free(aux);
break;
}
case 7: {
tab[7]->atual = tab[7]->prim;
aux = tab[7]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
printf("Elemento nao encontrado!!!\n\n\n");
return 0;
}
else
aux = aux->prox;
}
aux->ant->prox = aux->prox;
aux->prox->ant = aux->ant;
free(aux);
break;
}
}
}
//searches the table for an item
int consulta(tabela[], int chave){
int hashmod;
datReg *aux;
aux = (datReg *)malloc(sizeof(datReg));
aux->numero = 0;
hashmod = int (fmod(chave,7) + 1);
switch(hashmod){
case 1: {
tab[1]->atual = tab[1]->prim;
aux = tab[1]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
printf("Elemento nao encontrado!!!\n\n\n");
return 0;
}
else
aux = aux->prox;
}
printf("Numero: %d\nNome: %s\nNascimento: %l\n\n", aux->numero, aux->nome, aux->nascimento);
return 1;
}
case 2: {
tab[2]->atual = tab[2]->prim;
aux = tab[2]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
printf("Elemento nao encontrado!!!\n\n\n");
return 0;
}
else
aux = aux->prox;
}
printf("Numero: %d\nNome: %s\nNascimento: %l\n\n", aux->numero, aux->nome, aux->nascimento);
return 1;
}
case 3: {
tab[3]->atual = tab[3]->prim;
aux = tab[3]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
printf("Elemento nao encontrado!!!\n\n\n");
return 0;
}
else
aux = aux->prox;
}
printf("Numero: %d\nNome: %s\nNascimento: %l\n\n", aux->numero, aux->nome, aux->nascimento);
return 1;
}
case 4: {
tab[4]->atual = tab[4]->prim;
aux = tab[4]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
printf("Elemento nao encontrado!!!\n\n\n");
return 0;
}
else
aux = aux->prox;
}
printf("Numero: %d\nNome: %s\nNascimento: %l\n\n", aux->numero, aux->nome, aux->nascimento);
return 1;
}
case 5: {
tab[5]->atual = tab[5]->prim;
aux = tab[5]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
printf("Elemento nao encontrado!!!\n\n\n");
return 0;
}
else
aux = aux->prox;
}
printf("Numero: %d\nNome: %s\nNascimento: %l\n\n", aux->numero, aux->nome, aux->nascimento);
return 1;
}
case 6: {
tab[6]->atual = tab[6]->prim;
aux = tab[6]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
printf("Elemento nao encontrado!!!\n\n\n");
return 0;
}
else
aux = aux->prox;
}
printf("Numero: %d\nNome: %s\nNascimento: %l\n\n", aux->numero, aux->nome, aux->nascimento);
return 1;
}
case 7: {
tab[7]->atual = tab[7]->prim;
aux = tab[7]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
printf("Elemento nao encontrado!!!\n\n\n");
return 0;
}
else
aux = aux->prox;
}
printf("Numero: %d\nNome: %s\nNascimento: %l\n\n", aux->numero, aux->nome, aux->nascimento);
return 1;
}
}
}
int main(){
int opcao;
while(opcao != 4){
printf("\n\n\n\nOpcoes:\n1: Consultar tabela.\n2: Inserir.\n3: Excluir.\n4: Sair.");
scanf("%c", &opcao);
switch(opcao){
case 1: {
int num;
printf("\nNumero: ");
scanf("%d", &num);
// num = int (fmod(num,7) + 1);
consulta(tabela[],num);
break;
}
case 2: {
insere(tabela[]);
}
case 3: {
int num;
printf("\nNumero: ");
scanf("%d", &num);
remove(tabela[]);
}
case 4: return 0;
default: printf("Escolha outra opcao!!!\n\n\n");
}
}
}
Sorry again, if someone can read portuguese my hope lies on you. Ohhhhhh the main doubt = how can I pass tabela to the functions, I'm 99% sure the problem lies there, sorry again and thx again.