数据结构C++实现第一发,主要有几部分构成,分别是一个抽象类LinearList.h、数组链表的头文件ArrayList.h以及main.cpp源文件。
LinearList.h文件具体信息如下:
#ifndef INC_01_ARRAYLIST_LINEARLIST_H
#define INC_01_ARRAYLIST_LINEARLIST_H
#include
template
class LinearList {
public:
virtual ~LinearList() {};
virtual bool empty() const = 0;
virtual int size() const = 0;
virtual T& get(int index) const = 0;
virtual int indexof(const T& theElement) const = 0;//first pos
virtual void erase(int index) = 0;
virtual void insert(int index, const T& theElement) = 0;
virtual void output(std::ostream& out) const = 0;
};
#ifndef INC_01_ARRAYLIST_ARRAYLIST_H
#define INC_01_ARRAYLIST_ARRAYLIST_H
#include "LinearList.h"
#include
#include
using namespace std;
template
void resizeCapacity(T*& a,int oldCapacity, int newCapacity);
template
class ArrayList :public LinearList
{ public: //iterator class iterator{ public: // typedef bidirectional_iterator_tag iterator_category; typedef T value_type; typedef ptrdiff_t difference_type; typedef T* pointer; typedef T& reference; //construct iterator(T* thepos=0) { pos = thepos;} //operator T& operator*() const { return *pos;} T* operator->() const { return &*pos;} iterator& operator++() { ++pos; return *this;} iterator& operator++(int) { iterator old = *this; ++pos; return old;} iterator& operator--() { --pos; return *this;} iterator& operator--(int) { iterator old = *this; --pos; return old;} bool operator!=(const iterator rhl) const { return pos != rhl.pos;} bool operator==(const iterator rhl) const { return pos == rhl.pos;} protected: T* pos; }; iterator begin(){ return iterator(element);} iterator end(){ return iterator(element+listsize);} public: //construct copy destroy //ArrayList(int initCapacity = 10); ArrayList(int initCapacity = 10, int size = 0); ArrayList