1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
#ifndef _LISTALIN_H
#define _LISTALIN_H
#include <iostream>
using std::cout;
using std::endl;
using std::ostream;
// classe astratta listaLinare
template< class T, class P >
class Linear_list{
public:
typedef T value_type; // the type of object, T, stored in the list
typedef P position;
// operators
virtual void create() = 0;
virtual bool empty() const = 0; // true if the list's size is 0
virtual value_type read(position) const = 0;
virtual void write(const value_type & x, position p) = 0; // write x at position p
virtual position begin() const = 0; // returns a position pointing to the beginning of the list
virtual bool end(position) const = 0; // true with a position pointing to the end of the list
virtual position next(position) const = 0; // returns the next position
virtual position previous(position) const = 0; // return the previous position
virtual void erase(position) = 0;
// funzioni di servizio
//friend ostream& operator<< <T,P>(ostream&, const Linear_list<T,P>&);
/* Altre funzioni di servizio implementabili */
/*
virtual int size() const; // returns the size of the list
virtual void push_front(const value_type &); // insert a new element at the beginning
virtual void push_back(const value_type &); // insert a new element at the end
virtual void pop_front(); // removes the first element
virtual void pop_back(); // removes the last element
virtual void clear(); // erases all the elements
*/
int lunghezza() {
return _length;
}
position last() {
position p = begin();
int i=0;
while(i<lunghezza()) {
p = next(p);
i++;
}
}
void inverti(){
position l = begin();
position r = last();
value_type temp;
int i=0;
while(i<lunghezza()/2) {
temp = read(l);
write(read(r),l);
write(temp,r);
l=next(l);
r=previous(r);
i++;
}
}
bool isPalindroma(){
position l = begin();
position r = last();
bool palindroma = true;
value_type temp;
int i=0;
while(i<lunghezza()/2 && palindroma) {
palindroma = read(l)==read(r);
l=next(l);
r=previous(r);
i++;
}
return palindroma;
}
protected:
int _length;
};
/* sovraccarica <<. Attenzione se il tipo restituito da read non è primitivo, allora
* anche per questo tipo bisogna sovraccaricare l'operatore <<
*/
template< class T, class P >
ostream& operator<<(ostream& os, const Linear_list<T,P> &l){
typename Linear_list<T,P>::position p;
p = l.begin();
os << "[";
while (!l.end(p)){
if (p != l.begin())
os << ", " << l.read(p);
else
os << l.read(p);
p = l.next(p);
}
os << "]" << endl;
return os;
}
/*
template< class T, class P >
int Linear_list<T,P>::size() const{
....
}
*/
#endif // _LISTALIN_H
|