代碼:
#include <iostream>
using namespace std;
struct Student
{
int num;
double score;
struct Student *next;
};
int main( )
{
Student *head=NULL,*p,*q;
//建立動態鏈表
for(int i=0; i<3; i++)
{
p = new Student;
cin>>p->num>>p->score;
p->next=NULL;
if (i==0) head=p;
else q->next=p;
q=p;
}
//輸出動態鏈表
p=head;
while(p!=NULL)
{
cout<<p->num<<" "<<p->score<<endl;
p=p->next;
}
return 0;
}
運行結果 :
學習心得:
好久沒看鏈表了,只記得大體情況,很多都不記得了,看完代碼,突然醒悟,原來是這樣的。