Создаю классический односвязный список, но вот проблема несовсем въехал в тему с перегрузкой операторов, а по заданию надо перегрузить >> << и ->, может кто подскажет как это сделать, применительно к моей проге...
#include "stdafx.h"
#include<iostream>
using namespace std;
class CData
{
public:
int a;
int b;
CData *m_pNext;
/*CData()
{
a=0;
b=0;
m_pNext=NULL;
}
}*/
CData::CData(int a1,int b1)
{
a=a1;
b=b1;
m_pNext=NULL;
}
};
class CList
{
public:
CData *m_pFirst;
CList();
void Add(CData *pNext);
void ShowAll();
CData *GetLast();
CData *FindData(int a,int b);
bool IsEmpy();
CData *GetPrev(CData *p);
bool RemoveData(int a,int b);
friend ostream & operator << (ostream &, const CList &);
};
CList::CList()
{
m_pFirst=NULL;
}
void CList::Add(CData *pNext)
{
if(m_pFirst!=NULL)
{
GetLast()->m_pNext=pNext;
}
else
{
m_pFirst=pNext;
}
}
CData *CList::GetLast()
{
CData *pCurr=m_pFirst;
if (pCurr==NULL)
{
return NULL;
}
while(pCurr->m_pNext!=NULL)
{
pCurr=pCurr->m_pNext;
}
return pCurr;
}
void CList::ShowAll()
{
CData *pCurr=m_pFirst;
if(pCurr==NULL)
{
cout <<"List is empy.\n";
}
do{
cout<<"a="<<pCurr->a<<", b="<<pCurr->b<<"\n";
}
while ((pCurr=pCurr->m_pNext)!=NULL);
}
CData *CList::FindData(int a,int b)
{
CData *pCurr=m_pFirst;
if(pCurr==NULL)
{
return NULL;
}
do{
if(pCurr->a==a && pCurr->b==b)
{
return pCurr;
}
}
while((pCurr=pCurr->m_pNext)!=NULL);
return NULL;
}
bool CList::IsEmpy()
{
return m_pFirst==NULL;
}
CData *CList::GetPrev(CData *p)
{
if(p==m_pFirst)
return NULL;
CData *pCurr=m_pFirst;
do{
if(pCurr->m_pNext==p)
{
return pCurr;
}
}
while((pCurr=pCurr->m_pNext)!=NULL);
return NULL;
}
bool CList::RemoveData(int a,int b)
{
if(IsEmpy())
return false;
CData *pData=FindData(a,b);
if(pData==NULL)
return false;
if(pData==m_pFirst)
{
m_pFirst=pData->m_pNext;
delete pData;
return true;
}
GetPrev(pData)->m_pNext=pData->m_pNext;
delete pData;
return true;
}
ostream & operator << (ostream & output, CList &a)
{
CData *pCurr=m_pFirst;
if(pCurr==NULL)
{
output <<"List is empy.\n";
}
do{
output <<"a="<<pCurr->a<<", b="<<pCurr->b<<"\n";
}
while ((pCurr=pCurr->m_pNext)!=NULL);
return output;
}
int main()
{
CList list;
CData *pData;
int a,b,c,d,e;
cout << "Enter quantity elements of spisok:\n ";
cin >> c;
for (int i=0;i<c;i++)
{
cout << "Enter a:";
cin >> a;
cout<<"\n";
cout << "Enter b:";
cin >> b;
cout <<"\n";
pData=new CData(a,b);
list.Add(pData);
list.ShowAll();
}
cout << "Enter element of spisok:\n ";
cout << "a= \n";
cin >> d;
cout << "b= \n";
cin >> e;
if(list.FindData(d,e)==NULL)
{
cout<<"Nothing\n";
}
else
{
cout<<"Find\n";
}
cout << "Enter element for deleted: \n";
cout << "a= \n";
cin >> d;
cout << "b= \n";
cin >> e;
list.RemoveData(d,e);
list.ShowAll();
}