给定一个链表结构,其中每个节点代表一个链表和 包含其类型的两个指针:函数变平到单个列表
(I)指向下一个节点中的主列表。 (ii)指向该节点头部的链接列表的指针。
编写一个C函数将列表平铺到单个链表中。
例如,
如果给定链表
1 -- 5 -- 7 -- 10
| | |
2 6 8
| |
3 9
|
4
然后将其转换为
1 - 2 - 3 - 4 - 5 - 6 - 9 - 7 - 8 -10
我的解决方案
struct node {
int data;
struct node *fwd; //pointer to next node in the main list.
struct node *down; //pointer to a linked list where this node is head.
}*head,*temp,*temp2;
temp=head;
while(temp->fwd!=NULL) {
temp2=temp->fwd;
while(temp->down!=NULL) {
temp=temp->down;
}
temp->down=temp2;
temp->fwd=NULL;
temp=temp2;
}
PLZ通知我如果有什么......其他的解决方案和优化是欢迎
我的解决办法: 结构节点 { int数据; struct node * fwd; //指向主列表中下一个节点的指针。 struct node * down; //指向此节点所在链接列表的指针。 } * head,* temp,* temp2; temp = head; (temp-> fwd!= NULL) temp2 = temp-> fwd; (temp-> down!= NULL) temp = temp-> down; } temp-> down = temp2; temp-> fwd = NULL; temp = temp2; } PLZ通知我,如果有任何其他解决方案和优化,欢迎 – 2010-12-20 07:56:50
:::::作业? – wilhelmtell 2010-12-20 07:56:58
@wilhelmtell:从写作的方式来猜测你是对的。 – sjngm 2010-12-20 08:05:17