【发布时间】:2015-03-26 17:49:29
【问题描述】:
我正在合并两个链接列表,第三个列表由交替位置的元素组成,但将合并的功能不起作用
void insert2()
{
//node ptr-pointer of first linked list<br>
//node1 ptr1-pointer of second list<br>
//node2 ptr2 -pointer of the merged linked list
node *ptr=head;
node1 *ptr1=head1;
node2 *ptr2=(node2*)malloc(sizeof(node2));
node *ptr3;
while(ptr!=NULL&&ptr1!=NULL)
{
//Entering the element of first linked list
ptr2->info=ptr->info;
if(head2==NULL)
{
ptr->next=NULL;
head=ptr;
}
else
{
ptr3=head2;
while(ptr3->next!=NULL)
{
ptr3=ptr3->next;
}
ptr3->next=ptr2;
ptr2->next=NULL;
}
//Entering the element of second linked list
ptr2->info=ptr1->info;
while(ptr3->next!=NULL)
{
ptr3=ptr3->next;
}
ptr3->next=ptr2;
ptr2->next=NULL;
}
}
【问题讨论】:
-
问题是什么?
-
Input-2 3 5 6 7// list 1 Input- 8 9 10 11 Output 应该是-2 8 3 9 5 10 7 11 但我上面给出的功能不起作用跨度>
-
如果列表长度不同怎么办?有许多相关的问题,其中一些列在右侧。 Merging two sorted linked lists 接近你所追求的;您的数据未明确排序,因此您的比较更容易。关于简单地合并两个列表中的替代值,我还没有发现其他一些问题。
-
你的代码的输出是什么?
-
您既没有提供输入函数也没有提供输出函数,也没有提供调用您提供的函数的代码,因此我们很难确定可能出了什么问题。此外还不清楚为什么您有三种不同的节点类型。
标签: c data-structures linked-list