探討:將兩個鏈表非降序合并為一個鏈表并依然有序的實現(xiàn)方法
更新時間:2013年05月29日 11:07:39 作者:
本篇文章是對將兩個鏈表非降序合并為一個鏈表并依然有序的實現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
已知兩個鏈表list1和list,2,各自非降序排列,將它們合并成另外一個鏈表list3,并且依然有序,要求保留所有節(jié)點。
實現(xiàn)過程中,list1中的節(jié)點和list2中的節(jié)點都轉(zhuǎn)移到了list3中,注意泛型的友元函數(shù)的用法。
程序如有不足之處,還望指正?。?!
定義List類
#include "stdafx.h"
#include <iostream>
using namespace std;
template<class T>
struct Node
{
T data;
Node<T> * next;
};
template <class T>
class MyList
{
public:
//構(gòu)造函數(shù),初始化一個頭結(jié)點,data為空,next指向第一個節(jié)點
MyList()
{
phead = new Node<T>;
phead->data = NULL;
phead->next = NULL;
}
//析構(gòu)函數(shù),將整個鏈表刪除,這里采用的是正序撤銷
~MyList()
{
Node<T>* p;
p = phead;
while (p)
{
Node<T>* q;
q = p;
p = p->next;
delete q;
}
}
//復(fù)制構(gòu)造函數(shù)
MyList(MyList& mylist)
{
Node<T>* q = mylist.phead->next;
Node<T>* pb = new Node<T>;
this->phead = pb;
while (q != NULL)
{
Node<T>* p = new Node<T>;
p->data = q->data;
p->next = NULL;
pb->next = p;
pb = p;
q = q->next;
}
}
//插入一個元素的方法,在第i個元素插入一個元素e,
//返回值為NOTE<T>型指針,指向插入的那個元素
Node<T>* Insert(int i, T e)
{
//在鏈表的第i個位置,插入一個元素e
int j = 0;
Node<T>* p;
p = phead;
while (p && j < i - 1)
{
p = p->next;
++j;
}
if (!p || j > i -1)
{
return phead;
}
Node<T>* q;
q = new Node<T>;
q->data = e;
q->next = p->next;
p->next = q;
return q;
}
//輸出list中的元素
void Show()
{
Node<T> *p = phead->next;
while (NULL != p)
{
cout << p->data << " ";
p = p->next;
}
}
template<class T> friend void MergeList(MyList<T> &list1, MyList<T> &list2, MyList<T> &list3);
private:
Node<T>* phead;};
<PRE class=cpp name="code">// </PRE><PRE class=cpp name="code">//將兩個鏈表合并成一個鏈表,并且依然有序。方法保留了合并之前l(fā)ist1和list2的節(jié)點,
//合并之后list1和list2消失。將list1和list2合并為list3
template<class T>
void MergeList(MyList<T> &list1, MyList<T> &list2, MyList<T> &list3)
{
Node<T> *head1 = list1.phead, *head2 = list2.phead;
Node<T> *head3 = list3.phead, *temp = NULL;
if (head1->next == NULL)
{//如果list1為空,則list3頭指針指向list2
head3 = head2;
list2.phead->next = NULL;//將list2消除,防止析構(gòu)函數(shù)析構(gòu)list2時找不到對象
}
else if (head2->next == NULL)
{//如果list1為空,則list3頭指針指向list2
head3 = head1;
list1.phead->next = NULL;//將list1消除,防止析構(gòu)函數(shù)析構(gòu)list2時找不到對象
}
head1 = head1->next;
list1.phead->next = NULL;//將list1消除,防止析構(gòu)函數(shù)析構(gòu)list2時找不到對象
head2 = head2->next;
list2.phead->next = NULL;//將list2消除,防止析構(gòu)函數(shù)析構(gòu)list2時找不到對象
if (head1->data < head2->data)
{//如果list1的第一個元素小于list2的第一個元素
head3->next = head1;//將list1的第一個元素接到list3上
head1 = head1->next;
}
else
{
head3->next = head2;//將list2的第一個元素接到list3上
head2 = head2->next;
}
temp = head3->next;//指向list3當(dāng)前最后一個節(jié)點
while (head1 != NULL && head2 != NULL)
{
if (head1->data < head2->data)
{
temp->next = head1;//將list1中的元素接到list3的后面
temp = head1;
head1 = head1->next;
}
else
{
temp->next = head2;//將list2中的元素接到list3的后面
temp = head2;
head2 = head2->next;
}
}
if (NULL == head1) //將list1或者list2中的剩余部分接到list3的后面
{
temp->next = head2;
}
else if (NULL == head2)
{
temp->next = head1;
}
}<PRE class=cpp name="code"> </PRE><PRE class=cpp name="code">//主函數(shù)</PRE><PRE class=cpp name="code">int _tmain(int argc, _TCHAR* argv[])
{
MyList<int> list1, list2, list3;
for (int i = 1; i <= 10; i ++)
{
list1.Insert(i, 3*i);
list2.Insert(i, 2*i);
}
MergeList(list1, list2, list3);
list3.Show();
return 0;
}</PRE><BR>
<PRE></PRE>
<PRE></PRE>
<PRE></PRE>
<PRE></PRE>
<PRE></PRE>
<PRE></PRE>
<PRE></PRE>
<PRE></PRE>
</PRE>
實現(xiàn)過程中,list1中的節(jié)點和list2中的節(jié)點都轉(zhuǎn)移到了list3中,注意泛型的友元函數(shù)的用法。
程序如有不足之處,還望指正?。?!
定義List類
復(fù)制代碼 代碼如下:
#include "stdafx.h"
#include <iostream>
using namespace std;
template<class T>
struct Node
{
T data;
Node<T> * next;
};
template <class T>
class MyList
{
public:
//構(gòu)造函數(shù),初始化一個頭結(jié)點,data為空,next指向第一個節(jié)點
MyList()
{
phead = new Node<T>;
phead->data = NULL;
phead->next = NULL;
}
//析構(gòu)函數(shù),將整個鏈表刪除,這里采用的是正序撤銷
~MyList()
{
Node<T>* p;
p = phead;
while (p)
{
Node<T>* q;
q = p;
p = p->next;
delete q;
}
}
//復(fù)制構(gòu)造函數(shù)
MyList(MyList& mylist)
{
Node<T>* q = mylist.phead->next;
Node<T>* pb = new Node<T>;
this->phead = pb;
while (q != NULL)
{
Node<T>* p = new Node<T>;
p->data = q->data;
p->next = NULL;
pb->next = p;
pb = p;
q = q->next;
}
}
//插入一個元素的方法,在第i個元素插入一個元素e,
//返回值為NOTE<T>型指針,指向插入的那個元素
Node<T>* Insert(int i, T e)
{
//在鏈表的第i個位置,插入一個元素e
int j = 0;
Node<T>* p;
p = phead;
while (p && j < i - 1)
{
p = p->next;
++j;
}
if (!p || j > i -1)
{
return phead;
}
Node<T>* q;
q = new Node<T>;
q->data = e;
q->next = p->next;
p->next = q;
return q;
}
//輸出list中的元素
void Show()
{
Node<T> *p = phead->next;
while (NULL != p)
{
cout << p->data << " ";
p = p->next;
}
}
template<class T> friend void MergeList(MyList<T> &list1, MyList<T> &list2, MyList<T> &list3);
private:
Node<T>* phead;};
復(fù)制代碼 代碼如下:
<PRE class=cpp name="code">// </PRE><PRE class=cpp name="code">//將兩個鏈表合并成一個鏈表,并且依然有序。方法保留了合并之前l(fā)ist1和list2的節(jié)點,
//合并之后list1和list2消失。將list1和list2合并為list3
template<class T>
void MergeList(MyList<T> &list1, MyList<T> &list2, MyList<T> &list3)
{
Node<T> *head1 = list1.phead, *head2 = list2.phead;
Node<T> *head3 = list3.phead, *temp = NULL;
if (head1->next == NULL)
{//如果list1為空,則list3頭指針指向list2
head3 = head2;
list2.phead->next = NULL;//將list2消除,防止析構(gòu)函數(shù)析構(gòu)list2時找不到對象
}
else if (head2->next == NULL)
{//如果list1為空,則list3頭指針指向list2
head3 = head1;
list1.phead->next = NULL;//將list1消除,防止析構(gòu)函數(shù)析構(gòu)list2時找不到對象
}
head1 = head1->next;
list1.phead->next = NULL;//將list1消除,防止析構(gòu)函數(shù)析構(gòu)list2時找不到對象
head2 = head2->next;
list2.phead->next = NULL;//將list2消除,防止析構(gòu)函數(shù)析構(gòu)list2時找不到對象
if (head1->data < head2->data)
{//如果list1的第一個元素小于list2的第一個元素
head3->next = head1;//將list1的第一個元素接到list3上
head1 = head1->next;
}
else
{
head3->next = head2;//將list2的第一個元素接到list3上
head2 = head2->next;
}
temp = head3->next;//指向list3當(dāng)前最后一個節(jié)點
while (head1 != NULL && head2 != NULL)
{
if (head1->data < head2->data)
{
temp->next = head1;//將list1中的元素接到list3的后面
temp = head1;
head1 = head1->next;
}
else
{
temp->next = head2;//將list2中的元素接到list3的后面
temp = head2;
head2 = head2->next;
}
}
if (NULL == head1) //將list1或者list2中的剩余部分接到list3的后面
{
temp->next = head2;
}
else if (NULL == head2)
{
temp->next = head1;
}
}<PRE class=cpp name="code"> </PRE><PRE class=cpp name="code">//主函數(shù)</PRE><PRE class=cpp name="code">int _tmain(int argc, _TCHAR* argv[])
{
MyList<int> list1, list2, list3;
for (int i = 1; i <= 10; i ++)
{
list1.Insert(i, 3*i);
list2.Insert(i, 2*i);
}
MergeList(list1, list2, list3);
list3.Show();
return 0;
}</PRE><BR>
<PRE></PRE>
<PRE></PRE>
<PRE></PRE>
<PRE></PRE>
<PRE></PRE>
<PRE></PRE>
<PRE></PRE>
<PRE></PRE>
</PRE>
相關(guān)文章
C++實現(xiàn)圖的鄰接矩陣存儲和廣度、深度優(yōu)先遍歷實例分析
這篇文章主要介紹了C++實現(xiàn)圖的鄰接矩陣存儲和廣度、深度優(yōu)先遍歷,實例分析了C++實現(xiàn)圖的遍歷技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04C++分析類的對象作類成員調(diào)用構(gòu)造與析構(gòu)函數(shù)及靜態(tài)成員
終于到了對象的初始化和清理的最后階段了,在這里分享一個cpp里有多個類時,一個類的對象作為另一個類成員的時候構(gòu)造函數(shù)和析構(gòu)函數(shù)調(diào)用的時機。還有一個靜態(tài)成員也是經(jīng)??嫉降狞c,在這篇博客將會詳解其概念并舉出案例鞏固,讓我們開始2022-05-05C++的template模板中class與typename關(guān)鍵字的區(qū)別分析
這篇文章中我們來談一談C++的template模板中class與typename關(guān)鍵字的區(qū)別分析,同時會講到嵌套從屬名稱時的一些注意點,需要的朋友可以參考下2016-06-06C/C++實現(xiàn)矩陣的轉(zhuǎn)置(示例代碼)
C/C++實現(xiàn)矩陣的轉(zhuǎn)置(示例代碼)需要的朋友可以過來參考下,希望對大家有所幫助2013-10-10C語言實現(xiàn)二叉樹鏈?zhǔn)浇Y(jié)構(gòu)的示例詳解
這篇文章主要為大家詳細(xì)介紹了C語言實現(xiàn)二叉樹鏈?zhǔn)浇Y(jié)構(gòu)的相關(guān)資料,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C語言有一定的幫助,需要的可以參考一下2022-11-11