修改一下这个程序,下面对程序的修改建议
(1)尾插法<-->头插法,或者按大小插入
(2)设立菜单,可以多次选择某种操作
(3)有头结点<->无头结点
建立学生成绩单链表,实现对它的记录的遍历、插入和删除
#include<malloc.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
struct student
{
int num; /*学号*/
float score; /*成绩*/
struct student *next; /*指针域*/
};
#define LEN sizeof(struct student)
struct student *create() /*此函数返回一个指向链表表头结点的指针*/
{
struct student *head; /*指向student类型变量的指针head为头指针*/
struct student *p,*tail; /*tail指向链表末尾元素,p指向新分配的结点*/
float temp; /*临时数据变量*/
head=NULL;
do
{
p=(struct student *)malloc(sizeof(struct student));/*分配新结点*/
printf("Number Score:");
fflush(stdin); /*清除输入缓冲区*/
scanf("%d %f",&p->num,&temp);
if(p->num==0)
{
free(p);
break;
};
p->score=temp; /*取得结点数据*/
p->next=NULL;
if(head==NULL)
{
head=p;
tail=p;
}
else
{
tail->next=p;
tail=p; /*指向尾结点*/
}
}while(p->num!=0);
return(head);
}
void display(struct student *head)
{
struct student *p;
p=head;
while(p!=NULL)
{
printf("%4d %5.1f\n",p->num,p->score);
p=p->next;
}
}
struct student *insert(struct student *head,struct student *new)
{
struct student *p,*q;
if(head==NULL)
head=new;
else
{
if(head->num>=new->num)
{
new->next=head;
head=new;
}/*新结点插在链首*/
else
{
p=head;
while(p->num<new->num&&p->next!=NULL)
{
q=p;
p=p->next;
}
if(p->num>new->num)
{
q->next=new;
new->next=p;
}
else
{
p->next=new;
new->next=NULL;
}
}
}
return(head);
}
struct student *delete(struct student *head,int num)
/*在头为head的单链表中将指定的学号为num的结点删除*/
/*算法返回新的链表的表头结点*/
{
struct student *p1,*p2; /*设置两个指针,用以指示删除位置*/
if(head==NULL)
printf("\nList null\n");
else
{
p1=head;
while(num!=p1->num&&p1->next!=NULL)
/*p1所指的结点不是要删除的结点,且其后还有结点*/
{
p2=p1;
p1=p1->next;/*后移一个结点*/
}
if(num==p1->num)/*找到了要删除的结点*/
{
if(p1==head)
head=p1->next; /*若p1所指为第一个结点,则把第二个结点的地址赋给head*/
else
p2->next=p1->next; /*否则将下一个结点的地址赋给前一个结点的指针域*/
printf("delete:%4d\n",num);
free(p1);
}
else
printf("%4d not been found!\n",num);
}
return(head);
}
void main()
{
struct student *head,*stu;
float temp;
int number;
printf("Input records:\n");
head=create();
display(head);
stu=(struct student *)malloc(LEN);
printf("Input the insert Number Score:");
scanf("%d %f",&stu->num,&temp);
stu->score=temp;
stu->next=NULL;
head=insert(head,stu);
display(head);
printf("\nInput the number to delete:");
scanf("%4d",&number);
head=delete(head,number);
display(head);
getch();
}
(1)尾插法<-->头插法,或者按大小插入
(2)设立菜单,可以多次选择某种操作
(3)有头结点<->无头结点
建立学生成绩单链表,实现对它的记录的遍历、插入和删除
#include<malloc.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
struct student
{
int num; /*学号*/
float score; /*成绩*/
struct student *next; /*指针域*/
};
#define LEN sizeof(struct student)
struct student *create() /*此函数返回一个指向链表表头结点的指针*/
{
struct student *head; /*指向student类型变量的指针head为头指针*/
struct student *p,*tail; /*tail指向链表末尾元素,p指向新分配的结点*/
float temp; /*临时数据变量*/
head=NULL;
do
{
p=(struct student *)malloc(sizeof(struct student));/*分配新结点*/
printf("Number Score:");
fflush(stdin); /*清除输入缓冲区*/
scanf("%d %f",&p->num,&temp);
if(p->num==0)
{
free(p);
break;
};
p->score=temp; /*取得结点数据*/
p->next=NULL;
if(head==NULL)
{
head=p;
tail=p;
}
else
{
tail->next=p;
tail=p; /*指向尾结点*/
}
}while(p->num!=0);
return(head);
}
void display(struct student *head)
{
struct student *p;
p=head;
while(p!=NULL)
{
printf("%4d %5.1f\n",p->num,p->score);
p=p->next;
}
}
struct student *insert(struct student *head,struct student *new)
{
struct student *p,*q;
if(head==NULL)
head=new;
else
{
if(head->num>=new->num)
{
new->next=head;
head=new;
}/*新结点插在链首*/
else
{
p=head;
while(p->num<new->num&&p->next!=NULL)
{
q=p;
p=p->next;
}
if(p->num>new->num)
{
q->next=new;
new->next=p;
}
else
{
p->next=new;
new->next=NULL;
}
}
}
return(head);
}
struct student *delete(struct student *head,int num)
/*在头为head的单链表中将指定的学号为num的结点删除*/
/*算法返回新的链表的表头结点*/
{
struct student *p1,*p2; /*设置两个指针,用以指示删除位置*/
if(head==NULL)
printf("\nList null\n");
else
{
p1=head;
while(num!=p1->num&&p1->next!=NULL)
/*p1所指的结点不是要删除的结点,且其后还有结点*/
{
p2=p1;
p1=p1->next;/*后移一个结点*/
}
if(num==p1->num)/*找到了要删除的结点*/
{
if(p1==head)
head=p1->next; /*若p1所指为第一个结点,则把第二个结点的地址赋给head*/
else
p2->next=p1->next; /*否则将下一个结点的地址赋给前一个结点的指针域*/
printf("delete:%4d\n",num);
free(p1);
}
else
printf("%4d not been found!\n",num);
}
return(head);
}
void main()
{
struct student *head,*stu;
float temp;
int number;
printf("Input records:\n");
head=create();
display(head);
stu=(struct student *)malloc(LEN);
printf("Input the insert Number Score:");
scanf("%d %f",&stu->num,&temp);
stu->score=temp;
stu->next=NULL;
head=insert(head,stu);
display(head);
printf("\nInput the number to delete:");
scanf("%4d",&number);
head=delete(head,number);
display(head);
getch();
}