第1关:删除链表元素
任务描述
本关任务:给定程序中已建立一个带有头结点的单向链表,在main函数中将多次调用fun函数,每调用一次fun函数,输出链表尾部结点中的数据,并释放该结点,使链表缩短。 请在指定位置填写代码,使程序得出正确的结果。请不要增行或删行,或更改程序的结构。
相关知识
相关知识略
编程要求
请仔细阅读右侧代码,结合相关知识,在右边提示区域内进行代码补充,完成编写删除链表元素的小程序。
测试说明
平台会对你编写的代码进行测试:
预期输出:
输出每次删除的链表尾部数据。
输出当前链表数据。
代码如下:
#include <stdio.h>
#include <stdlib.h>
#define N 8
typedef struct list
{
int data;
struct list *next;
} SLIST;
void fun( SLIST *p)
{
SLIST *t, *s;
t=p->next;
s=p;
while(t->next != NULL)
{
s=t;
/***** 在以下一行填写代码 *****/
t=t->next;
}
/***** 在以下一行填写代码 *****/
printf(" %d ", t->data);
s->next=NULL;
/***** 在以下一行填写代码 *****/
free(t );
}
SLIST *creatlist(int *a)
{
SLIST *h,*p,*q;
int i;
h=p=(SLIST *) malloc(sizeof(SLIST));
for(i=0; i<N; i++)
{
q=(SLIST *)malloc(sizeof(SLIST));
q->data=a[i];
p->next=q;
p=q;
}
p->next=0;
return h;
}
void outlist(SLIST *h)
{
SLIST *p;
p=h->next;
if (p==NULL)
printf("\nThe list is NULL!\n");
else {
printf("\nHead");
do {
printf("->%d",p->data);
p=p->next;
} while(p!=NULL);
printf("->End\n");
}
}
int main()
{
SLIST *head;
int a[N]={11,12,15,18,19,22,25,29};
head=creatlist(a);
printf("\nOutput from head:\n"); outlist(head);
printf("\nOutput from tail: \n");
while (head->next != NULL){
fun(head);
printf("\n\n");
printf("\nOutput from head again :\n");
outlist(head);
}
return 0;
}
第2关:链表求和
任务描述
本关任务:程序中函数fun的功能是计算出带有头结点的单向链表中各结点数据域中值之和作为函数值返回。请不要增行或删行,或者更改程序的结构。
相关知识
相关知识略
编程要求
请仔细阅读右侧代码,结合相关知识,在右边提示区域内进行代码补充,完成编写链表求和的小程序。
测试说明
平台会对你编写的代码进行测试:
预期输出:
输出链表数据。
输出链表数据之和。
代码如下:
#include <stdio.h>
#include <stdlib.h>
#define N 8
typedef struct list
{
int data;
struct list *next;
} SLIST;
SLIST *creatlist(int *);
void outlist(SLIST *);
int fun( SLIST *h)
{
SLIST *p; int s=0;
p=h->next;
while(p)
{
/***** 在以下一行填写代码 *****/
s+= p->data ;
/***** 在以下一行填写代码 *****/
p=p->next ;
}
return s;
}
int main()
{
SLIST *head;
int a[N]={12,87,45,32,91,16,20,48};
head=creatlist(a);
outlist(head);
/***** 在以下一行填写代码 *****/
printf("\nsum=%d\n", fun( head));
return 0;
}
SLIST *creatlist(int a[])
{
SLIST *h,*p,*q;
int i;
h=p=(SLIST *)malloc(sizeof(SLIST));
for(i=0; i<N; i++)
{
q=(SLIST *)malloc(sizeof(SLIST));
q->data=a[i];
p->next=q;
p=q;
}
p->next=0;
return h;
}
void outlist(SLIST *h)
{
SLIST *p;
p=h->next;
if (p==NULL)
printf("The list is NULL!\n");
else {
printf("\nHead ");
do{
printf("->%d", p->data);
p=p->next;
} while(p!=NULL);
printf("->End\n");
}
}
第3关:链表求平均
任务描述
本关任务:N名学生的成绩已在主函数中放入一个带头节点的链表结构中,h指向链表的头节点。函数fun的功能是求出平均分,由函数值返回。例如学生的成绩是:85,76,69,85,91,72,64,87, 则平均分应当是78.625。
相关知识
相关知识略
编程要求
请仔细阅读右侧代码,结合相关知识,在Begin-End区域内进行代码补充,完成编写链表求平均的小程序。
测试说明
平台会对你编写的代码进行测试:
预期输出:
输出链表存储的成绩数据。
输出平均分。
代码如下:
#include <stdio.h>
#include <stdlib.h>
#define N 8
struct slist
{ double s;
struct slist *next;
};
typedef struct slist STREC;
double fun( STREC *h )
{
STREC *p;
double sum=0,avg;
int n=0;
p=h->next;
while(p)
{
sum=sum+p->s;
n++;
p=p->next;
avg=sum/n;
}
return avg;
}
STREC * creat( double *s)
{ STREC *h,*p,*q; int i=0;
h=p=(STREC*)malloc(sizeof(STREC));
p->s=0;
while(i<N)
{ q=(STREC*)malloc(sizeof(STREC));
q->s=s[i]; i++; p->next=q; p=q;
}
p->next=0;
return h;
}
void outlist( STREC *h)
{ STREC *p;
p=h->next; printf("head");
do
{ printf("->%4.1f",p->s); p=p->next; }
while(p!=0);
printf("\n\n");
}
void main()
{ double s[N]={85,76,69,85,91,72,64,87},ave;
STREC *h;
h=creat( s ); outlist(h);
ave=fun( h );
printf("ave= %6.3f\n",ave);
}