谁能帮我调试成功了,给50分

cluber2001 2002-12-24 10:37:02
#include<iostream.h>
#include<string.h>
struct Student
{
char name[10];
Student* next;
};
Student* head;
Student* re(Student* pl);
Student* Create()
{
Student* pS;
Student* pEnd;
pS=new Student;
cout<<"please input a student name."<<endl;
cin.getline(pS->name,sizeof(pS->name));
head=NULL;
pEnd=pS;
while(!strcmp(pS->name,"exit"))
{
if(head==NULL)
{
head=pS;
pS->next=NULL;
}
else
{
pEnd->next=pS;
}
pEnd=pS;
cout<<"please input a student name."<<endl;
pS=new Student;
cin.getline(pS->name,sizeof(pS->name));
}
pEnd->next=NULL;
delete pS;
return(head);
}

void ShowList(Student* head)
{
Student* ps=head;
cout<<"now the items of list are \n";
while(ps!=NULL)
{
cout<<ps->name<<endl;
ps=ps->next;
}
return;
}
void main()
{
ShowList(Create());
Student* pw=new Student;
strcpy(pw->name,"mant");
re(pw);
ShowList(head);
return;
}
Student* re(Student* pl)
{
Student* pb,*ph;
ph=head;
if((ph->name)=="jone")
{
pl->next=ph;
head=pl;
return head;
}
while(ph)
{
pb=ph;
ph=pb->next;
if((ph->name)=="jone")
{
pb->next=pl;
pl->next=ph;
return head;
}
}
pb->next=pl;
pl->next=NULL;
return head;
}
...全文
44 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lyc96532 2002-12-24
  • 打赏
  • 举报
回复
朋友:我调试你这个程序有两个问题:

1.while(!strcmp(pS->name,"exit"))你的用意是当输入exit时结束建链,但并不能如你愿。应该为while(strcmp(pS->name,"exit"))。
2.在re函数内要把if((ph->name)=="jone"))改为if(!strcmp(ph->name,"jone"))

你再试试吧!
langziji 2002-12-24
  • 打赏
  • 举报
回复
#include<iostream.h>
#include<string.h>
struct Student
{
char name[10];
Student* next;
};
Student* head;
Student* re(Student* pl);
Student* Create()
{
Student* pS;
Student* pEnd;
pS=new Student;
cout<<"please input a student name."<<endl;
cin.getline(pS->name,sizeof(pS->name));
head=NULL;
pEnd=pS;
while(strcmp(pS->name,"exit"))//此处被修改
{
if(head==NULL)
{
head=pS;
pS->next=NULL;
}
else
{
pEnd->next=pS;
}
pEnd=pS;
cout<<"please input a student name."<<endl;
pS=new Student;
cin.getline(pS->name,sizeof(pS->name));
}
pEnd->next=NULL;
delete pS;
return(head);
}

void ShowList(Student* head)
{
Student* ps=head;
cout<<"now the items of list are \n";
while(ps!=NULL)
{
cout<<ps->name<<endl;
ps=ps->next;
}
return;
}
void main()
{
ShowList(Create());
Student* pw=new Student;
strcpy(pw->name,"mant");
re(pw);
ShowList(head);
return;
}
Student* re(Student* pl)
{
Student* pb,*ph;
ph=head;
if(0==strcmp(ph->name,"jone"))//此处被修改
{
pl->next=ph;
head=pl;
return head;
}
while(ph)
{
pb=ph;
ph=pb->next;
if(0==strcmp(ph->name,"jone"))//此处被修改
{
pb->next=pl;
pl->next=ph;
return head;
}
}
pb->next=pl;
pl->next=NULL;
return head;
}
zxm954712 2002-12-24
  • 打赏
  • 举报
回复
还要加上一段在main函数最后
Student *p;
while(head) {
p = head;
head = head->next;
delete p;
p = NULL;
}
来释放内存阿
zxm954712 2002-12-24
  • 打赏
  • 举报
回复
以下尉我改过的,试试:
#include<iostream.h>
#include<string.h>
#include <memory.h>
struct Student
{
char name[10];
Student* next;
};

Student* head;
Student* re(Student* pl);
Student* Create()
{
Student* pS;
Student* pEnd;
pS=new Student;
cout<<"please input a student name."<<endl;
cin.getline(pS->name,sizeof(pS->name));
head=NULL;
pEnd=pS;
while(strcmp(pS->name,"exit"))
{
if(head==NULL)
{
head=pS;
// pS->next=NULL;
}
else
{
pEnd->next=pS;
}
pEnd=pS;
cout<<"please input a student name."<<endl;
pS=new Student;
cin.getline(pS->name,sizeof(pS->name));
}
pEnd->next=NULL;
delete pS;
return(head);
}

void ShowList(Student* head)
{
Student* ps=head;
cout<<"now the items of list are \n";
while(ps!=NULL)
{
cout<<ps->name<<endl;
ps=ps->next;
}
return;
}
void main()
{
ShowList(Create());
Student* pw=new Student;
strcpy(pw->name,"mant");
re(pw);
ShowList(head);
return;
}
Student* re(Student* pl)
{
Student* pb,*ph;
ph=head;
if(!memcmp(ph->name,"jone",4))
{
pl->next=ph;
head=pl;
return head;
}
while(ph->next)
{
pb=ph;
ph=pb->next;
if(!memcmp(ph->name,"jone", 4))//名字为jone
{
pb->next=pl;
pl->next=ph;
return head;
}
}
ph->next=pl;
pl->next=NULL;
return head;
}

casinosun 2002-12-24
  • 打赏
  • 举报
回复
new 与DELETE 有问题
cluber2001 2002-12-24
  • 打赏
  • 举报
回复
如果有“jone“就把"mant"加在前边,否则放到表尾

70,032

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