c语言创建了一个单链表,然后输出,结果都正确,但为什么我报错
#include"stdio.h"#include"malloc.h"typedefstructnode{charinfo;structnode*next;}HeadLi...
#include "stdio.h"
#include "malloc.h"
typedef struct node{
char info;
struct node *next;
}HeadLink;
HeadLink *cyclecreat(){
HeadLink *head,*q,*p;
char c;
head=(HeadLink *)malloc(sizeof(HeadLink));
head=NULL;
q=NULL;
while((c=getchar())!='\n'){
p=(HeadLink *)malloc(sizeof(HeadLink));
p->info=c;
if(head==NULL)
head=q=p;
else{
q->next=p;
q=p;
}
}
return head;
}
void print(HeadLink *head){
HeadLink *p;
p=head;
printf("\n");
while(p){
putchar(p->info);
putchar(' ');
p=p->next;
}
}
void main(){
HeadLink *head;
head=cyclecreat();
print(head);
} 展开
#include "malloc.h"
typedef struct node{
char info;
struct node *next;
}HeadLink;
HeadLink *cyclecreat(){
HeadLink *head,*q,*p;
char c;
head=(HeadLink *)malloc(sizeof(HeadLink));
head=NULL;
q=NULL;
while((c=getchar())!='\n'){
p=(HeadLink *)malloc(sizeof(HeadLink));
p->info=c;
if(head==NULL)
head=q=p;
else{
q->next=p;
q=p;
}
}
return head;
}
void print(HeadLink *head){
HeadLink *p;
p=head;
printf("\n");
while(p){
putchar(p->info);
putchar(' ');
p=p->next;
}
}
void main(){
HeadLink *head;
head=cyclecreat();
print(head);
} 展开
4个回答
展开全部
个人感觉这样可以:
HeadLink *cyclecreat(){
HeadLink *head,*q,*p;
char c;
head=(HeadLink *)malloc(sizeof(HeadLink));
head=NULL;
while((c=getchar())!='\n'){
p=(HeadLink *)malloc(sizeof(HeadLink));
p->info=c;
//p->next=NULL;
if(head==NULL)
head=q=p;
else{
q->next=p;
q=p;
}
}
p->next=NULL;//在链表建立完毕后为最后一个节点地址存储位置定为空
return head;
}
HeadLink *cyclecreat(){
HeadLink *head,*q,*p;
char c;
head=(HeadLink *)malloc(sizeof(HeadLink));
head=NULL;
while((c=getchar())!='\n'){
p=(HeadLink *)malloc(sizeof(HeadLink));
p->info=c;
//p->next=NULL;
if(head==NULL)
head=q=p;
else{
q->next=p;
q=p;
}
}
p->next=NULL;//在链表建立完毕后为最后一个节点地址存储位置定为空
return head;
}
展开全部
HeadLink *cyclecreat(){
HeadLink *head,*q,*p;
char c;
head=(HeadLink *)malloc(sizeof(HeadLink));
head=NULL;
q=NULL;
while((c=getchar())!='\n'){
p=(HeadLink *)malloc(sizeof(HeadLink));
p->info=c;
p->next=NULL;/*注意这里*/
if(head==NULL)
head=q=p;
else{
q->next=p;
q=p;
}
}
return head;
}
HeadLink *head,*q,*p;
char c;
head=(HeadLink *)malloc(sizeof(HeadLink));
head=NULL;
q=NULL;
while((c=getchar())!='\n'){
p=(HeadLink *)malloc(sizeof(HeadLink));
p->info=c;
p->next=NULL;/*注意这里*/
if(head==NULL)
head=q=p;
else{
q->next=p;
q=p;
}
}
return head;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询