#ifndef DEF_SIMPLELIST_CLASS_H #define DEF_SIMPLELIST_CLASS_H #include /* This class implements a singly linked list. It is also used to represent one of its elements. */ template class SimpleList { protected: T m_value; SimpleList* m_next; public: SimpleList(const T& value, SimpleList* next = 0) : m_value(value), m_next(next) {} ~SimpleList() { if (m_next != 0) delete m_next; } T& v() { return m_value; } T& operator* () { return m_value; } SimpleList* cons(const T& value) { return new SimpleList(value, this); } SimpleList* addAtEnd(const T& value) { if (this == 0) { return new SimpleList(value); } else { if (m_next == 0) { m_next = new SimpleList(value); return m_next; } else { return m_next->addAtEnd(value); } } } SimpleList* next() { return m_next; } SimpleList* last() { if (m_next == 0) return this; return m_next->last(); } SimpleList* delThis() { SimpleList* ret = m_next; Mem::free(this); return ret; } void delNext() { if (m_next == 0) return; SimpleList* temp = m_next; m_next = m_next->m_next; Mem::free(temp); } SimpleList* removeOnce(const T& value) { if (this == 0) return 0; if (value == m_value) return delThis(); for (SimpleList *iter = this; iter->next() != 0; iter = iter->next()) { if (iter->next()->v() == value) { iter->delNext(); break; } } return this; } bool isEnd() { return m_next == 0; } u32int size() { if (m_next == 0) return 0; return m_next->size() + 1; } }; #endif