题目描述
编写一个函数creatlink,用来建立一个动态链表。(包含学号和成绩)
编写一个函数printlink,用来输出一个链表。
编写一个函数dellink,用来删除动态链表中一个指定的结点(由实参指定某一学号,表示要删除该学生结点)。
编写一个函数insertlink,用来向动态链表插入一个结点。
编写一个函数freelink,用来释放一个动态链表。
输入
输入多个学生的学号和成绩,建立动态链表,以0 0 结束
输入学号,删除链表中的对应结点
插入两个链表结点
输出
输出的链表
样例输入
1001 100
1002 95
1005 90
1008 76
0 0
1005
1006 98
1009 99
样例输出
1001 100.00
1002 95.00
1006 98.00
1008 76.00
1009 99.00
参考代码
#include <stdio.h>
#include <stdlib.h>
struct student
{
long num;
float score;
struct student *next;
}
;
struct student *creatlink(void) //建ç«é¾è¡¨çå½æ°
{
struct student *head;
struct student *p1,*p2;
int n=0;
p1=p2=(struct student*)malloc(sizeof(struct student));
//å¼è¾ä¸ä¸ªæ°åå
ï¼å¹¶ä½¿p1,p2æåå®
scanf("%ld%f",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1) head=p1; else p2->next=p1;
p2=p1;
p1=(struct student*)malloc(sizeof(struct student));
scanf("%ld%f",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}
struct student *dellink(struct student *head,long num) //å é¤ç»çå½æ°
{
struct student *p1,*p2;
if (head==NULL) //æ¯ç©ºè¡¨
{
//cout<<"list null!"<<endl;
return(head);
}
p1=head;
//使p1æå第ä¸ä¸ªç»ç¹
while(num!=p1->num && p1->next!=NULL) //p1æåçä¸æ¯æè¦æ¾çç»ç¹ä¸åé¢è¿æç»ç¹
{
p2=p1;
//p1å移ä¸ä¸ªç»ç¹
p1=p1->next;
}
if(num==p1->num) //æ¾å°äº
{
if(p1==head) head=p1->next;
//è¥p1æåçæ¯é¦ç»ç¹ï¼æ第äºä¸ªç»ç¹å°åèµäºhead else p2->next=p1->next;
//å¦åå°ä¸ä¸ç»ç¹å°åèµç»åä¸ç»ç¹å°å
//cout<<"delete:"<<num<<endl;
// n=n-1;
}
//else cout<<"cannot find "<<num; //æ¾ä¸å°è¯¥ç»ç¹
return(head);
}
struct student *insertlink(struct student *head,struct student *stud) //æå
¥ç»ç¹çå½æ°
{
struct student *p0,*p1,*p2;
p1=head;
//使ï½ï¼æå第ä¸ä¸ªç»ç¹
//p0=stud; //æåè¦æå
¥çç»ç¹
p0 =(struct student*)malloc(sizeof(struct student));
//edit by lyh
*p0 = *stud;
// 2013.11.7
if(head==NULL) //åæ¥çé¾è¡¨æ¯ç©ºè¡¨
{
head=p0;
//使ï½ï¼æåçç»ç¹ä½ä¸ºå¤´ç»ç¹
p0->next=NULL;
} else
{
while((p0->num>p1->num) && (p1->next!=NULL))
{
p2=p1;
//使ï½ï¼æååæpï¼æåçç»ç¹
p1=p1->next;
}
//p1å移ä¸ä¸ªç»ç¹
if(p0->num<=p1->num)
{
if(head==p1) head=p0;
//æå°åæ¥ç¬¬ä¸ä¸ªç»ç¹ä¹å else p2->next=p0;
//æå°p2æåçç»ç¹ä¹å*ï¼
p0->next=p1;
} else
{
p1->next=p0;
//æå°æåçç»ç¹ä¹å
p0->next=NULL;
}
}
// n=n+1; //ç»ç¹æ°å ï¼
return (head);
}
void printlink(struct student *head) //è¾åºé¾è¡¨çå½æ°
{
struct student *p;
//cout<<"Nowï¼These "<<n<<" records areï¼"<<endl;
p=head;
if(head!=NULL)
do
{
printf("%ld %.2fn",p->num,p->score);
p=p->next;
}
while(p!=NULL);
}
void freelink(struct student *head) //éæ¾é¾è¡¨
{
struct student *p,*q;
//cout<<"Nowï¼These "<<n<<" records areï¼"<<endl;
p=head;
if(head!=NULL)
do
{
q =p ;
p=p->next;
free(q);
}
while(p!=NULL);
}
int main()
{
struct student *creatlink(void);
struct student *dellink(struct student *,long);
struct student *insertlink(struct student *,struct student *);
void printlink(struct student *);
void freelink(struct student *);
struct student *head,stu;
long del_num;
head=creatlink();
scanf("%ld",&del_num);
head=dellink(head,del_num);
scanf("%ld%f",&stu.num,&stu.score);
head=insertlink(head,&stu);
scanf("%ld%f",&stu.num,&stu.score);
head=insertlink(head,&stu);
printlink(head);
freelink(head);
return 0;
}
解析
暂无