SCAU高级语言程序设计--实验11 链表操作(2)
二、堂下练习
1、链表结点的插入
题目:完成插入链表结点的函数(按学号顺序),并调试通过、提交。
#include "stdio.h"
#include "malloc.h"
#define LEN sizeof(struct student)
struct student
{
long num;
int score;
struct student *next;
};
struct student *create(int n)
{
struct student *head=NULL,*p1=NULL,*p2=NULL;
int i;
for(i=1;i<=n;i++)
{ p1=(struct student *)malloc(LEN);
scanf("%ld",&p1->num);
scanf("%d",&p1->score);
p1->next=NULL;
if(i==1) head=p1;
else p2->next=p1;
p2=p1;
}
return(head);
}
void print(struct student *head)
{
struct student *p;
p=head;
while(p!=NULL)
{
printf("%8ld%8d",p->num,p->score);
p=p->next;
printf("\n");
}
}
struct student *insert(struct student *head, struct student *stud)
{
_______________________
}
main()
{
struct student *head,*stu;
int n;
scanf("%d",&n);
head=create(n);
print(head);
stu=(struct student *)malloc(LEN);
scanf("%ld",&stu->num);
scanf("%d",&stu->score);
stu->next = NULL;
head=insert(head,stu);
print(head);
}
思路:这里需要考虑到各种各样的情况,表头,中间,表尾,3中插入方式都需要考虑到。
struct student *insert(struct student *head, struct student *stud)
{
struct student *p0=NULL,*p1=NULL,*p2=NULL;
p1=head;
p0=stud;
//链表没有值,直接插入
if(head==NULL){
head=p0;
p0->next=NULL;
}
else{
//循环找到合适的位置
while((p0->num > p1->num)&&(p1->next!=NULL)){
p2=p1;
p1=p1->next;
}
if(p0->num <= p1->num){//如果是表头或者中间位置
if(head==p1)//如果刚好是表头的位置
head=p0;
else{//表中间的位置
p2->next=p0;
p0->next=p1;
}
}
else{//如果刚好是表尾的位置,直接连接到最后
p1->next=p0;
p0->next=NULL;
}
}
return (head);
}
2、链表的排序
题目:下面程序,先创建一个链表(链表中各结点未按学号由小到大排序),然后调用sort函数,将链表中各结点按学号由小到大排序。
#include "stdio.h"
#include "malloc.h"
#define LEN sizeof(struct student)
struct student
{
long num;
int score;
struct student *next;
};
struct student *create(int n)
{
struct student *head=NULL,*p1=NULL,*p2=NULL;
int i;
for(i=1;i<=n;i++)
{ p1=(struct student *)malloc(LEN);
scanf("%ld",&p1->num);
scanf("%d",&p1->score);
p1->next=NULL;
if(i==1) head=p1;
else p2->next=p1;
p2=p1;
}
return(head);
}
void print(struct student *head)
{
struct student *p;
p=head;
while(p!=NULL)
{
printf("%8ld%8d",p->num,p->score);
p=p->next;
printf("\n");
}
}
struct student *insert(struct student *head, struct student *stud)
{ struct student *p0,*p1,*p2;
p1=head; p0=stud;
if(head==NULL)
{head=p0;}
else
{ while( (p0->num > p1->num) && (p1->next!=NULL) )
{ p2=p1; p1=p1->next;}
if( p0->num < p1->num )
{ if( head==p1 ) head=p0;
else p2->next=p0;
p0->next=p1; }
else { p1->next=p0;}
}
return(head);
}
struct student *del(struct student *head,long num)
{
struct student *p1,*p2;
p1=head;
while(p1!=NULL)
{
if(p1->num == num)
{
if(p1 == head) head=p1->next;
else p2->next=p1->next;
free(p1);
break;
}
p2=p1;
p1=p1->next;
}
return(head);
}
struct student *sort(struct student *head)
{
_______________________
}
main()
{
struct student *head,*stu;
int n;
scanf("%d",&n);
head=create(n);
print(head);
head=sort(head);
print(head);
}
思路:
struct student *sort(struct student *head){
struct student *p1,*p2;
p2=head;
p1=head;
p2=p2->next;
p1->next=NULL;
p1=p2;
while(p2->next!=NULL){
p2=p2->next;
p1->next=NULL;
head=insert(head,p1);
p1=p2;
}
head=insert(head,p1);
return (head);
}