ch10_11.cpp
Upload User: puke2000
Upload Date: 2022-07-25
Package Size: 912k
Code Size: 1k
Category:

CSharp

Development Platform:

Visual C++

  1. //***********************
  2. //**    ch10_11.cpp    **
  3. //***********************
  4. #include <iostream.h>
  5. struct Student
  6. {
  7.   long number;
  8.   float score;
  9.   Student* next;
  10. };
  11. Student* head;    //链首指针
  12. Student* Create()
  13. {
  14.   Student* pS;        //创建的结点指针
  15.   Student* pEnd;      //链尾指针,用于在其后面插入结点
  16.   pS=new Student;     //新建一个结点,准备插入链表
  17.   cin >>pS->number >>pS->score;    //给结点赋值
  18.   head=NULL;          //一开始链表为空
  19.   pEnd=pS;
  20.   while(pS->number!=0){
  21.     if(head==NULL)
  22.       head=pS;
  23.     else
  24.       pEnd->next=pS;
  25.     pEnd=pS;        //s点
  26.     pS=new Student;
  27.     cin >>pS->number >>pS->score;
  28.   }
  29.   pEnd->next=NULL;
  30.   delete pS;
  31.   return(head);
  32. }
  33. void ShowList(Student* head)
  34. {
  35.   cout <<"now the items of list are n";
  36.   while(head){
  37.     cout <<head->number <<"," <<head->score <<endl;
  38.     head=head->next;
  39.   }
  40. }
  41. void main()
  42. {
  43.   ShowList(Create());
  44. }