链表

来源:互联网 发布:讨鬼传极捏脸数据 编辑:程序博客网 时间:2024/05/21 07:51

两个有序的单链表,合并为一个有序的单链表

#define   _CRT_SECURE_NO_WARNINGS#include<iostream>#include<cstdio>#include<cstdlib>#include<string>#include<cstdarg>using namespace std;typedef int element;class LinkList{public:class NODE{public:element data;NODE *next;NODE(element e = 0){this->data = e;this->next = NULL;}};LinkList();    LinkList(int n,...);LinkList(const LinkList &ref);LinkList& operator=(const LinkList &ref);~LinkList();bool IsEmpty();int GetLength();void InsetByHead(element e);void InsetByTail(element e);bool InsertByPos(int pos, element e);bool DeleteByPos(int pos,element *e);bool DeleteHead();bool DeleteTail();element GetPrioNodeByValue(element e);element GetPrioNodeByPos(int pos);void Show();void Merge(LinkList &src){if(phead == src.phead)return;Merge_Link(phead,src.phead);src.phead->next= NULL;}private:NODE *phead;void Merge_Link(NODE *head1,NODE *head2){if(head1 == NULL ||head2 == NULL){return ;}NODE *p = head1;NODE *s = head1->next;NODE *q = head2->next ;while(s!= NULL && q != NULL){if(s->data>=q->data){p->next = s;p = p->next ;s = s->next;}else{p->next = q;p = p->next ;q = q->next;}}if(s != NULL){p->next = s;}if(q != NULL){p->next = q;}}};LinkList::LinkList(){this->phead  = new NODE(0);this->phead->next  = NULL;    cout<<" 调用无参构造函数 "<<this<<endl;}LinkList::LinkList(int n,...){this->phead  = new NODE(0);this->phead->next = NULL;va_list parameter;va_start(parameter,n);int i;for(i = 0; i<n; i++){this->InsetByTail(va_arg(parameter,int));//this->InsetByHead(va_arg(parameter,int));}va_end(parameter); cout<<" 调用可变参数列表构造函数 "<<this<<endl;}LinkList::LinkList(const LinkList &ref){this->phead  = new NODE(0);this->phead->next = NULL;NODE *p = this->phead ;NODE *s = ref.phead->next;element tmp;while(s != NULL){tmp = s->data ;p->next  = new NODE(tmp);p->next->next  = NULL;p = p->next ;s = s->next ;}cout<<"  调用拷贝构造函数  "<<this<<"<---"<<&ref<<endl;}LinkList& LinkList::operator=(const LinkList &ref){if(this == &ref){return *this;}if(ref.phead  == NULL){return *this;}NODE *p = this->phead->next;while(p != NULL){this->phead->next = p->next;delete p;p = this->phead->next;}//this->phead->next = NULL;p = this->phead ;NODE *s = ref.phead->next;element tmp;while(s != NULL){tmp = s->data ;p->next  = new NODE(tmp);p->next->next  = NULL;p = p->next ;s = s->next ;}return *this;cout<<" 调用赋值运算符重载函数 "<<this<<"<---"<<&ref<<endl;}LinkList::~LinkList(){NODE *p = this->phead->next;while(p != NULL){this->phead->next = p->next;delete p;p = this->phead->next;}delete this->phead;this->phead  = NULL;cout<<" 调用析构函数 "<<this<<endl;}bool LinkList::IsEmpty(){if(this->phead  == NULL ){cout<<"此链表不存在"<<endl;return false;}if(this->phead->next  == NULL ){return true;}else{return false;}}int LinkList::GetLength(){if(this->phead  == NULL || this->phead->next  == NULL ){return -1;}NODE *p = this->phead->next;int length = 0;while(p != NULL){length++;p = p->next;}return length;}void LinkList::InsetByHead(element e){if(this->phead  == NULL){cout<<"链表不存在,头插尾插"<<endl;return ;}NODE *p = this->phead ;NODE *tmp = new NODE(e);tmp->next  = p->next;p->next  = tmp;}void LinkList::InsetByTail(element e){if(this->phead  == NULL){cout<<"链表不存在,无法尾插"<<endl;return ;}NODE *p = this->phead  ;while(p->next != NULL){p = p->next ;}p->next  = new NODE(e);p->next ->next  = NULL;//cout<<"  调用尾插函数  "<<this<<endl;}bool LinkList::InsertByPos(int pos, element e){if(this->phead  == NULL){return false;}if(pos<1||pos>this->GetLength()+1){return false;}NODE *p = this->phead;NODE *tmp = new NODE(e);int k = 1;while(1){if(k == pos){break;}p = p->next ;k++;}tmp->next = p->next ;p->next  = tmp;return true;}bool LinkList::DeleteByPos(int pos,element *e){if(this->phead  == NULL){return false;}if(pos<1||pos>this->GetLength()){return false;}NODE *p = this->phead;NODE *s ;int k = 1;while(1){if(k == pos){break;}p = p->next ;k++;}s = p->next ;p->next  = s->next ;*e = s->data ;delete s;return true;}bool LinkList::DeleteHead(){if(this->phead  == NULL ||this->phead->next  == NULL){return false;}NODE *tmp = this->phead->next;this->phead->next = tmp->next ;delete tmp;return true;}bool LinkList::DeleteTail(){if(this->phead  == NULL ||this->phead->next  == NULL){return false;}NODE *p = this->phead;NODE *s = p->next ;while(s->next != NULL ){p = p->next;s = p->next;}p->next  = NULL;delete s;return true;}element LinkList::GetPrioNodeByValue(element e){if(this->phead  == NULL ||this->phead->next  == NULL){return -1;}NODE *p = this->phead ;NODE *s = this->phead->next ;while(s != NULL){if(s->data  == e && s != this->phead->next){return p->data ;break;}if(s->data  == e && s == this->phead->next){return -1;//没有前驱}p = p->next ;s = p->next ;}return -1;}element LinkList::GetPrioNodeByPos(int pos){if(this->phead  == NULL ||this->phead->next  == NULL){return -1;}if(pos<2||pos>=this->GetLength()){return -1;}NODE *p = this->phead->next ;NODE *s = p->next ;int k = 2;while(1){if(k == pos){return p->data ;break;}p = p->next ;s = p->next ;k++;}}void LinkList::Show(){if(this->phead  == NULL || this->phead->next  == NULL ){return ;}NODE *p = this->phead->next  ;while(p != NULL){cout<<p->data<<"  ";p = p->next ;}cout<<endl;} 
测试用例如下

int main(){LinkList list1;LinkList list2;list1.InsetByHead(10);list1.InsetByHead(14);list1.InsetByHead(17);list1.InsetByHead(29);list1.InsetByHead(39);list1.InsetByHead(89);list2.InsetByHead(6);list2.InsetByHead(18);list2.InsetByHead(45);list2.InsetByHead(55);list2.InsetByHead(90);list1.Show();list2.Show();list1.Merge(list2);list1.Show();}
结果如下


调用无参构造函数 003FF7EC
 调用无参构造函数 003FF7E0
89  39  29  17  14  10
90  55  45  18  6
90  89  55  45  39  29  18  17  14  10  6
 调用析构函数 003FF7E0
 调用析构函数 003FF7EC
请按任意键继续. . .