数据结构与算法(C++)之一——顺序表(SeqList)

《数据结构与算法(C++)》,窦延平 张同珍 姜丽红 陈玉泉,上海交通大学出版社。上海交通大学算法与数据结构课程教材。

本系列文章为书中数据结构与算法的具体实现。

顺序表,通过下标访问元素,插入操作复杂度较高。Next和Prev因为没有实质性作用,所以没有实现。

 

写这个类也是遇到了不少问题。最大的一个问题是,刚开始我是把声明和定义分开在.h和.cpp文件中写的,但是写完之后测试的时候怎么都无法编译通 过,一直提示unreference。直到询问了V姐,才知道template的编译器实现在这里似乎有一点问题,一般用template都需要把声明和 定义放在同一个文件里。删掉了seqlist.cpp,把定义也放到了seqlist.h,果然可以了。

顺便提一下,CSDN会吃掉代码不含空格的空行,真囧……

 

FILE: seqlist.h

/* element 0 doesn't belong to seqlist */ #ifndef SEQLIST_H_ #define SEQLIST_H_ #include "exceptions.h" using namespace std; const ExceptionIndexOutOfRange eIndexOutOfRange; const ExceptionContainerIsFull eContainerIsFull; const ExceptionContainerIsEmpty eContainerIsEmpty; const ExceptionUnavailableSize eUnavailableSize; template class SeqList { public: SeqList(int initSize); ~SeqList(void); void Clear(void); //clear the seqlist bool IsEmpty(void) const; //return true when the seqlist is empty and false otherwise bool IsFull(void) const; //return true when the seqlist is full and false otherwise int Size(void) const; //return the length of the table T At(int p) const; //return the element at place i, throw exception when error //int Next(int i) const; //get next element, 0 when error //int Prev(int i) const; //get previous element, 0 when error //these two function is not useful, commented int Find(const T& x) const; void Insert(int p, const T& x); //insert x at place p, and move behind elements backword T Delete(int p); //delete the element at place p, and return the element T operator [](int p) const; private: T* m_data; int m_size; //current size of the seqlist int m_maxSize; //max allowed size of the seqlist }; template SeqList::SeqList(int initSize): m_maxSize(initSize), m_size(0) { if (initSize < 0) throw eUnavailableSize; //initSize == 0 is allowed! but not useful m_data = new T[m_maxSize + 1]; } template SeqList::~SeqList(void) { } template void SeqList::Clear(void) { m_size = 0; } template bool SeqList::IsEmpty(void) const { return m_size == 0; } template bool SeqList::IsFull(void) const { return m_size == m_maxSize; } template int SeqList::Size(void) const { return m_size; } template T SeqList::At(int p) const { if (p < 1 || p > m_size) throw eIndexOutOfRange; return m_data[p]; } template int SeqList::Find(const T& x) const { int result = 0; for (int i = 1; i <= m_size; ++i) if (m_data[i] == x) { result = i; break; } return result; } template void SeqList::Insert(int p, const T& x) //if p out of range, no exception throwed, p = 1 or m_size { if (IsFull()) throw eContainerIsFull; if (p > m_size + 1) p = m_size + 1; if (p < 1) p = 1; //no exception throwed for (int i = m_size; i >= p; --i) m_data[i + 1] = m_data[i]; m_data[p] = x; ++m_size; } template T SeqList::Delete(int p) { if (IsEmpty()) throw eContainerIsEmpty; if (p > m_size || p < 1) throw eIndexOutOfRange; T result = m_data[p]; for (int i = p; i <= m_size - 1; ++i) m_data[i] = m_data[i + 1]; --m_size; return result; } template T SeqList::operator [](int p) const { return At(p); } #endif

你可能感兴趣的:(数据结构与算法(C++)之一——顺序表(SeqList))