下面的C函数将整数的单链表作为参数,并重新排列列表中的元素。调用该函数时,将使用按给定顺序包含整数1、2、3、4、5、6、7的列表。函数完成执行后,列表的内容是什么?
null
struct node { int value; struct node *next; }; void rearrange( struct node *list) { struct node *p, * q; int temp; if ((!list) || !list->next) return ; p = list; q = list->next; while (q) { temp = p->value; p->value = q->value; q->value = temp; p = q->next; q = p?p->next:0; } } |
(A) 1,2,3,4,5,6,7 (B) 2,1,4,3,6,5,7 (C) 1,3,2,5,4,7,6 (D) 2,3,4,5,6,7,1
答复: (B) 说明:
给定的链表是1->2->3->4->5->6->7
如果你仔细观察给定的函数,
它只是交换每一对的相邻值,直到到达终点。
修改后的链表为2->1->4->3->6->5->7
见本报告问题4 https://www.geeksforgeeks.org/data-structures-and-algorithms-set-15/
这个解决方案是由 阿尼尔·赛克里希纳·德瓦拉塞蒂 这个问题的小测验
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END