c - 列表矢量打印不起作用
问题描述
我正在做一个哈希表,其中我有一个向量,其中每个节点内都有一个列表。但是当我去打印时,它不会出现列表中的项目,如果我尝试在列表中放置多个元素,它会在第二个中给出分段失败。下面是代码和结果:
#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)
如果你能帮忙,谢谢。
解决方案
您在这里至少有一个严重的问题:
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);
您答案中的代码会产生未定义的行为。在其他作品中,它可能会起作用。谷歌“未定义的行为”。
您的代码中的其他地方可能存在更多问题。
推荐阅读
- java - 如何在 gradle 中创建文件集合的集合?
- asp.net - 如何在 ASP.NET Core Razor 视图中防止 XSS 攻击?
- python - 如何在 matplotlib 中使子图中的轴字体大小相似?
- ios - 在 iOS 14 中,QML 文本对象在逗号或句点后缺少空格
- php - 如何从 woocommerce 上的标签云中删除空产品标签
- elasticsearch - elasticsearch - 使用查找表值标记数据
- amazon-cloudformation - 是否可以在 Cloud-formation 中的 ECS 容器定义之间共享秘密?
- r - 列作为命名列表
- fortran - Fortran的hello world程序中的'*'是什么意思?
- python - 烧瓶没有得到饼干