List With Iterator(STL)

实现标准模板库中的功能

#include<iostream>
#include<string>
#include<cassert>
using namespace std;

template<class T>
class list {
 public:
  struct listelem;  // forward declarations
  //  也可将struct和iterator的定义直接放在前面
  class iterator;
  // 构造函数
  list() {
    h.ptr = t.ptr = NULL;
  }
  //  注意构造函数中,首先都应该将head和tail置为NULL
  //  n个值为c的链表
  list(size_t n_elements, const T& c) {
    h.ptr = t.ptr = NULL;
    for (size_t i = 0; i < n_elements; ++i) {
      //  此处用push_front和push_back都可以
      push_back(c);
    }
  }
  list(const list& x) {
    h.ptr = t.ptr = NULL;  //  重要
    list::iterator iter = x.begin();
    while (iter != NULL) {
      //  下面两句等价于push_back(*iter++);
      push_back(*iter);
      iter++;
    }
  }
  list(iterator b, iterator e) {
    h.ptr = t.ptr = NULL;
    while (b != e) {
      //  等价于push_back(*b++);
      push_back(*b);
      b++;
    }
  }
  ~list() {
    this->clear();
  }
  iterator begin() const {
    return h;
  }
  iterator end() const {
    //  返回的是最后一个迭代器的下一个
    iterator temp;
    return (temp.ptr == NULL ? iterator(0) : ++temp);
  }
  size_t size() {
    size_t count = 0;
    iterator temp = h;
    while (temp != end()) {
      temp++;
      count++;
    }
    return count;
  }
  void push_front(const T& c) {
    listelem* temp = new listelem(c, h, NULL);
    if (empty()) {  //  链表为空时或是if(h == NULL)
    // 或是if(h.ptr == NULL)
      h.ptr = t.ptr = temp;
    } else {   //  链表不为空时
      h->prev = temp;
      h.ptr = temp;
    }
  }
  void push_back(const T& c) {
    listelem* temp = new listelem(c, NULL, t);
    if (empty()) {
      h.ptr = t.ptr = temp;
    } else {
      t->next = temp;
      t.ptr = temp;
    }
  }
  void pop_front() {
    if (empty()) return;  //   链表为空的情况
    listelem* temp = h.ptr;
    if (h.ptr == t.ptr) {
      //  只有一个节点的情况
      h.ptr = NULL;
      t.ptr = NULL;
      delete temp;
    } else {
      h.ptr = h->next;
      h->prev = NULL;
      delete temp;
    }
  }
  void pop_back() {
    if (empty()) return;
    listelem* temp = t.ptr;
    if (h.ptr == t.ptr) {
      h.ptr = t.ptr = NULL;
      delete temp;
    } else {
      t.ptr = t->prev;
      t->next = NULL;
      delete temp;
    }
  }
  iterator insert(iterator position, const T& val) {
    listelem* p = position.ptr;
    if (p == end()) {  //  或者是p == NULL
      push_back(val);
      return t;
    } else if (p == begin()) {  //  或者是 p->prev == NULL
      push_front(val);
      return begin();
    } else {
      listelem* temp = new listelem(val, p, p->prev);
      temp->next->prev = temp;
      temp->prev->next = temp;
      return iterator(temp);
    }
  }
  iterator erase (iterator position) {
    listelem* temp = position.ptr;
    if (temp == NULL||empty()) return iterator(0);
    if (temp == h) {
      pop_front();
      return h;
    }
    temp->prev->next = temp->next;
    temp->next->prev = temp->prev;
    listelem* re = temp->next;
    delete temp;
    return iterator(re);
  }
  bool empty() const {
    return h.ptr == 0;
  }
  T& front() {
    return *h;
  }
  T& back() {
    return *t;
  }
  void clear() {
    while (h.ptr != 0) {
      pop_front();
    }
    h.ptr = t.ptr = NULL;
  }
  friend ostream& operator << (ostream& out, const list& x) {
    list<T>::iterator p = x.begin();
    out << "[";
    while (p != 0) {
      out << *p << " ";
      ++p;
    }
    out << "]";
    return out;
  }

