首页 > 解决方案 > 列表矢量打印不起作用

问题描述

我正在做一个哈希表,其中我有一个向量,其中每个节点内都有一个列表。但是当我去打印时,它不会出现列表中的项目,如果我尝试在列表中放置多个元素,它会在第二个中给出分段失败。下面是代码和结果:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
enum erro {
     semErro = 0, posInv = 1, listaCheia = 2, listaVazia = 3, existe         = 4, naoExiste = 5
};

typedef struct no{       
    char nome[100];
    char telefone[20];
    struct no *prox;
} nodo;

typedef struct{
    int tamanho;
    nodo *inicio;
} lista;

typedef struct { 
    int tamanhoTabelaHash;
    int colisoes;
    lista* vetor;
} tabela;

///////////////////Chamada de Funções///////////////////
lista *novaLista();
tabela *criaTabela(int tam);
int insereTabela(tabela *tabela, char *entraNome, char *entraTelefone);
int insereNoInicioI(lista *lista,char *entraNome, char *entraTelefone);
int hash1(char *entraNome, int tam);
void imprime(lista *lista);
void imprimeTabela(tabela *tabela);
///////////////////Funções Lista///////////////////

void imprime(lista *lista){        
     int i;
     nodo *no; //<<<<<Possível local do erro>>>>>
     puts("Lista: \n");
     for (i = 0; i < lista->tamanho; i++) {  
          printf("Nome: %s Telefone: %s\n",no->nome,no->telefone);
          no=no->prox;
     }
}

lista *novaLista(){ 

     lista *l = (lista*)malloc(sizeof(lista));
     l->tamanho=0;
     l->inicio=NULL;
     return l;
}

int insereNoInicioI(lista *lista,char *entraNome, char *entraTelefone){ 

     nodo *novo=(nodo *)malloc(sizeof(nodo));    
     strcpy(novo->nome,entraNome);
     strcpy(novo->telefone,entraTelefone);
     novo->prox = lista->inicio;
     lista->inicio = novo;        
     lista->tamanho++;
     return semErro;
}

tabela *criaTabela(int tam) {

    if( tam < 1 ) return NULL;
    tabela *table = (tabela *)malloc(sizeof(tabela));
    table->tamanhoTabelaHash = tam;
    table->colisoes = 0;
    for (int i = 0; i < 10; i++) {   
        table[i].vetor = NULL;
    }
    return table;   
}

void imprimeTabela(tabela *tabela) {

    int i;
    printf("\nTabela: \n");
    for (i = 0; i < tabela->tamanhoTabelaHash ; i++) {
        printf("\nindice[%d]:", i);
            if(tabela[i].vetor!=NULL){
               imprime(tabela[i].vetor);
            }  
    }
}

int insereTabela(tabela *tabela, char *entraNome, char *entraTelefone){

    int pos = 0;
        pos = hash1(entraNome,10000);//Função que retorna uma posição( no caso 8 para o primeiro nome e 6 para o segundo e terceiro
    }
    if(tabela[pos].vetor==NULL){
        lista *list = novaLista();
        tabela[pos].vetor = list;
    }
    nodo *novo=(nodo *)malloc(sizeof(nodo));
    insereNoInicioI(tabela[pos].vetor, entraNome, entraTelefone);
    return semErro;
} 

int main(){
   setlocale(LC_ALL, "Portuguese");
   tabela *table = criaTabela(10);

   char nome[100] = "Maria Cláudia Feliz";
   char telefone[20] = "(53)98401-8583";
   char nome1[100] = "Everton Almeida";
   char telefone1[20] = "(53)90000-8583";
   char nome2[100] = "Everton Almeida";
   char telefone2[20] = "(53)90000-8583";

   insereTabela(table,nome,telefone);
   insereTabela(table,nome1,telefone1);
   insereTabela(table,nome2,telefone2);
   imprimeTabela(table);
return semErro;
} 
Resultado:
Tabela: 
indice[0]: 
indice[1]:
indice[2]: 
indice[3]: 
indice[4]: 
indice[5]: 
indice[6]:Lista: 
Nome:  Telefone:  //<<<<Deveria imprimir o nome e telefone>>>>
Nome:  Telefone:  //<<<<Deveria imprimir o nome e telefone>>>>
indice[7]: 
indice[8]:Lista:
                  //<<<<Deveria imprimir o nome e telefone>>>>
Falha de segmentação(imagem do núcleo gravada)

如果你能帮忙,谢谢。

标签: clisthashtable

解决方案


您在这里至少有一个严重的问题:

tabela *criaTabela(int tam) {
  if (tam < 1) return NULL;

  // you allocate space for 1 tabela
  tabela *table = (tabela *)malloc(sizeof(tabela));
  table->tamanhoTabelaHash = tam;
  table->colisoes = 0;

  // but here you write to 10 tabelas ....
  for (int i = 0; i < 10; i++) {
    table[i].vetor = NULL;
  }
  return table;
}

如果你写 10 个 tabelas,你应该为 10 个 tabelas 分配空间:

  tabela *table = (tabela *)malloc(sizeof(tabela) * 10);

您答案中的代码会产生未定义的行为。在其他作品中,它可能起作用。谷歌“未定义的行为”

您的代码中的其他地方可能存在更多问题。


推荐阅读