#include<bits/stdc++.h>
using namespace std;
struct data
{//建立结构体data,方便存储信息,也方便输入输出
char num[25],name[45],sex,score[45],address[45];
int age;
bool scan()
{
cin>>num;
if(strcmp(num,"end")==0)
return true;//如果输入end,就停止输入,返回true
cin>>name>>sex>>age>>score>>address;
return false;//输入还没有停止,返回false
}
void print()
{cout<<num<<" "<<name<<" "<<sex<<" "<<age<<" "<<score<<" "<<address<<endl;//输出信息
}
};
struct node
{data d;//d表示该节点对应元素的信息
node *next,*pre;//next指向结点的后继,pre指向结点的前驱
}*head,*tail,*p;//head表示头指针,tail表示尾指针
int n,k;
int main()
{head=new node;
head->pre=head->next=NULL;
tail=head;//新建一个虚拟头结点(不存储元素的值)方便删除操作(不会越界),此时头尾指针都指向它
while(1)
{
p=new node;
if(p->d.scan())//输入该学生的信息
break;//如果输入已经停止,就跳出循环
p->pre=tail;
tail->next=p;//将p指向的结点插入到tail指向的结点后面
p->next=NULL;
tail=p;//将tail更新为p,下次就插入到它的后面
}
p=tail;//从尾结点向前遍历
while(p!=head)//遍历到虚拟头结点时停止
{p->d.print();//输出该学生的信息
p=p->pre;
}
return 0;
}
统计学生信息(动态链表)
于 2018-02-12 03:13:49 首次发布