依自己理解写了双链表,运行好像行,请大家指点一下,该如何处理
依自己理解写了双链表,运行好像行,请大家指点一下
#include<iostream>
#include<string>
using namespace std;
struct student
{
char name[20];
float score;
student *prior;
student *next;
};
void main()
{
student *head=NULL,*tail=NULL,*pnew,*p=NULL;
char name[20];
float score;
while(1)
{
cout<<"input name and score:(0 to exit)"<<endl;
cin>>name;
if(name[0]=='0')break;
cin>>score;
pnew=new student;
strcpy(pnew->name ,name);
pnew->score=score;
pnew->prior=p;
pnew->next=NULL;
if(tail==NULL)
head=pnew;
else
tail->next=pnew;
tail=pnew;
p=pnew;
}
student *pos=tail;
while(pos!=NULL)
{
cout<<pos->name<<" "<<pos->score;
pos=pos->prior ;
if(pos!=NULL)cout<<"-->";
}
cout<<endl;
while(head!=NULL)
{
pos=head;
head=head->next ;
delete pos;
}
}
------解决方案--------------------
#include<iostream>
#include<string>
using namespace std;
struct student
{
char name[20];
float score;
student *prior;
student *next;
};
void main()
{
student *head=NULL,*tail=NULL,*pnew,*p=NULL;
char name[20];
float score;
while(1)
{
cout<<"input name and score:(0 to exit)"<<endl;
cin>>name;
if(name[0]=='0')break;
cin>>score;
pnew=new student;
strcpy(pnew->name ,name);
pnew->score=score;
pnew->prior=p;
pnew->next=NULL;
if(tail==NULL)
head=pnew;
else
tail->next=pnew;
tail=pnew;
p=pnew;
}
student *pos=tail;
while(pos!=NULL)
{
cout<<pos->name<<" "<<pos->score;
pos=pos->prior ;
if(pos!=NULL)cout<<"-->";
}
cout<<endl;
while(head!=NULL)
{
pos=head;
head=head->next ;
delete pos;
}
}
------解决方案--------------------
#include<iostream>
#include<string.h>
#include <stdio.h>
using namespace std;
typedef struct student
{
char name[20];
float score;
struct student *prior;
struct student *next;
}Student;
int main()
{
Student *head=NULL,*tail=NULL,*pnew,*p=NULL;
char name[20];
float score;
while(1)
{
cout<<"input name and score:(0 to exit)"<<endl;
cin>>name;
if( name[0] =='0')
break;
cin>>score;
pnew=new student;
strcpy(pnew->name ,name);
pnew->score=score;
pnew->prior=p;
pnew->next=NULL;
if(tail==NULL)
head=pnew;
else
tail->next=pnew;
tail=pnew;
p=pnew;
}
Student *pos=tail;
while(pos!=NULL)
{
cout<<pos->name<<" "<<pos->score;
pos=pos->prior ;
if(pos!=NULL)
cout<<"-->";
}
cout<<endl;
while(head!=NULL)
{
pos=head;