Code:
#ifndef LIST_HPP_INCLUDED
#define LIST_HPP_INCLUDED
template<class T>
class List;
template <class T>
ostream &operator <<(ostream &,const List<T> &);
template <class T>
class List{
private:
struct listEl{
T sadrzaj;
struct listEl *next;
};
listEl *head;
listEl *tail;
int noEl;
public:
List(){
head=tail=NULL;
noEl=0;
}
List(const List<T> &);
virtual ~List();
bool add(int,const T &);
bool remove(int);
bool read(int,T &)const;
int size() const{return noEl;}
bool empty()const{return head==NULL?1:0;}
void clear();
friend ostream &operator <<<>(ostream &,const List<T> &);
};
template<class T>
ostream &operator <<(ostream &out,const List<T> &l){
out<<"\n------------------------------------\n";
T res;
for(int i=1;i<=l.size();i++){
l.read(i,res);
if(i!=1)out<<", ";
out<<res;
}
out<<"\n------------------------------------\n";
return out;
}
template<class T>
bool List<T>::add(int n,const T &el){
if(n<1||(!empty()&&n>noEl+1))return false;
else{
listEl *newEl=new listEl;
newEl->sadrzaj=el;
if(n==1){
newEl->next=head;
head=newEl;
}
else if(n==noEl+1){
tail->next=newEl;
newEl->next=NULL;
}
else{
listEl *temp=head;
for(int i=2;i<n;i++)temp=temp->next;
newEl->next=temp->next;
temp->next=newEl;
}
noEl++;
if(newEl->next==NULL)tail=newEl;
return true;
}
}
template <class T>
bool List<T>::remove(int n){
if(n<1||empty()||n>noEl)return false;
else{
if(n==1){
listEl *temp=head;
head=temp->next;
delete temp;
noEl--;
}
else{
listEl *temp=head;
for(int i=2;i<n;i++)temp=temp->next;
listEl *del;
del=temp->next;
temp->next=del->next;
if(tail==del)tail=temp;
delete del;
noEl--;
}
return true;
}
}
template <class T>
bool List<T>::read(int n,T &res)const{
if(n<1||empty()||n>noEl)return false;
else{
if(n==1)res=head->sadrzaj;
else if(n==noEl)res=tail->sadrzaj;
else{
listEl *temp=head;
for(int i=1;i<n;i++)temp=temp->next;
res=temp->sadrzaj;
}
return true;
}
}
template<class T>
void List<T>::clear(){
while(!empty()){
remove(1);
}
}
template<class T>
List<T>::~List(){
while(!empty()){
remove(1);
}
}
template<class T>
List<T>::List(const List<T> &l){
head=tail=NULL;
noEl=0;
T res;
for(int i=1;i<=l.noEl;i++){
l.read(i,res);
add(i,res);
}
}
#endif // LIST_HPP_INCLUDED
#ifndef LIST_HPP_INCLUDED
#define LIST_HPP_INCLUDED
template<class T>
class List;
template <class T>
ostream &operator <<(ostream &,const List<T> &);
template <class T>
class List{
private:
struct listEl{
T sadrzaj;
struct listEl *next;
};
listEl *head;
listEl *tail;
int noEl;
public:
List(){
head=tail=NULL;
noEl=0;
}
List(const List<T> &);
virtual ~List();
bool add(int,const T &);
bool remove(int);
bool read(int,T &)const;
int size() const{return noEl;}
bool empty()const{return head==NULL?1:0;}
void clear();
friend ostream &operator <<<>(ostream &,const List<T> &);
};
template<class T>
ostream &operator <<(ostream &out,const List<T> &l){
out<<"\n------------------------------------\n";
T res;
for(int i=1;i<=l.size();i++){
l.read(i,res);
if(i!=1)out<<", ";
out<<res;
}
out<<"\n------------------------------------\n";
return out;
}
template<class T>
bool List<T>::add(int n,const T &el){
if(n<1||(!empty()&&n>noEl+1))return false;
else{
listEl *newEl=new listEl;
newEl->sadrzaj=el;
if(n==1){
newEl->next=head;
head=newEl;
}
else if(n==noEl+1){
tail->next=newEl;
newEl->next=NULL;
}
else{
listEl *temp=head;
for(int i=2;i<n;i++)temp=temp->next;
newEl->next=temp->next;
temp->next=newEl;
}
noEl++;
if(newEl->next==NULL)tail=newEl;
return true;
}
}
template <class T>
bool List<T>::remove(int n){
if(n<1||empty()||n>noEl)return false;
else{
if(n==1){
listEl *temp=head;
head=temp->next;
delete temp;
noEl--;
}
else{
listEl *temp=head;
for(int i=2;i<n;i++)temp=temp->next;
listEl *del;
del=temp->next;
temp->next=del->next;
if(tail==del)tail=temp;
delete del;
noEl--;
}
return true;
}
}
template <class T>
bool List<T>::read(int n,T &res)const{
if(n<1||empty()||n>noEl)return false;
else{
if(n==1)res=head->sadrzaj;
else if(n==noEl)res=tail->sadrzaj;
else{
listEl *temp=head;
for(int i=1;i<n;i++)temp=temp->next;
res=temp->sadrzaj;
}
return true;
}
}
template<class T>
void List<T>::clear(){
while(!empty()){
remove(1);
}
}
template<class T>
List<T>::~List(){
while(!empty()){
remove(1);
}
}
template<class T>
List<T>::List(const List<T> &l){
head=tail=NULL;
noEl=0;
T res;
for(int i=1;i<=l.noEl;i++){
l.read(i,res);
add(i,res);
}
}
#endif // LIST_HPP_INCLUDED
Iz nje izvedenu klasu Red:
Code:
#ifndef RED_HPP_INCLUDED
#define RED_HPP_INCLUDED
#include "list.hpp"
template<class T>
class Red;
template <class T>
ostream &operator <<(ostream &,const Red<T> &);
template<class T>
class Red : protected List<T>{
public:
Red():List<T>(){}
~Red(){}
void addR(const T &el){add(size()+1,el);}
int size() const{return List<T>::size();}
void removeR() {List<T>::remove(1);}
void readR(T &res)const {read(1,res);}
friend ostream &operator <<<>(ostream &,const Red<T> &);
};
template<class T>
ostream &operator <<(ostream &out,const Red<T> &r){
out<<"\n===================================\n";
T res;
for(int i=1;i<=r.size();i++){
r.read(i,res);
if(i!=1)out<<", ";
out<<res;
}
out<<"\n===================================\n";
return out;
}
#endif // RED_HPP_INCLUDED
#ifndef RED_HPP_INCLUDED
#define RED_HPP_INCLUDED
#include "list.hpp"
template<class T>
class Red;
template <class T>
ostream &operator <<(ostream &,const Red<T> &);
template<class T>
class Red : protected List<T>{
public:
Red():List<T>(){}
~Red(){}
void addR(const T &el){add(size()+1,el);}
int size() const{return List<T>::size();}
void removeR() {List<T>::remove(1);}
void readR(T &res)const {read(1,res);}
friend ostream &operator <<<>(ostream &,const Red<T> &);
};
template<class T>
ostream &operator <<(ostream &out,const Red<T> &r){
out<<"\n===================================\n";
T res;
for(int i=1;i<=r.size();i++){
r.read(i,res);
if(i!=1)out<<", ";
out<<res;
}
out<<"\n===================================\n";
return out;
}
#endif // RED_HPP_INCLUDED
Problem je sledeci: Zasto prilikom poziva metoda size() i remove() moram da navodim klasu porekla List<T>::(size sam i redefinisao da ne bih morao stalno da navodim mada se ovde javlja samo jednom..), a prilikom poziva metoda add(int,const T &) i read(int,T &) to ne moram da radim? Zar nije potrebno navodjenje klase porekla samo ako je metoda nasledjene klase istog imena pa onda pokriva metodu osnovne klase?
drugi problem je kako da postavim funkciju za preklapanje operatora << da bude virtual(ovako ne moze zato sto je friend)? javlja se problem kada se napravi pokazivac na List,pa onda se stavi da pokazuje na Red(sto je sasvim uredu jer je red naslednik od liste)i onda se pozove << ispisace funkciju od Liste a ne od reda...