C++高手来看看

来源:百度知道 编辑:UC知道 时间:2024/05/14 21:45:04
#include<iostream>
using namespace std;
struct students
{int num;
char b;
students*next;};
int main()
{students *head;
students*creat();
void print(students*);
head=creat();
print(head);
return 0;
}
students *creat()
{students*head,*p_new,*p_tail;
head=NULL;
p_new=p_tail=new students;
cin>>p_new->num>>p_new->b;
while(p_new->num!=0)
{if(head=NULL)head=p_new;
else p_tail->next=p_new;
p_tail=p_new;
p_new=new students;
cin>>p_new->num>>p_new->b;
}
p_tail->next=NULL;
return(head);
}
void print(students*x)
{students*p;
p=x;
if(p!=NULL)
do{cout<<p->num<<p->b;
p=p->next;}while(p!=NULL);
else cout<<"empty";}哪里有错啊~~帮忙看下,达不到目的啊

#include<iostream>
using namespace std;

struct students
{
int num;
char b;
students*next;
};

students*creat();
void print(students*);

int main()
{
students *head;
head=creat();
print(head);
// 此处还应该释放你申请的堆内存资源,你自己搞定吧!
return 0;
}

students *creat()
{
students*head,*p_new,*p_tail;
head=NULL;
p_new=p_tail=new students;
cin>>p_new->num>>p_new->b;
while(p_new->num!=0)
{
if(head==NULL) // 关键是你此处的比较,你用了“=”,而应该用“==”
head=p_new;
else
p_tail->next=p_new;
p_tail=p_new;
p_new=new students;
cin>>p_new->num>>p_new->b;
}
delete p_new;
p_tail->next=NULL;

return(head);
}

void print(students*x)
{
students*p;
p=x;
if(p!=NULL)
{
do{