问题 填空题

请补充函数fun(),该函数的功能是建立一个带头结点的单向链表并输出到文件“out98.dat”和屏幕上,各结点的值为对应的下标,链表的结点数及输出的文件名作为参数传入。
注意:部分源程序给出如下。
请勿改动主函数main 和其他函数中的任何内容,仪在函数fun()的横线上填入所编写的若干表达式或语句。
试题程序:
#include<stdio. h>
#include<conio. h>
#include<stdlib. h>
typedef struct ss

int data;
struct ss *next;
NODE;
void fun(int n,char*filename)

NODE *h,*p, *s;
FILE *pf;
int i;
h=p= (NODE *) malloc (sizeof (NODE));
h->data=0;
for (i=1; i
s=(NODE *)malloc (sizeof (NODE));
s->data= 【1】 ;
【2】 ;
p= 【3】

p->next=NULL;
if ( (pf=fopen (filename, "w") ) ==NULL)

printf "Can not open out9B.clat! ");
exit (0);

p=h;
fprintf (pf, "\n***THE LIST***\n");
print f ("\n***THE LIST***\n")
while (p)

fprintf (pf, "%3d", p->data)
printf ("%3d",p->data);
if (p->next ! =NULL)

fprintf (pf, "->");
printf ("->");

p=p->next;

fprintf (pf, "\n");
printf ("\n");
fclose (pf);
p=h;
while (p)

s=p;
p=p->next;
free (s);


main()

char * filename="out98. dat";
int n;
clrscr ();
printf (" \nInput n: ");
scanf ("%d", &n);
fun (n, filename);

答案

参考答案:[1] i [2] p->next=s [3] p->next

解析: 填空1:题目要求各结点的值为对应的下标,头结点的值为0,其他结点的值从1开始,所以此空应填i。填空2:为了将结点p和结点s连接起来,应将结点p的next 指针指向结点s。填空3:为了通过for 循环形成链表,每执行完一次循环操作,都要将指针p 指向下一个结点。

单项选择题
判断题