C 在链表中输入数据
我需要将邮件的每个字符都插入到一个链接列表中,但我不知道哪里出错了 我的分配是正确的,但在打印消息时,给出了错误 有人能帮我吗 As结构C 在链表中输入数据,c,linked-list,C,Linked List,我需要将邮件的每个字符都插入到一个链接列表中,但我不知道哪里出错了 我的分配是正确的,但在打印消息时,给出了错误 有人能帮我吗 As结构 struct mensagem{ char msg; char chave; }; struct elemento{ struct mensagem dados; struct elemento *prox; }; typedef struct elemento* Lista; typedef struct elemento
struct mensagem{
char msg;
char chave;
};
struct elemento{
struct mensagem dados;
struct elemento *prox;
};
typedef struct elemento* Lista;
typedef struct elemento Elem;
函数-生成列表、插入元素和打印列表
Lista* cria_lista(){
Lista* li = (Lista*) malloc(sizeof(Lista));
if(li != NULL)
*li = NULL;
return li;
}
int insere_lista_final(Lista* li, struct mensagem al){
if(li == NULL)
return 0;
Elem *no;
no = (Elem*) malloc(sizeof(Elem));
if(no == NULL)
return 0;
no->dados = al;
no->prox = NULL;
if((*li) == NULL){//lista vazia: insere início
*li = no;
}else{
Elem *aux;
aux = *li;
while(aux->prox != NULL){
aux = aux->prox;
}
aux->prox = no;
}
return 1;
}
void imprime_lista(Lista* li){
if(li == NULL)
return 0;
Elem* no = *li;
while(no != NULL){
printf("%c1",no->dados.msg);
no = no->prox;
}
}
Int main
int main()
{
int mod_exec=1,i=0;
char c;
struct mensagem al;
Lista* li = cria_lista();
while((c = getchar()) != '\n')
{
insere_lista_final(li,al);
imprime_lista(li);
}
return 0 ;
}
typedef struct elemento* Lista;
typedef struct elemento Elem;
Lista* cria_lista(){
Lista* li = (Lista*) malloc(sizeof(Lista));
if(li != NULL)
*li = NULL;
return li;
}
int insere_lista_final(Lista* li, struct mensagem al){
if(li == NULL)
return 0;
Elem *no;
no = (Elem*) malloc(sizeof(Elem));
if(no == NULL)
return 0;
no->dados = al;
no->prox = NULL;
if((*li) == NULL){//lista vazia: insere início
*li = no;
}else{
Elem *aux;
aux = *li;
while(aux->prox != NULL){
aux = aux->prox;
}
aux->prox = no;
}
return 1;
}
void imprime_lista(Lista* li){
if(li == NULL)
return 0;
Elem* no = *li;
while(no != NULL){
printf("%c1",no->dados.msg);
no = no->prox;
}
}
int main()
{
int mod_exec=1,i=0;
char c;
struct mensagem al;
Lista* li = cria_lista();
while((c = getchar()) != '\n')
{
insere_lista_final(li,al);
imprime_lista(li);
}
return 0 ;
}
Int main
int main()
{
int mod_exec=1,i=0;
char c;
struct mensagem al;
Lista* li = cria_lista();
while((c = getchar()) != '\n')
{
insere_lista_final(li,al);
imprime_lista(li);
}
return 0 ;
}
typedef struct elemento* Lista;
typedef struct elemento Elem;
Lista* cria_lista(){
Lista* li = (Lista*) malloc(sizeof(Lista));
if(li != NULL)
*li = NULL;
return li;
}
int insere_lista_final(Lista* li, struct mensagem al){
if(li == NULL)
return 0;
Elem *no;
no = (Elem*) malloc(sizeof(Elem));
if(no == NULL)
return 0;
no->dados = al;
no->prox = NULL;
if((*li) == NULL){//lista vazia: insere início
*li = no;
}else{
Elem *aux;
aux = *li;
while(aux->prox != NULL){
aux = aux->prox;
}
aux->prox = no;
}
return 1;
}
void imprime_lista(Lista* li){
if(li == NULL)
return 0;
Elem* no = *li;
while(no != NULL){
printf("%c1",no->dados.msg);
no = no->prox;
}
}
int main()
{
int mod_exec=1,i=0;
char c;
struct mensagem al;
Lista* li = cria_lista();
while((c = getchar()) != '\n')
{
insere_lista_final(li,al);
imprime_lista(li);
}
return 0 ;
}
添加带有正确初始化变量的主函数不会导致错误:
int mainvoid
{
Lista*l=cria_Lista;
结构mensagem1={.msg='a',.chave='b'};
结构mensagem2={.msg='c',.chave='d'};
插图最后,m1;
最终插图,m2;
即兴演出;
返回0;
}
确保编译时触发警告标记。是否考虑过添加主函数?此外,触发警告并将其视为错误;使用malloc时无需强制转换malloc ing sizeoflista可能不是您想要的,因为lista是一个指针。您可能希望将lista定义为:typedef struct elemento lista;我强烈建议不要在源代码中使用您的本地语言。例如,对于一个不懂你所用语言的人来说,你发布的代码容易理解的程度是他的两倍。