继承的类:
List类
成员变量:
class LinkList : public List<T>
{
protected:
struct Node : public Object
{
T value;
Node* next;
};
mutable struct : public Object
{
char reserved[sizeof(T)];
Node* next;
} m_header;
int m_length;
Node* m_current;
int m_step;
}
Node类是链表中的一个结点类型,有指针域和数据域。因为涉及到申请内存所以继承Object顶层父类。
还有匿名类型,目的是防止泛指类型的类型为自定义类类型时报错,如果一个自定义类类型的无参构造函数异常,而且只是在程序里定义了变量,并没有使用,那么Node类型里的value就会有异常使程序停止,为了让他可以通过编译,使用的时候再报错,所以有了这个匿名类,类的内存布局和Node完全一样,所以此处也必须继承Object。这个匿名类的第一条语句申请了泛指类型大小的内存,而没有调用构造函数,在函数中使用的时候再强制转换为Node类型就可以了。所以定义变量不会报错,使用的话就会爆掉程序。m_current是为了考虑到遍历一个链表的效率,使用的游标模式的一个必要组成。
m_step是我们在游标模式中的步长值
必须实现的虚函数:
virtual bool insert(int i, const T& e) = 0;
virtual bool remove(int i) = 0
virtual bool set(int i, const T& e) = 0;
virtual bool get(int i, T& e) const = 0;
virtual int find(const T& e) const = 0;
virtual int length() const = 0;
virtual void clear() = 0;
特有的函数:
Node* position(int i) const
Node* creat()
void destroy(Node* p)
LinkList()
bool insert(const T& e)
virtual T get(int i) const
virtual T current()
virtual bool next()
virtual bool move(int i, int step = 1)
virtual bool end()
~LinkList()