⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shiyan8_2.cpp

📁 学习c++的一些实例程序
💻 CPP
字号:
#include<iostream.h>
#include<iomanip.h>
#include<string.h>
struct Student
{
   int num;
   char name[8];
   char sex;
   float grade;
};
struct StudentNode
{
   int num;
   char name[8];
   char sex;
   float grade;
   StudentNode*next;
};

void ClearList(StudentNode*head)      //清除链表中各结点
{
  StudentNode*temp;
  while(head!=NULL)
   {
      temp=head->next;
      delete head;
      head=temp;
   }
}
void OutputStudentList(StudentNode*head)         //输出链表中学生信息
{
     StudentNode*p=head;
       while(p!=NULL)
          {
              cout<<setw(5)<<p->num<<setw(10)<<p->name
                  <<setw(4)<<p->sex<<setw(6)<<p->grade<<endl;
              p=p->next;
           }
}
void InsertToOrderedList(StudentNode*head,StudentNode*newNode)
//向学生链表中插入结点,保持链表结点成绩升序排列
{
   StudentNode*p=head,*q=NULL;
   while(p!=NULL&&p->grade<newNode->grade) //确定结点插入位置
   {
        q=p;
        p=p->next;
   }
  if(p==head)       //待插入结点具有最低成绩,插入链表首
   {
       newNode->next=head;
       head=newNode;
    }
   else
 {
     newNode->next=p;
     q->next=newNode;
 }
}
void main()
{
    Student stu[]=
    {
       {101,"Zhang",'M',99.5},
       {102,"Li",'F', 87},
       {103,"Wang",'M',60},
       {104,"Liu",'M',88},
       {105,"Zhao",'F',72},
       {106,"Wu",'M',86.5}
    };
    int i;
    cout<<"before sorted"<<endl;
    for(i=0;i<sizeof(stu)/sizeof(stu[0]);i++)
         cout<<setw(5)<<stu[i].num<<setw(10)<<stu[i].name
             <<setw(4)<<stu[i].sex<<setw(6)<<stu[i].grade<<endl;
    StudentNode*head=NULL;     //初始为空表
    StudentNode*temp;
    for(i=0;i<sizeof(stu)/sizeof(stu[0]);i++)
    {
         temp=new StudentNode;
         temp->num=stu[i].num;
         strcpy(temp->name,stu[i].name);
         temp->sex=stu[i].sex;
         temp->grade=stu[i].grade;
         InsertToOrderedList(head,temp);
      }
   cout<<"after sorted"<<endl;
   OutputStudentList(head);
   ClearList(head);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -