填空题
请补充fun函数,该函数的功能是:将带头结点的单向链表逆置。即若原链表中从头至尾结点数据域依次为:2、4、6、8、10,逆置后,从头至尾结点数据域依次为:10、8、6、4、2。 [注意] 部分源程序给出如下。 请勿改动主函数main和其他函数中的任何内容,仅在函数fun的横线上填入所编写的若干表达式或语句。 [试题源程序] #include <stdio.h> #include <stdlib.h> #define N 5 typedef Struct node int data; struct node *next; NODE; void fun(NODE *h) NODE *p, *q, *r; p= (1) ; if ( (2) ) return; q=P->next; P->next=NULL; while (q) r=q->next; q->next=p; p=q; q= (3) ; h->next=p; NODE *creatlis (int a[]) NODE *h, *p, *q; int i; h= (NODE *)malloc(sizeof(NODE)); h>next=NULL: for(i=0; i<N; i++) q=(NODE *)malloc(sizeof(NODE)); q->data=a[i]; q->next=NULL; if(h->next==NULL) h->next=p=q; else p->next=q; p=q; return h; void ou list(NODE *h) NODE *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"); main() NODE *head; int a[N]=(2, 4, 6, 8, 10); head=creatlist(a); printf(’\nThe original list:\n"); outlist(head); fun(head); printf("\nThe list after inverting :\n"); outlist(head);
【参考答案】
[1] h->next [2] D==NULL [3] r
点击查看答案&解析