nil | cons(h,t) | |
empty(l) | true | false |
head(l) | error | h |
tail(l) | error | t |
ADT
OOP
typedef int element;\fbox{ADT}
struct list; extern list* nil(); extern list* cons(element e, list* l); extern element head(list* l); extern list* tail(list* l); extern bool equal(list* l, list* m);
template< class E >\fbox{OOP}
class list { public: list() { } virtual ~list() { } virtual bool empty() = 0; virtual E head() = 0; virtual list<E>* tail() = 0; virtual bool operator==(list<E>* m) = 0; };
ADT
typedef int element; enum { NIL, CONS }; struct list { int tag; element e; list* next; };
list* nil() {\c{\fbox{}}
list* l = new list; l->tag = NIL; return l; } list* cons( element e, list* l) {\c{\fbox{} }
list* x = new list; x->tag = CONS; x->e = e; x->next = l; return x; }
ADT
int empty(list* lst) { return !lst || lst->tag == NIL; } element head(list* l) {head
require( ! empty(l) ); return l->e; } list* tail(list* l) {tail
require( ! empty(l) ); return l->next; } bool equal(list* l, list* m) {\c{// equal}
switch( l->tag) { case NIL: return empty(m); case CONS: return !empty(m) && head(l) == head(m) && tail(l) == tail(m); } }
list* r = cons(1,cons(2,nil())); while (!empty(r)) { cout << head(r) << endl; r = tail(r); }
OOP
template< class E > class nil : public list<E> {\c{\fbox{}}
public: nil() {} bool empty() { return 1; } E head() { require( false ); return E(); } list<E>* tail() { require( 0 ); return 0; } bool operator==(list<E>* m) { return m->empty(); } }; template< class E > class cons : public list<E> {\c{\fbox{}}
public: cons(E e, list<E>* l) : _e(e), next(l) {} ~cons() { delete next; } bool empty() { return 0; } E head() { return _e; } list<E>* tail() { return next; } bool operator==(list<E>* m); protected: E _e; list<E>* next; };
list* r = new cons (1, new cons (2, new nil )); while (! r->empty()) { cout << r->head() << endl; r = r->tail(); } delete r;
ADT
typedef int element; enum { NIL, CONS, INTERVAL }; struct list { int tag; element e; union { element z; list* next; }; };
list* interval( element x, element y ) { list* l = new list; if ( x <= y ) { l->tag = INTERVAL; l->e = x; l->z = y; } else l->tag = NIL; return l; }
ADT
element head(list* l) {head
require( ! empty(l) ); return l->e;\c{// for both CONS and INTERVAL}
} list* tail(list* l) {tail
require( ! empty(l) ); switch( l->tag ) { case CONS: return l->next; case INTERVAL: return interval((l->e)+1,l->z); } }
OOP
class interval : public list<int> {\c{\fbox{interval}}
public: interval(int x, int y) : _x(x), _y(y) { require( x <= y ); } bool empty() { return 0; } int head() { return _x; } list<int>* tail() { return (_x+1 <=_y)? new interval(_x+1,_y): new nil<int>; } bool operator==(list<int>* m) { return !m->empty() && _x == m->head() && tail() == m->tail(); } protected: int _x; int _y; };
ADT
int length( list* l ) {length
switch( l->tag ) { case NIL: return 0; case CONS: return 1 + length(l->next); case INTERVAL: return l->z - l->e + 1; }; }
OOP
list* r = new cons (1, new cons (2, new interval(3,7) )); while (! r->empty()) { cout << ((listWL *)r)->length() << endl; r = r->tail(); } delete r;