Linear List template class T class linear List

  • Slides: 34
Download presentation

基 于 公 式 化 的 线 性 表 类 Linear. List template <class

基 于 公 式 化 的 线 性 表 类 Linear. List template <class T> class linear. List { // 程序 5. 1 public: virtual linear. List() {}; //构造函数 virtual bool empty() const = 0; virtual int size() const = 0; //返回元素个数 virtual T& get(int the. Index) const = 0; //返回第 the. Index元素 virtual index. Of(const T& the. Index) const = 0; // virtual void erase(int the. Index) = 0; // 删除第the. Index个元素 virtual void insert(int the. Index, const T& the. Element) 0; // 在the. Index位置上插入the. Element virtual void output(ostream& out) const = 0; }; 山东大学计算机科学与技术学院 数据结构 第 3章 数据描述 9

变长一维数组程序 template<class T> void change. Length 1 D (T*& a, int old. Length, int

变长一维数组程序 template<class T> void change. Length 1 D (T*& a, int old. Length, int new. Length) { if (new. Length < 0) throw illegal. Parameter. Value(“new length must be >=0”); T * temp = new T(new. Length); int number = min(old. Length, new. Length); copy (a, a+number, temp); delete [] a; a = temp; } 山东大学计算机科学与技术学院 数据结构 第 5章 数据描述 11

template <class T> class array. List : public linear. List<T> //5 -3 array. List

template <class T> class array. List : public linear. List<T> //5 -3 array. List 类定义,从虚拟类派生array. List { public: arraylist. List(int initial. Capcity = 10) ; //构造函数 arraylist. List(const array. List<T>&); arraylist. List(delete [] element; ) bool empty() const {return list. Size == 0}; int size() const {return list. Size; }; //返回元素个数 T& get(int the. Index) const ; //返回第the. Index个 元素 int index. Of(const T& the. Index) const ; void erase(int the. Index) ; 山东大学计算机科学与技术学院 数据结构 第 3章 数据描述 12

void insert(int the. Index, const T& the. Element) ; void output(ostream& out) ; int

void insert(int the. Index, const T& the. Element) ; void output(ostream& out) ; int capacity () const {return array. Length; } protected: void check. Index(int the. Index) const; //当下�超 范�,按异常�理 T* element; //指向第一个元素的指� int array. Length; int list. Size //��元素个数 }; 山东大学计算机科学与技术学院 数据结构 第 3章 数据描述 13

构造函数 基于给定的长度,创建空表 template<class T> array. List<T>: : array. List(int initial. Capacity) { if (initial.

构造函数 基于给定的长度,创建空表 template<class T> array. List<T>: : array. List(int initial. Capacity) { if (initial. Capacity <1) {ostringstream s; s << “initial capacity = “ << initial. Capcity << “ Must be > 0 “; throw illegal. Parameter. Value(s. str()); } array. Length = initial. Capcity; element = new T[array. Length]; list. Size = 0; } n 时间复杂性 : Q(1) 山东大学计算机科学与技术学院 数据结构 第 3章 数据描述 14

构造函数 通过copy产生新表 template<class T> array. List<T>: : array. List(const array. List<T> & the. List)

构造函数 通过copy产生新表 template<class T> array. List<T>: : array. List(const array. List<T> & the. List) { //基于存在的链表the. List, 复制一个新链表 array. Length = the. List. array. Length; element = new T[array. Length]; copy(the. List. element, the. List. element+list. Size, element); //起点-终点,目的地 } n 时间复杂性 : Q(array. Length) 山东大学计算机科学与技术学院 数据结构 第 3章 数据描述 15

Template<class, iterator> void copy(iterator start, iterator end, iterator to) { while (start != end)

Template<class, iterator> void copy(iterator start, iterator end, iterator to) { while (start != end) {*to = *start; start ++; to ++) } 山东大学计算机科学与技术学院 数据结构 第 3章 数据描述 16

check. Index template<class T> void array. List<T>: : check. Index(int the. Index) const {

check. Index template<class T> void array. List<T>: : check. Index(int the. Index) const { if (the. Index < 0 || the. Index >= list. Size) {ostringstram s; s << “index =“<< the. Index << “ size =“<< list. Size; } throw illegal. Index(s. str()); } 山东大学计算机科学与技术学院 数据结构 第 3章 数据描述 17

操作 ‘Search’ template<class T> int array. List<T>: : index. Of(const T& the. Element) const

操作 ‘Search’ template<class T> int array. List<T>: : index. Of(const T& the. Element) const { // 返回元素the. Element第一次出� 的下� // 如果� 元素不在表中,� 返回 -1 int the. Index= (int)(find(element, element+list. Size, the. Element) – element; //find 是STL� 的� 准函 数, 若找到,� 算� 果是� 元素的位置,否� 是 list. Size。 if (the. Index == list. Size) return -1; elese return the. Index; } //�� 复� 性 : O(list. Size) 山东大学计算机科学与技术学院 数据结构 第 3章 数据描述 19

操作 ‘erase’ n template<class T> void array. List<T>: : erase(int the. Index) { //删除其索引为the.

操作 ‘erase’ n template<class T> void array. List<T>: : erase(int the. Index) { //删除其索引为the. Index的元素 //如果不存,则引发异常illegal. Index check. Index(the. Index); copy(element+the. Index+1, element+list. Size, element+th e. Index); //把the. Index+1~list. Size元素左移一位。 element[--list. Size]. T(); //调用析构函数把该元素回收 } 时间复杂性 : O((list. Size-the. Index)s) 山东大学计算机科学与技术学院 数据结构 第 3章 数据描述 20

Insert an element at the. Index template<class T> void array. List<T>: : insert(int the.

Insert an element at the. Index template<class T> void array. List<T>: : insert(int the. Index, const T& the. Element) { if (the. Index<0 || the. Index>list. Size) { ostringstram s; s<<“index=“ << the. Index << “size =“ << list. Size; throw illegal. Index(s. str()); } if (list. Size == array. Length) //空间上不允许插入了 {change. Length 1 D(element, array. Length, 2*array. Length); array. Length *= 2; } //数组长度 2倍变长 copy_backward(element+the. Index, element+list. Size+1); //从最右元素开始 element[the. Index]= the. Element; list. Size ++; } 山东大学计算机科学与技术学院 数据结构 第 3章 数据描述 21

操作 ‘Output’ template<class T> void array. List<T>: : output(cout -> out) const { //把表输送至输出流

操作 ‘Output’ template<class T> void array. List<T>: : output(cout -> out) const { //把表输送至输出流 copy (element, element+list. Size, ostream_iterator<T>(cout, “ “)); } //重� << template <class T> ostream& operator<<(ostream& out, array. List<T>& x) {x. output(out); return out; } 山东大学计算机科学与技术学院 数据结构 第 3章 数据描述 24

Template<class, iterator> void copy(iterator start, iterator end, iterator to) { while (start != end)

Template<class, iterator> void copy(iterator start, iterator end, iterator to) { while (start != end) {*to = *start; start ++; to ++) } C++ STL 定义了5中迭代器,I/O, 向前(后),双向,随机。 使用前要为iterator 赋初值 class iterator; iterator begin() {return iterator(element); ) iterator end() {return iterator(element+list. Size(; ) 若是双向 reverse(y. begin(), y. end()); //逆向访问 int sum = accumulate(y. begin(), y. end(), 0); 山东大学计算机科学与技术学院 数据结构 第 3章 数据描述 25

使用方法 linear. List *x = (linear. List) new array. List<int>(100); array. List<double> y(100) 山东大学计算机科学与技术学院

使用方法 linear. List *x = (linear. List) new array. List<int>(100); array. List<double> y(100) 山东大学计算机科学与技术学院 数据结构 第 3章 数据描述 26

class iterator { public: typedef bidirectional_iterator_tag iterator_category typedef T value_type; typedef ptrdiff_t difference_type; typedef

class iterator { public: typedef bidirectional_iterator_tag iterator_category typedef T value_type; typedef ptrdiff_t difference_type; typedef T* pointer; typedef T& reference; //typedef的用途: 为已有的数据类型重新命名。 iterator(T* the. Position = 0) {position =the. Position; } T& operator*() const {return *position; } T* operatior->() const {return &*position; } 山东大学计算机科学与技术学院 数据结构 第 3章 数据描述 28

iterator& operator++() {++position; return *this; } iterator& operator++(int) {iterator old = *this; ++position; return

iterator& operator++() {++position; return *this; } iterator& operator++(int) {iterator old = *this; ++position; return old; } //因� 二者形式相同,加int()目的是为了区分,使用时只 要写成后++就调用这个函数 iterator& operator--() {--position; return *this; } iterator& operator--(int) //同上 {iterator old = *this; --position; return old; } 山东大学计算机科学与技术学院 数据结构 第 3章 数据描述 29

bool operator != (const iterator right) const {return position != right. position; } bool

bool operator != (const iterator right) const {return position != right. position; } bool operator == (const iterator right) const {return position = right. position; } protected: T* position; } 山东大学计算机科学与技术学院 数据结构 第 3章 数据描述 30

n#include <iostream> n#include <vector> nusing namespace std; nint main() n{ nvector<int> v; //v是存放int类型变量的可变长数组,开始时没有元素 nfor

n#include <iostream> n#include <vector> nusing namespace std; nint main() n{ nvector<int> v; //v是存放int类型变量的可变长数组,开始时没有元素 nfor (int n = 0; n<5; ++n) nv. push_back(n); //push_back成员函数在vector容器尾部添加一个元素 nvector<int>: : iterator i; //定义正向迭代器 nfor (i = v. begin(); i != v. end(); ++i) { //用迭代器遍历容器 ncout << *i << " "; //*i 就是迭代器i指向的元素 n*i *= 2; //每个元素变为原来的2倍 n} ncout << endl; n//用反向迭代器遍历容器 nfor (vector<int>: : reverse_iterator j = v. rbegin(); j != v. rend(); ++j) ncout << *j << " "; nreturn 0; vector 0 1 2 3 4 n} output 8 6 4 2 0 山东大学计算机科学与技术学院 数据结构 第 3章 数据描述 32