# 2-11. 两个有序链表序列的合并（15）:链表数据结构基础练习

#include<iostream>
using namespace std;

typedef struct Node
{
int data;
struct Node * next;

int main()
{
int num;

List_1 = new Node;
List_1->next = NULL;

List_2 = new Node;
List_2->next = NULL;

Node *T = List_1;

bool flag =1;
while( flag ){
cin>>num;
if( num!=-1 )
{
Node *temp = new Node;
temp->data = num;
T->next = temp;
T = temp;
}
else
{
flag =0;
T->next = NULL;
}
}

flag = 1;
T = List_2;
while( flag )
{
cin>>num;
if( num!=-1 )
{
Node *temp = new Node;
temp->data = num;
T->next = temp;
T = temp;
}
else
{
flag =0;
T->next = NULL;
}
}

if( List_1->next==NULL && List_2->next==NULL )
{
cout<<"NULL"<<endl;
return 0;
}

Node *P1, *P2;
P1 = List_1->next;
P2 = List_2->next;

bool first = true;
while( P1!=NULL && P2 != NULL )
if( P1->data <= P2->data )
{
if( first ){
cout<<P1->data;
first = false;
}
else
cout<<" "<<P1->data;
P1 = P1->next;
}
else
{
if( first ){
cout<<P2->data;
first = false;
}
else
cout<<" "<<P2->data;
P2 = P2->next;
}

if( P1 )
while( P1 )
{
cout<<" "<<P1->data;
P1=P1->next;
}
if( P2 )
while( P2 )
{
cout<<" "<<P2->data;
P2=P2->next;
}
return  0;
}


• 本文已收录于以下专栏：

举报原因： 您举报文章：2-11. 两个有序链表序列的合并（15）:链表数据结构基础练习 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)