  struct listelem {      // list cell
    T data;
    listelem *next, *prev;
    listelem(const T& s, listelem* n, listelem* p) {
      data = s;
      next = n;
      prev = p;
    }
  };

  class iterator {
   public:
    friend class list; 
    explicit iterator(listelem* p = 0) {
      ptr = p;
    }

    iterator(const iterator& other) {
      ptr = NULL;
      ptr = other.ptr;
    }
    iterator& operator ++ () {
      //  ++p;
      ptr = ptr->next;
      return *this;
    }
    iterator& operator -- () {
      ptr = ptr->prev;
      return *this;
    }
    iterator operator ++ (int) {
      //  p++;
      iterator temp = *this;
      ptr = ptr->next;
      return temp;
    }
    iterator operator -- (int) {
      iterator temp = *this;
      ptr = ptr->prev;
      return temp;
    }
    listelem* operator -> () {
      return ptr;
    }
    T& operator * () {
      return ptr->data;
    }
    operator listelem * () {
      return ptr;
    }  // conversion;
    private:
    listelem* ptr;  // current listelem or 0;
  };

  private:
  iterator h, t;  // head and tail
};

下面是main函数部分

int main() {
  list<int> li;
  cout << "test size" << endl;
  cout << li.size() << endl;
  cout << (li.empty() ? "empty" : "not empty") << endl;
  int n;  // n >= 3
  cout << "test push and pop" << endl;
  li.push_back(1);
  cout << li << endl;
  li.pop_back();
  cout << li << endl;
  li.push_front(1);
  cout << li << endl;
  li.pop_front();
  cout << li << endl;
  li.push_back(1);
  cout << li << endl;
  li.pop_front();
  cout << li << endl;
  li.push_front(1);
  cout << li << endl;
  li.pop_back();

  cin >> n;
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    li.push_back(tmp);
    li.push_front(tmp);
  }
  cout << li.size() << endl;
  cout << li << endl;
  li.pop_back();
  li.pop_front();
  li.pop_front();
  cout << li << endl;
  cout << "front : " << li.front() << endl;
  cout << "back : " << li.back() << endl;
  cout << (li.empty() ? "empty" : "not empty") << endl;

  cout << "test begin and end" << endl;
  list<int>::iterator it = li.begin();
  while (it != li.end()) {
    cout << it->data << " ";
    it++;
  }
  cout << endl;

  cout << "test insert and erase and ++ --" << endl;
  it = li.begin();
  cout << *(li.insert(it, 100)) << endl;
  it = li.end();
  cout << *(li.insert(it, 200)) << endl;
  cout << li << endl;
  it = li.begin();
  cout << *(li.erase(++it)) << endl;
  it = li.begin();
  it++;
  cout << *(li.erase(it)) << endl;
  cout << li << endl;
  it = li.begin();
  it++;
  it--;
  li.insert(it, 101);
  it = li.begin();
  ++it;
  --it;
  li.insert(it, 99);
  cout << li << endl;
  it = li.begin();
  *it = 90;

  cout << "test constructor" << endl;
  list<int> li2(5, 9);
  list<int> li3(li);
  it = li.begin();
  list<int>::iterator it2 = it;
  ++(++(++it2));
  list<int> li4(li.begin(), li.end());
  list<int> li5(it, it2);
  cout << li2 << endl;
  cout << li3 << endl;
  cout << li4 << endl;
  cout << li5 << endl;

  return 0;
}

input :
4
1 2 3 4
output:
test size
0
empty
test push and pop
[1 ]
[]
[1 ]
[]
[1 ]
[]
[1 ]
4
1
2
3
4
8
[4 3 2 1 1 2 3 4 ]
[2 1 1 2 3 ]
front : 2
back : 3
not empty
test begin and end
2 1 1 2 3
test insert and erase and ++ –
100
200
[100 2 1 1 2 3 200 ]
1
1
[100 1 2 3 200 ]
[99 101 100 1 2 3 200 ]
test constructor
[9 9 9 9 9 ]
[90 101 100 1 2 3 200 ]
[90 101 100 1 2 3 200 ]
[90 101 100 ]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值