Nevíte-li si rady s jakýmkoliv matematickým problémem, toto místo je pro vás jako dělané.
Nástěnka
❗22. 8. 2021 (L) Přecházíme zpět na doménu forum.matweb.cz!
❗04.11.2016 (Jel.) Čtete, prosím, před vložení dotazu, děkuji!
❗23.10.2013 (Jel.) Zkuste před zadáním dotazu použít některý z online-nástrojů, konzultovat použití můžete v sekci CAS.
Nejste přihlášen(a). Přihlásit
Ahoj, mám za úkol do třídy List (ze spojového seznamu) doplnit následující operátory:
operator= (vytvoří hlubokou kopii)
operator== (testuje, zda seznamy obsahují stejné hodnoty)
operator!=
operator[] (bude vracet referenci na data v prvku se zadaným indexem
operator+= (spojování řetězců)
operator<< (vložení seznamu do proudu)
Pomůže mi prosím někdo??
\\Header
#ifndef LIST_H
#define LIST_H
#include "item.h"
class ListIterator;
class List
{
Item *first;
Item *last;
unsigned int counter;
void init();
public:
List();
List(const List &other);
~List();
void copyFrom(const List &source);
unsigned int count() const;
bool empty() const;
void pushBack(Data &d);
void pushFront(Data &d);
void popBack();
void popFront();
Data back() const;
Data front() const;
void print() const;
void clear();
Item* find(Data &what);
bool remove(Data &what);
friend class ListIterator;
ListIterator* getIterator();
};
#endif // LIST_H
//Source file
#include "list.h"
#include "iterator.h"
List::List()
: first(new Item(0)), last(new Item(0)), counter(0)
{
init();
}
/* melka kopie!
List::List(const List &other)
: first(other.first), last(other.last), counter(other.counter)
{}
*/
List::List(const List &other)
: first(new Item(0)), last(new Item(0)), counter(0)
{
init();
copyFrom(other);
}
void List::copyFrom(const List &source){
Item *curr = source.first->next;
while(curr!=source.last){
this->pushBack(curr->data);
curr=curr->next;
}
}
List::~List(){
clear();
delete first;
delete last;
}
void List::init(){
first->next = last;
last->prev = first;
}
unsigned int List::count() const{
return counter;
}
bool List::empty() const{
return (counter == 0);
}
void List::pushBack(Data &d){
//1. data uloz do zarazky
last->setData(d);
//2. vytvor novy prvek a zapoj jej za zarazku
last->next = new Item(0);
//3. pred nove vytvoreny prvek zapojte zarazku
last->next->prev = last;
//4. zarazku posun na nove vytvoreny prvek
last=last->next;
counter++;
}
/*
last <-> first
next <-> prev
*/
void List::pushFront(Data &d){
first->setData(d);
first->prev = new Item(0);
first->prev->next = first;
first=first->prev;
counter++;
}
void List::print() const{
for(Item *curr = first->next; curr!=last; curr = curr->next){
curr->print();
}
}
void List::popBack(){
last = last->prev;
delete last->next;
last->next = nullptr;
counter--;
}
void List::popFront(){
first = first->next;
delete first->prev;
first->prev = nullptr;
counter--;
}
Data List::back() const{
return last->prev->getData();
}
Data List::front() const{
return first->next->getData();
}
void List::clear(){
while(!empty())
popFront();
}
Item* List::find(Data &what){
last->setData(what);
Item *p = first->next;
for(;p->getData()!=what;p=p->next);
return (p == last ? nullptr : p);
}
bool List::remove(Data &what){
Item *p = find(what);
if(!p){
return false;
}
p->next->prev = p->prev;
p->prev->next = p->next;
delete p;
counter--;
return true;
}
ListIterator* List::getIterator(){
return new ListIterator(this);
}
Offline