c语言链表问题,高手们帮帮忙

#include <stdio.h>
#include <stdlib.h>

struct Node_{
char c;
struct Node_*next;
};
struct Node_ *Creatnode()
{
int n=0;
struct Node_*phead,*prear,*newnode;
newnode=(struct Node_*)malloc(sizeof(struct Node_));
phead=NULL;
while (newnode->c!='p') {
if(n==0)
phead=newnode;
else
prear->next=newnode;
prear=newnode;
print("please input a char:\n");
scanf("%c",&newnode->c);
n++;
prear->next=NULL;
return phead;
}
}
struct Node_*Findnode(struct Node_ *head,int dates)
{
struct Node_*p;
p=head;
while (p->c!=dates&&p->next!=NULL) {
p=p->next;
if(p->c==dates)
return p;
else
return NULL;
}
}
struct Node_*FindAhead(struct Node_*head,int dates)
{
struct Node_ *p,*q;
q=NULL;
p=head;
while (p->c!=dates&&p->next!=NULL) {
p=p->next;
if(p->c==dates)
return p;
else
return NULL;
}

}
struct Node_*Deletenode(struct Node_ *head,int dates)
{
struct Node_ *delete,*p;
p=FindAhead(head, dates);
p=Findnode(head, dates);
if(!delete)
return NULL;
else
p->next=delete->next;
free(delete);
return head;
}
void display(struct Node_ *head)
{
struct Node_ *p;
p=head;
if(p!=NULL){
printf(" %c\n",p->c);
p=p->next;
}
}
int main()
{
struct Node_ *head,*fd;
head=Creatnode();
display(head);
fd=FindAhead(head,'q');
if(fd==NULL)
printf("Ahead of found is null\n");
else
printf("%c\n",fd->c);
printf("Hello, World!\n");
return 0;
}
题目要求用户一个一个输入字符,当输入q时,打印出之前所有输入的并结束程序,并注意内存泄露问题。为什么有错啊,编译器提示有错,但是代码没错

问题挺多,先说说创建函数,请看
struct Node_ *Creatnode()
{
int n=0;
struct Node_*phead,*prear,*newnode;
newnode=(struct Node_*)malloc(sizeof(struct Node_));
phead=NULL;
while (newnode->c!='p') {
if(n==0)
phead=newnode;
else
prear->next=newnode;
prear=newnode;
print("please input a char:\n");
scanf("%c",&newnode->c);
n++;
prear->next=NULL;
return phead;
}
}
第一个问题,一个小问题,你要求输入q结束,你却用了p,这个倒不是重点
重点是你while函数的括号范围用错了,你试想一下,你输入第一个数据,scanf进去了,然后呢,return phead了,程序结束了!你后面的元素怎么办?所以至少你要把while的大括号提前一句!
再看看你的Delete函数,开头声明了一个指针叫delete,这时候delete必然是一串乱码,然后你就什么都不做来了个if(!delete)你究竟打算做什么呢,是不是抄错了?怎么说也先给个初始值吧
你先改改我再看
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-05-03
编译错误:
struct Node_ *Creatnode()
{
int n=0;
struct Node_*phead,*prear,*newnode;
newnode=(struct Node_*)malloc(sizeof(struct Node_));
phead=NULL;
while (newnode->c!='p') {
if(n==0)
phead=newnode;
else
prear->next=newnode;
prear=newnode;
printf("please input a char:\n"); //// 这里写错了。print应该是printf。
第2个回答  2012-05-03
这么复杂啊,好好看一下啊~
第3个回答  2012-05-05
打酱油来的
相似回答
大家正在搜