最基本的顺序表(经典顺序表)
2011-09-17 13:44
239 查看
// 顺序表.cpp -- 最基本的顺序表(经典顺序表) // 完整的class. // List abstract class -- 线性表的C++抽象类声明 template<class Elem> class List() { public: // Reinitialize the list. the client is responsible for // reclaiming the storange used by the list elements. virtual void clear() = 0; // Insert an element at the front of the right partition. // Return true if successful, false if the list is full. virtual bool insert(const Elem&) = 0; // Append an element at the end of the right partition. // Return true if successful, false if the list is full. virtual bool append(const Elem&) = 0; // Remove the first element of right partition. Return // true if successful, false if the list is empty. // The element removed is returned in the parameter. virtual bool remove(Elem&) = 0; // Place fence at list start, making left partition empty. virtual void setStart() = 0; // Place fence at list end, making right partition empty. virtual void setEnd() = 0; // Move fence one step left; no change if already at start. virtual void prev() = 0; // Move fence one step right; no change if already at end virtual void next() = 0; // Return length of left partition virtual int leftLength() const = 0; // Return length of right partition virtual int rightLength() const = 0; // If pos or more elements are in the list, set the size // of left partition to pos and return true. Otherwise, // do nothing and return false. virtual bool setPos(int pos) = 0; // Return in first parameter the first element of the // right partition. Return true if successful, false // if the right partition is empty. virtual bool getValue(Elem&) const = 0; // print the contents of the list virtual void print() const = 0; }; // Array-based list implementation -- 线性表的实现 template <class Elem> class Alist: public List<Elem> //继承 { private: int maxSize; // Maximum size of list int listSize; // Actual number of elements in list int fence; // Position of fence Elem* listArray; // Array holding list elementss public: AList(int size = DefaultListSize) // constructor { maxSize = size; lastSize = fence = 0; listArray = new Elem[maxSize]; } ~AList() { delete [] listArray;} // Destructor void clear() { delete [] listArray; listSize = fence = 0; listArray = new Elem[maxSize]; } bool insert(const Elem&); bool append(const Elem&); bool remove(Elem&); void setStart() { fence = 0;} void setEnd() { fence = listSize;} void prev() { if (fence != 0) fence--;} void next() { if (fence <= listSize) fence++;} int leftLength()const {return fence;} int rightLength()const {return lastsize - fence;} bool setPos(int pos) { if ((pos >= 0) && (pos <= listSize)) fence = pos; return (pos >= 0) && (pos <= listSize); } bool getValue(Elem& it)const() { if (rightLength() == 0) return flase; else { it = listArray[fence]; reutrn true;} } void print()const { int temp = 0; cout << " < "; while (temp < fence) cout << listArray[temp++] << " "; cout << " | "; while (temp < ListSize) cout << listarray[temp++] << " "; cout << " >\n"; } }; template <class Elem> // Insert at front of right partition bool Alist <Elem>::insert(const Elem& item) { if (listsize == maxSize) return flase; // List is full for (int i = listsize; i > fence; i--) // Shift Elem up listArray[i] = listArray[i-1]; // to make room listArray[fence] = item; listSize++; // Increment list size return true; } // Remove and return first Elem in right partition template <class Elem> bool AList <Elem>::remove(Elem& it) { if (rightLength() == 0) return false; // nothing in right it = listArray[fence]; // copy removed elem for (int i = fence; i < listSize -1; i++) // Shift them down listArray[i] = listArray[i + 1]; listSize--; // Decrement Size return true; } // 看完以后就觉得,原来掌握顺序表是很简单的事,只需要掌握几个点; // 其中最重要的思想是确定位置。 // 知道现在的位置,开始的位置,结束的位置,数组的大小,该位置左右的大小。
相关文章推荐
- 最基本的顺序表(经典顺序表)
- 最基本的顺序表(经典顺序表)
- (2011.07.19) 顺序表.cpp -- 最基本的顺序表(经典顺序表)
- 编写一个程序实现顺序栈的各种基本运算(假设顺序表的元数基本类型为Char)
- 顺序表的应用举例(利用顺序表的基本运算,实现如果在顺序表A中出现的元素,在顺序表B中也出现,则将A中该元素删除)
- 顺序表的应用举例(顺序表A和顺序表B的元素都是非递减排列,利用顺序表的基本运算,将它们合并成一个顺序表C,要求C也是非递减序列。)
- 链表,顺序表,链队,链栈,顺序队,顺序栈,二叉树的基本操作函数
- 【顺序表项目1 - 顺序表的基本运算】
- 数据结构之顺序表增、删、查找、初始化以及顺序表输出基本相关操作
- SDUT 3329----顺序表应用5:有序顺序表归并(很经典)
- 线性表—顺序表-顺序表基本运算的实现
- 写一个顺序表模板,包括顺序表的基本功能,例如查找,插
- 编写一个程序实现顺序表的各种基本运算(假设顺序表的元数基本类型为Char)
- 编程实现顺序表的以下基本操作:建立顺序表,修改顺序表,插入顺序表,删除顺序表。
- 写一个顺序表模板,包括顺序表的基本功能,例如查找,插
- 数据结构笔记(一)线性表的顺序表示和基本操作及其顺序表实现的集合运算(A-B)U(B-A)实例
- 第3周项目1 -顺序表的基本运算
- 第三周项目1--顺序表的基本运算
- 第三周项目1 顺序表的基本运算
- 链表,顺序表的基本操作及其实现的栈与队列