Chinaunix首页 | 论坛 | 博客
  • 博客访问: 98507
  • 博文数量: 21
  • 博客积分: 145
  • 博客等级: 入伍新兵
  • 技术积分: 250
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-22 17:37
文章分类

全部博文(21)

文章存档

2013年(16)

2012年(5)

我的朋友

分类: C/C++

2012-12-26 14:18:11

chapter6
这一章主要讲的是链表,基于list实现。
deque、vector较list的主要优势是随机访问是常数级别的,但是频繁的随机位置插入,复杂度是线性的,同时,插入、删除会带来大面积的迭代器失效。
list的主要优势是随机插入是常数的,但是要求传入插入位置,除了erase会使当前位置迭代器失效外,基本不失效。

课后有两个pj,一个是行编辑器,另一个是实现一个简单的list。
我选择了后一个,前一个感觉没什么意思。
释放空间和申请空间,我用的是delete和new,偷懒了。

list类定义:

点击(此处)折叠或打开

  1. template <class T>
  2. class list {
  3. protected:
  4.     struct Node {
  5.         T data;
  6.         Node* prev;
  7.         Node* next;

  8.         Node(const T &x): data(x), prev(NULL), next(NULL) {}
  9.         Node(): prev(NULL), next(NULL) {}
  10.     };

  11.     Node* head_ptr; // point to the list head
  12.     size_t length;

  13. public:
  14.     class iterator {
  15.     protected:
  16.         // important: otherwise couldn't call iterator(Node*)
  17.         friend class list;
  18.         Node* cur_ptr;
  19.         iterator(Node* ptr): cur_ptr(ptr) {}

  20.     public:
  21.         iterator() {}
  22.         T& operator*() const { return cur_ptr->data; }
  23.         inline bool operator==(const iterator &itr) const;
  24.         inline bool operator!=(const iterator &itr) const;
  25.         inline iterator& operator++(); // ++cur_ptr
  26.         inline iterator operator++(int); // cur_ptr++
  27.         inline iterator& operator--(); // --cur_ptr
  28.         inline iterator operator--(int); // cur_ptr--
  29.     };

  30.     list();
  31.     list(const list<T> &another);
  32.     ~list();
  33.     void push_front(const T &x);
  34.     void push_back(const T &x);
  35.     iterator insert(iterator pos, const T &x); // return the insert position
  36.     void pop_front();
  37.     void pop_back();
  38.     void erase(iterator pos);
  39.     void erase(iterator first, iterator last); // do not include *(last)
  40.     inline size_t size() const;
  41.     inline bool empty() const;
  42.     iterator begin() const;
  43.     iterator end() const;
  44. };
因为外部不需要知道list是以struct Node存储的,所以iterator(Node*)是一个私有构造器,但是要声明list是iterator的友元类,不然list就无法访问这个构造器(函数传参和返回时,用于类型隐式自动转换)。

遇到的问题:
list的两个构造器,有相近的代码,为了方便,我就这么写了,结果悲剧:

点击(此处)折叠或打开

  1. template <class T>
  2. list<T>::list(): length(0) {
  3.     head_ptr = new Node();
  4.     head_ptr->next = head_ptr;
  5.     head_ptr->prev = head_ptr;
  6. }

  7. template <class T>
  8. list<T>::list(const list<T> &another): length(0) {
  9.     list();
  10.     for (list<T>::iterator itr = another.begin();
  11.             itr != another.end(); itr++)
  12.         push_back(*itr);
  13. }
后来仔细一想,这个错误真是太可笑了。list()只不过构造了一个temp类,想当然了(貌似,java这样是可以的)。不过我也不太确信这样解释对不对,先搁在这里吧。
后来就改成,过了:

点击(此处)折叠或打开

  1. template <class T>
  2. list<T>::list(): length(0) {
  3.     head_ptr = new Node();
  4.     head_ptr->next = head_ptr;
  5.     head_ptr->prev = head_ptr;
  6. }

  7. template <class T>
  8. list<T>::list(const list<T> &another): length(0) {
  9.     head_ptr = new Node();
  10.     head_ptr->next = head_ptr;
  11.     head_ptr->prev = head_ptr;
  12.     for (list<T>::iterator itr = another.begin();
  13.             itr != another.end(); itr++)
  14.         push_back(*itr);
  15. }

源代码:
阅读(892) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~