链表——逆置
给定程序中,函数fun的功能是将带头结点的单向链表逆置,即若原链表中从头至尾结点数据与依次为2、4、6、8、10,逆置后,从头至尾结点数据依次为10、8、6、4、2。
请在程序的下画线处填入正确的内容并将下画线删除,使程序得出正确的结果。
试题程序。
1 #define N 5 2 typedef struct node 3 { 4 int data; 5 struct node *next; 6 } NODE; 7 void fun(NODE *h) 8 { 9 NODE *p, *q, *r; 10 11 p = h-> 1 ; 12 if (p == 2 ) 13 return; 14 q = p->next; 15 p->next = NULL; 16 while (q) 17 { 18 r = q->next; q->next = p; 19 p = q; q = 3 ); 20 } 21 h->next = p; 22 } 23 NODE *creatlist(int a[ ]) 24 { 25 NODE *h, *p, *q; int i; 26 h = (NODE *)malloc(sizeof(NODE)); 27 h->next = NULL; 28 for (i = 0; i < N; i++) 29 { 30 q = (NODE *)malloc(sizeof(NODE)); 31 q->data = a[i]; 32 p->next = q; 33 p = q; 34 } 35 36 } 37 return h; 38 } 39 void outlist(NODE *h) 40 { 41 NODE *p; 42 p = h->next; 43 if (p == NULL) 44 printf("The list is NULL!\n"); 45 else 46 { 47 printf("\nHead "); 48 do 49 { 50 printf("->%d", p->data); 51 p = p->next; 52 } 53 while (p != NULL); 54 printf("->End\n"); 55 56 } 57 58 } 59 main( ) 60 { 61 NODE *head; 62 int a[N] = {2, 4, 6, 8, 10}; 63 head = creatlist(a); 64 printf("\nThe original list:\n"); 65 outlist(head); 66 fun(head); 67 printf("\nThe list after inverting :\n"); 68 outlist(head); 69 }
答案:
你的答案 (正确)
1 next
2 NULL
3 r