Добавил:
korayakov
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:
/*Слияние двух списков. лаб2_4 Этмо-2 2003*/
#include "iostream.h"
#include "new.h"
#include "conio.h"
#pragma hdrstop
#define ElemType int
//Структура для списка
struct Node
{ElemType Data;
Node *Next;
};
//Класс, который оперирует с самосортируещимся списком
class Clist
{Node *list;
public:
void Add(ElemType D)
{Node*temp,
*temp1 = list;
temp = new( Node );
temp->Data = D;
temp->Next = list;
if (list == NULL) list = temp; else
{
while((temp1->Next != NULL))
{ if (temp1->Next->Data > D) break;
temp1=temp1->Next;
}
if ((temp1==list)&&(temp1->Data > D))
{ temp->Next=list;
list=temp;
return;
}
if (temp1->Next==NULL)
{temp1->Next=temp;
temp->Next=NULL;
}
if (temp1->Next->Data > D)
{ temp->Next =temp1->Next;
temp1->Next=temp;
return;
}
}
};
void amalgamation(Clist& L)
{Node *temp1=L.list;
while(temp1!=NULL)
{Add(temp1->Data);
temp1=temp1->Next;
}
}
Clist(const Clist& L)
{Node *temp=L.list;
list=NULL;
while(temp!=NULL)
{Add(temp->Data);
temp=temp->Next;
}
};
/*Clist& operator+ (const Clist& L)
{Clist temp(L);;
Node *temp1=list;
while(temp1!=NULL)
{temp.Add(temp1->Data);
temp1=temp1->Next;
};
return Clist(temp);
}; */
//konstructory
Clist(){list=NULL;};
~Clist()
{Node *temp;
while(list!=NULL)
{temp=list;
list=list->Next;
cout<<temp->Data<<" ";
delete(temp);
}
};
void Show()
{Node *temp=list;
cout<<"list:";
while(temp!=NULL)
{cout<<temp->Data<<" ";
temp=temp->Next;
}
cout<<endl;
};
};
int main(int argc, char* argv[])
{ Clist h,h2;
h.Add(4);
h.Add(3);
h.Add(2);
h.Add(1);
h.Show();
h2.Add(5);
h2.Add(3);
h2.Add(7);
h2.Add(3);
h2.Show();
h.amalgamation(h2);
h.Show();
getch();
return 0;
}
//---------------------------------------------------------------------------