MergeList
13099 ワード
結合一方向整列チェーンテーブル
1.1テーマ説明
タイトル:コードを作成して、2つの一方向秩序チェーンテーブルの結合入力説明を実現してください.
出力の説明:
例
入力
しゅつりょく
1.2回答
1.2.1テーマの解析
テーマは2つの一方向秩序チェーンテーブルを統合し、チェーンテーブル、ポインタなどの知識点を考察することを要求している.考え方は以下の通りである.
C++コード実装:
1.3参照
C++におけるmalloc関数の詳細(転載)C++におけるtypedefとタイプ別名C言語構造体とポインタcin.get()とsystem(「pause」)
1.1テーマ説明
タイトル:コードを作成して、2つの一方向秩序チェーンテーブルの結合入力説明を実現してください.
, 1 2 3 4 5
, 2 3 4 5 6
出力の説明:
:1 2 2 3 3 4 4 5 5 6
例
入力
1 2 3 4 5
2 3 4 5 6
しゅつりょく
1 2 2 3 3 4 4 5 5 6
1.2回答
1.2.1テーマの解析
テーマは2つの一方向秩序チェーンテーブルを統合し、チェーンテーブル、ポインタなどの知識点を考察することを要求している.考え方は以下の通りである.
1. , ;
2. , , , ;
3. ;
4. ;
5.
C++コード実装:
// cin、cout、cerr clog , 、 、 。
#include
// Mac 'malloc.h' file not found, num_bytes [C++ malloc ( )](https://blog.csdn.net/sinat_27456831/article/details/50834965)
#include
//vector( ): C++ , . , , .
#include
using namespace std;
// , LNode ( ),*Linklist [C++ typedef ](https://blog.csdn.net/csxiaoshui/article/details/78038799)
typedef struct Node
{
int data;
struct Node* next;
}LNode,*Linklist;
//
void PrintList(Linklist head)
{
Linklist tmp=head;
while(tmp!=NULL)
{
cout<<tmp->data<<" ";
tmp=tmp->next;
}
}
//
Linklist CreatList(Linklist head)
{
// head( ),end( ),node( )
head=(Linklist)malloc(sizeof(struct Node));
// node end
LNode* node=NULL;
LNode* end=NULL;
int num;
vector<int> vec;
head->next=NULL;
end=head;
// cin ;cin.get() cin ,
while(cin>>num){
vec.push_back(num);
if(cin.get()=='
')
break;
}
//
// node , node end->next ( end ),end
for(int i=0;i<vec.size();i++){
node=(Linklist)malloc(sizeof(struct Node));
node->data=vec[i];
// ,end->next = node node end ,end=node end
end->next=node;
end=node;
}
end->next=NULL;
return head;
}
//
Linklist MergeList(Linklist head1,Linklist head2)
{
Linklist mergeHead=(Linklist)malloc(sizeof(struct Node));
if(head1==NULL)
return head2;
else if(head2==NULL)
return head1;
if(head1->data<head2->data)
{
mergeHead=head1;
mergeHead->next=MergeList(head1->next,head2);
}
else if (head1->data>=head2->data)
{
mergeHead=head2;
mergeHead->next=MergeList(head1,head2->next);
}
return mergeHead;
}
int main()
{
Linklist head1,head2,head3=NULL;
head1=CreatList(head1);
head2=CreatList(head2);
head3=MergeList(head1->next,head2->next);
PrintList(head3);
return 0;
}
1.3参照
C++におけるmalloc関数の詳細(転載)C++におけるtypedefとタイプ別名C言語構造体とポインタcin.get()とsystem(「pause」)