Mam dwie klasy element i lista ,które mam wykorzystać do wczytania pliku tekstowego a następnie do zapisania tego samego pliku z odwróconą kolejnością linii tekstu.
element.h:
#include <string>
using namespace std;
class Element{
private:
string line;
Element* nextEl;
public:
string getLine(){return line;};
Element* getNext(){return nextEl;};
void setLine(string l){line=l;};
//char*konwersja(){};
Element(){};
Element(const Element& c);
Element& operator=(const Element& c);
~Element();
void setNext(Element* n){nextEl=n;}
}
element.cpp:
#include "element.h"
Element :: Element(const Element& c)
{
string line = c.getLine();
Element *nextEl= new Element;
*nextEl=c.getNext();
}
Element & operator=(const Element& c)
{
delete this->nextEl;
nextEl=c.getNext();
line=c.getLine();
}
Element::~Element()
{
delete this->nextEl;
delete line;
}
list.h:
#include "element.h"
class Lista{
private
Element* head;
public:
Lista():head(NULL){};
Lista(const Lista& copy);
Lista& operator=(const Lista& copy);
~Lista();
void pushFront(const Element& e);
Element& getFront(){return head;};
void deleteFront();
void printList();
}
list.cpp:
void Lista::pushFront(const Element& e){
Element *newEl;
newEl -> setLine(e.getLine());
newEl -> setNext(head);
head=newEl;
}
void Lista::deleteFront(){
if(head != NULL){
Element* tmp=head;
head=tmp->getNext();
delete tmp;
}
}
Lista::~Lista(){
Element *tmp=head;
Element *toDelete=NULL;
while(tmp!=NULL){
toDelete=tmp;
tmp=tmp->getNext();
delete toDelete;
}
Lista::Lista( const Lista ©)
{ //copy nazwa kopiowanej listy
if (copy.head==NULL)return;//powoduje wyjście z funkcji*
Element *tempSource=NULL;
Element * tempDest=NULL;//dwa wspaźniki wedrujące po kopiowanej liście
head=new Element;
head->setValue(copy.head->getValue());
tempDest=head;
tempSource=copy.head->next();
while(tempDest!=NULL)
{
tempDest-> setNext(new Element);
tempDest=tempDest->next();
tempDest-> SetValue(tempSource->getValue(1);
tempSource=tempSource->next();
}
}
Lista &operator=(const Lista & copy)
{
if(this!=©){
Element *tmp=head;
Element *toDelete=NULL;
while(tmp!=NULL){
toDelete=tmp;
tmp=tmp->getNext();
delete toDelete;
if (copy.head==NULL)return;
Element *tempSource=NULL;
Element * tempDest=NULL;
head=new Element;
head->setValue(copy.heat->getValue());
tempDest=head;
tempSource=copy.head->next();
while(tempDest!=NULL)
{
tempDest-> setNext(new Element);
tempDest=tempDest->next();
tempDest-> SetValue(tempSource->getValue(1);
tempSource=tempSource->next();
}
}return *this;
void Lista::printList( )
{
Element *tmp=head;
Element *copy=NULL;
while(tmp!=NULL){copy=tmp;tmp=tmp->getNext();, cout<<copy->getLine()<<endl;}
};