#include<iostream>
#include<algorithm>
#include<assert.h>
using?namespace?std;
template<class?ElemType>
struct?Node
{
?//數(shù)據(jù)成員
?ElemType?data;?????????//數(shù)據(jù)域
?Node<ElemType>?*next;???????//指針域
?????????????//構(gòu)造函數(shù)
?Node();???????????//無參數(shù)的構(gòu)造函數(shù)
?Node(ElemType?e,?Node<ElemType>?*Link?=?NULL);?//有參數(shù)的構(gòu)造函數(shù)
};
template<class?ElemType>
Node<ElemType>::Node()
{
?next?=?NULL;
}
template<class?ElemType>
Node<ElemType>::Node(ElemType?e,?Node<ElemType>?*link)
{
?data?=?e;
?next?=?link;
}
//單鏈表
template<class?ElemType>
class?LinkList
{
protected:
?//單鏈表類模板的數(shù)據(jù)成員
?Node<ElemType>?*head;??//頭指針結(jié)點
?int?length;?????//元素個數(shù)
public:
?//單鏈表類模板的函數(shù)成員
?LinkList();??????//無參數(shù)構(gòu)造函數(shù)
?LinkList(ElemType?v[],?int?n);?//有參數(shù)的構(gòu)造函數(shù)
?virtual?~LinkList();???//析構(gòu)函數(shù)
?int?GetLength()const;???//求鏈表長度
?bool?IsEmpty()const;???//判斷鏈表是否為空?
?void?Clear();?????//將鏈表清空
?void?UseMerge(LinkList<ElemType>p1,?LinkList<ElemType>p2);
?Node<ElemType>*?merge(Node<ElemType>?*head1,?Node<ElemType>?*head2);?//合并
?void?output();?????//將鏈表輸出
};
//無參數(shù)的構(gòu)造函數(shù)
template<class?ElemType>
LinkList<ElemType>::LinkList()
{
?head?=?new?Node<ElemType>;??//構(gòu)造頭函數(shù)
?length?=?0;??????//單鏈表長度定為0;
}
//有參數(shù)構(gòu)造函數(shù)
template<class?ElemType>
LinkList<ElemType>::LinkList(ElemType?v[],?int?n)
{
?//排序
?for?(int?i?=?0;?i?<?n;?i++)
?{
??for?(int?j?=?1;?j?<?n?-?i;?j++)
??{
???if?(v[j]?<?v[j?-?1])
????swap(v[j],?v[j?-?1]);
??}
?}
?Node<ElemType>?*p;
?p?=?head?=?new?Node<ElemType>;?//構(gòu)造頭結(jié)點
?for?(int?i?=?0;?i?<?n;?i++)
?{
??p->next?=?new?Node<ElemType>(v[i],?NULL);
??assert(p->next);???//構(gòu)造元素結(jié)點失敗,終止程序運行
??p?=?p->next;
?}
?length?=?n;
}
//析構(gòu)函數(shù)
template<class?ElemType>
LinkList<ElemType>::~LinkList()
{
?Clear();???//清空鏈表
?delete?head;??//釋放頭結(jié)點所指空間
}
//清空鏈表
template<class?ElemType>
void?LinkList<ElemType>::Clear()
{
?Node<ElemType>?*p;
?p?=?head->next;
?while?(p?!=?NULL)
?{
??cout?<<?p->data;
??head->next?=?p->next;
??delete?p;
??p?=?head->next;
?}
?length?=?0;
}
//求鏈表長度
template<class?ElemType>
int?LinkList<ElemType>::GetLength()const
{
?return?length;
}
//判斷鏈表是否為空
template<class?ElemType>
bool?LinkList<ElemType>::IsEmpty()const
{
?Node<ElemType>?*p?=?head->next;
?if?(p?!=?NULL)
??return?false;
?return?true;
}
//調(diào)用合并
template<class?ElemType>
void?LinkList<ElemType>::UseMerge(LinkList<ElemType>p1,?LinkList<ElemType>p2)
{
?head->next?=?merge(p1.head->next,?p2.head->next);
}
//合并鏈表
template<class?ElemType>
Node<ElemType>*?LinkList<ElemType>::merge(Node<ElemType>?*head1,?Node<ElemType>?*head2)
{
?if?(head1?==?NULL)
??return?head2;
?if?(head2?==?NULL)
??return?head1;
?Node?<ElemType>?*head?=?NULL;
?if?(head1->data?<?head2->data)
?{
??head?=?head1;
??head->next?=?merge(head1->next,?head2);
?}
?else
?{
??head?=?head2;
??head->next?=?merge(head1,?head2->next);
?}
?cout?<<?"e";
?return?head;
}
//輸出鏈表
template<class?ElemType>
void?LinkList<ElemType>::output()
{
?Node<ElemType>?*p;
?p?=?head->next;
?while?(p?!=?NULL)
?{
??cout?<<?p->data?<<?",";
??p?=?p->next;
?}
}
void?main()?{
?cout?<<?"輸入第一組五個整數(shù):";
?int?v1[1];
?for?(int?i?=?0;?i?<?1;?i++)
??cin?>>?v1[i];
?cout<<"\n";
?LinkList<int>?p1(v1,?1);
?cout?<<?"輸出有序單鏈表應為:";
?p1.output();
?cout?<<?"\n";
?cout?<<?"輸入第二組五個整數(shù):";
?int?v2[1];
?for?(int?i?=?0;?i?<?1;?i++)
??cin?>>?v2[i];
?cout?<<?"\n";
?LinkList<int>?p2(v2,?0);
?cout?<<?"輸出有序單鏈表應為:";
?p2.output();
?cout?<<?"\n";
?
?LinkList<int>?p;
?p.UseMerge(p1,?p2);
?cout?<<?"合并兩個單鏈表,合并后輸出結(jié)果應為:";
?p.output();
?system("pause");
}問題出在析構(gòu)有問題
- 0 回答
- 1 關(guān)注
- 1891 瀏覽
添加回答
舉報
0/150
提交
取消