完成两个顺序表的合并(用C语言)

来源:百度知道 编辑:UC知道 时间:2024/06/27 22:02:03
要求:两个顺序表通过键盘输入,输出合并后的结果.麻烦高手帮我一下,谢谢了~
问题要求要从键盘输入两个顺序表,这个答案好像没有这一点啊,不过不是非常感谢~希望帮忙补充一下

struct Node
{
int data ;
Node *next ;
};
typedef struct Node Node ;
Node * Merge(Node *head1 , Node *head2)
{
if ( head1 == NULL)
return head2 ;
if ( head2 == NULL)
return head1 ;
Node *head = NULL ;
Node *p1 = NULL;
Node *p2 = NULL;
if ( head1->data < head2->data )
{
head = head1 ;
p1 = head1->next;
p2 = head2 ;
}
else
{
head = head2 ;
p2 = head2->next ;
p1 = head1 ;
}
Node *pcurrent = head ;
while ( p1 != NULL && p2 != NULL)
{
if ( p1->data <= p2->data )
{
pcurrent->next = p1 ;
pcurrent = p1 ;
p1 = p1->next ;
}
else
{
pcurrent->next = p2 ;
pcurrent = p2 ;
p2 = p2->next ;
}
}
if ( p1 != NULL )
pcurrent->next = p1 ;
if ( p2 != NULL )
pcurrent->next = p2 ;
return head ;
}