知道到了,谢谢~~
//链表的建立,删除,插入
struct student
{
long number;
float score;
student * next;
};
student * head; //链首指针
student *stud; //插入节点指针
student * creat() //创建链表
{
student *ps; //结点指针
student *pend; //链尾指针
ps=new student; //分配内存空间,建立一个节点,准被插入链表
cin>>ps->number>>ps->score;
head=NULL; //开始链表为空
pend=ps;
while(ps->number!=0) //输入为0结束
{
if(head==NULL)
head=ps;
else
pend->next=ps;
pend=ps;
ps=new student;
cin>>ps->number>>ps->score;
}
pend->next=NULL;
delete ps;
return head;
}
void showlist(student * head) //打印链表信息
{
cout<<"\nnow the items of list are:"<<endl;
while(head)
{
cout<<head->number<<","<<head->score<<endl;
head=head->next;
}
}
void ldelete(student *head,long number) //删除链表节点
{
char ch;
cout<<"是否要删除?(Y/N)";
cin>>ch;
if(ch=='y'||ch=='Y')
{
cout<<"enter a number will be deleted:";
cin>>number;
student *p;
if(!head)
{
cout<<"list is null.";
return; //未作删除,返回
}
if(head->number==number) //要删除的节点在链首
{
p=head;
head=head->next;
delete p;
cout<<number<<"the head of list have been deleted.\n\n";
return;
}
for(student *pguard=head;pguard->next;pguard=pguard->next) //删除的节点在链中或结尾
{
if(pguard->next->number==number)
{
p=pguard->next;
pguard->next=p->next;
delete p;
cout<<number<<"have been deleted.\n\n";
return;
}
}
cout<<number<<"not found!\n"; //此处表示未找到要删除的节点
}
}
void insert(student *head,student *stud) //插入链表节点
{
char ch;
cout<<"\n是否要插入?(Y/N)";
cin>>ch;
if(ch=='y'||ch=='Y')
{
cout<<"enter informaion of insert:";
cin>>stud->number>>stud->score;
if(head==NULL)
{
head=stud;
stud->next=NULL;
return;
}
if(head->number>stud->number) //插入节点的位置在链首
{
stud->next=head;
stud=head; //插入节点成为链首
return;
}
student * pguard=head; //把插入节点先放置到链首
while((pguard->next)&&(pguard->next->number)<(stud->number))//未找到,继续向后走
pguard=pguard->next;
stud->next=pguard->next; //找到插入位置进行操作
pguard->next=stud;
}
}
void main()
{
long number;
student st;
showlist(creat());
ldelete(head,number);
showlist(head);
insert(head,&st);
showlist(head);
}
想以学号大小来排序,算法没想出来~