#include <iostream>
#include <string>
#define maxsize 200
using namespace std;
struct student
{
int num;
string name;
string sex;
int age;
string hometown;
};
struct listnode
{
student s;
listnode *next;
};
void Intilist(listnode *h)
{
h=new listnode;
h->next=NULL;
}
void Insert(listnode *h)
{
listnode *q,*p;
int a,n;
string na,s,ho;
q=new listnode;
cout<<"Enter information(number name sex age hometown)"<<endl;
cin>>n>>na>>s>>a>>ho;
q->s.num=n;
q->s.name=na;
q->s.sex=s;
q->s.age=a;
q->s.hometown=ho;
p=h;
while (p->next!=NULL)
p=p->next;
q->next=p->next;
p->next=q;
}
void Find(listnode *h)
{
string f;
listnode *p;
p=h->next;
cout<<"Enter the name"<<endl;
cin>>f;
while ((p->s.name!=f)&&(p->next!=NULL))
p=p->next;
if(p->s.name==f)
cout<<p->s.num<<' '<<p->s.name<<' '<<p->s.sex<<' '<<p->s.age<<' '<<p->s.hometown<<endl;
else
cout<<"error"<<endl;
}
void Delet(listnode *h)
{
string d;
listnode *p;
p=h;
cout<<"Enter it's name:"<<endl;
cin>>d;
while (p->next->s.name!=d&&p->next->next!=NULL)
p=p->next;
if (p->next->s.name==d)
p->next=p->next->next;
else
cout<<"error"<<endl;
}
int Change(listnode *h)
{
listnode *p;
string cs,n,wh;
int ci;
cout<<"Enter it's name"<<endl;
cin>>n;
p=h->next;
while (p->s.name!=n&&p->next!=NULL)
p=p->next;
if (p->s.name!=n)
{
cout<<"error"<<endl;
return 0;
}
cout<<"Which element?"<<endl;
cin>>wh;
cout<<"Enter the new value:"<<endl;
if (wh=="num")
{
cin>>ci;
p->s.num=ci;
}
else if(wh=="name")
{
cin>>cs;
p->s.name=cs;
}
else if(wh=="sex")
{
cin>>cs;
p->s.sex=cs;
}
else if(wh=="age")
{
cin>>ci;
p->s.age=ci;
}
else if (wh=="hometown")
{
cin>>cs;
p->s.hometown=cs;
}
else
{
cout<<"error"<<endl;
return 0;
}
return 1;
}
void Coutall(listnode *h)
{
listnode *p;
p=h;
while (p->next!=NULL)
{
p=p->next;
cout<<p->s.num<<' '<<p->s.name<<' '<<p->s.sex<<' '<<p->s.age<<' '<<p->s.hometown<<endl;
}
}
void Selectsort(listnode list[],int size)
{
int i,j,k;
listnode tmp;
for(i=0;i<size-1;i++)
{
k=i;
for (j=i+1;j<size;j++)
if (list[j].s.num<list[k].s.num)
k=j;
tmp=list[i];
list[i]=list[k];
list[k]=tmp;
}
}
void Coutorder(listnode list[],int t)
{
cout<<"After sort:"<<endl;
for(int a=0;a<t;a++)
{
cout<<list[a].s.num<<' '<<list[a].s.name<<' ';
cout<<list[a].s.sex<<' '<<list[a].s.age<<' '<<list[a].s.hometown<<endl;
}
}
void Sort(listnode *h)
{
listnode list[maxsize];
listnode *p;
int i=0;
p=h->next;
while (p!=NULL)
{
list[i]=*p;
i++;
p=p->next;
}
Selectsort(list,i+1);
Coutorder(list,i);
}
int main()
{
listnode *h;
char u;
Intilist(h);
cout<<"Enter the order(I to insert,D to delet,C to chang,S to sort,F to find,O to cout all)"<<endl;
while (cin>>u)
{
if (u=='I')
Insert(h);
else if (u=='D')
Delet(h);
else if (u=='C')
Change(h);
else if (u=='S')
Sort(h);
else if (u=='F')
Find(h);
else if (u=='O')
Coutall(h);
else
cout<<"error"<<endl;
cout<<"Enter the order"<<endl;
}
return 0;
}