// List.h
template
{
class List
{
public:
Data d;
List *next;
List(Data key){d=key; next=0;}
};
List *beg;
public:
SingleList(){beg=0;}
~SingleList();
void add(Data d);
int remove(Data d);
int removeplace(int n);
List * insert(Data key, Data d);
List * find(Data d);
void print();
SingleList & operator +(const SingleList & Z);
SingleList & operator =(const SingleList &Z);
};
template
SingleList ::~SingleList()
{
if (beg!=0){
List *p=beg;
while (p){
p=p->next;
delete beg;
beg=p;
}
}
}
template
void SingleList ::print()
{
List *p=beg;
cout<<"List: ";
while (p)
{
cout<
p=p->next;
}
}
template
void SingleList::add(Data d){
List *p= new List(d);
if (beg==0) beg=p;
else
{
List *tmp=beg;
while (tmp->next) tmp=tmp->next;
tmp->next=p;
p->next=0;
}
}
template
SingleList & SingleList ::operator +(const SingleList & Z)
{
SingleList *pnt=new SingleList();
SingleList *tmp=this->beg,*tmp2=pnt->beg;
while (tmp->next!=0)
{
tmp2->next=new List();
tmp2->next->data=tmp->next->data;
tmp=tmp->next;
tmp2=tmp2->next;
}
tmp=*(Z.list);
while (tmp->next!=0)
{
tmp2->next=new List();
tmp2->next->data=tmp->next->data;
tmp=tmp->next;
tmp2=tmp2->next;
}
tmp2->next=0;
return *pnt;
}
template
SingleList & SingleList::operator =(const SingleList &Z)
{
List *tmp=beg, *tmp2=Z.beg.next;
tmp->data=Z.data;
while (tmp2!=0)
{
if (tmp->next==0)
tmp->next=new SingleList(tmp2->next->data);
else
tmp->next->data=tmp2->data;
tmp2=tmp2->next;
tmp=tmp->next;
}
tmp->next=0;
}
template
List * SingleList::find(Data d){
List *p=beg;
while(p)
{
if (p->d==d) return p;
p=p->next;
}
return 0;
}
template
List * SingleList::insert(Data key, Data d){
if (List *place=find(key))
{
List *p=new List(d);
p->next=place->next;
place->next=p;
}
return 0;
}
template
int SingleList::removeplace(int n)
{
List * tmp;
if (n<1) return 0;
if (n==1)
{
tmp=beg->next;
delete beg;
beg=tmp;
return 1;
}
List *p=beg;
for (int i=0;i
{
if (p=0) return 0;
p=p->next;
}
tmp=p->next;
p->next=tmp->next;
delete tmp;
}
template
int SingleList::remove(Data d)
{
if (beg->d==d)
{
removeplace(1);
return 1;
}
List *p=beg;
while(p->next)
{
if (p->next->d==d)
{
List *tmp=p->next;
p->next=tmp->next;
delete tmp;
return 1;
}
p=p->next;
}
return 0;
}

Этот комментарий был удален автором.
ОтветитьУдалить// List.h
ОтветитьУдалитьtemplate class SingleList
{
class List
{
public:
Data d;
List *next;
List(Data key){d=key; next=0;}
};
List *beg;
public:
SingleList(){beg=0;}
~SingleList();
void add(Data d);
int remove(Data d);
int removeplace(int n);
List * insert(Data key, Data d);
List * find(Data d);
void print();
SingleList & operator +(const SingleList & Z);
SingleList & operator =(const SingleList &Z);
};
template
SingleList ::~SingleList()
{
if (beg!=0){
List *p=beg;
while (p){
p=p->next;
delete beg;
beg=p;
}
}
}
template
void SingleList ::print()
{
List *p=beg;
cout<<"List: ";
while (p)
{
cout<d<<' ';
p=p->next;
}
}
template
void SingleList::add(Data d){
List *p= new List(d);
if (beg==0) beg=p;
else
{
List *tmp=beg;
while (tmp->next) tmp=tmp->next;
tmp->next=p;
p->next=0;
}
}
template
SingleList & SingleList ::operator +(const SingleList & Z)
{
SingleList *pnt=new SingleList();
SingleList *tmp=this->beg,*tmp2=pnt->beg;
while (tmp->next!=0)
{
tmp2->next=new List();
tmp2->next->data=tmp->next->data;
tmp=tmp->next;
tmp2=tmp2->next;
}
tmp=*(Z.list);
while (tmp->next!=0)
{
tmp2->next=new List();
tmp2->next->data=tmp->next->data;
tmp=tmp->next;
tmp2=tmp2->next;
}
tmp2->next=0;
return *pnt;
}
template
SingleList & SingleList::operator =(const SingleList &Z)
{
List *tmp=beg, *tmp2=Z.beg.next;
tmp->data=Z.data;
while (tmp2!=0)
{
if (tmp->next==0)
tmp->next=new SingleList(tmp2->next->data);
else
tmp->next->data=tmp2->data;
tmp2=tmp2->next;
tmp=tmp->next;
}
tmp->next=0;
}
template
List * SingleList::find(Data d){
List *p=beg;
while(p)
{
if (p->d==d) return p;
p=p->next;
}
return 0;
}
template
List * SingleList::insert(Data key, Data d){
if (List *place=find(key))
{
List *p=new List(d);
p->next=place->next;
place->next=p;
}
return 0;
}
template
int SingleList::removeplace(int n)
{
List * tmp;
if (n<1) return 0;
if (n==1)
{
tmp=beg->next;
delete beg;
beg=tmp;
return 1;
}
List *p=beg;
for (int i=0;inext;
}
tmp=p->next;
p->next=tmp->next;
delete tmp;
}
template
int SingleList::remove(Data d)
{
if (beg->d==d)
{
removeplace(1);
return 1;
}
List *p=beg;
while(p->next)
{
if (p->next->d==d)
{
List *tmp=p->next;
p->next=tmp->next;
delete tmp;
return 1;
}
p=p->next;
}
return 0;
}