目录
2.3.1 数据类型表:将MyList_iterator声明为内部类型Iterator
2.3.6 基于insert()、erase()设计push_back()/push_front()/pop_back()/pop_front()
- public:
- //内嵌类型表
- typedef T value;
- typedef T* vIter;
- protected:
- vIter m_Data;//数组头指针
- int m_nLen;//数组长度
- vIter start;//数组的起始地址
- int finish;//数组的满位标志
- int end_of_element;//数组的末尾标志
- void push_back(const value& x) {
- if (end_of_element != finish) {
- *(start + finish) = x;
- ++finish;
- }
- else {
- cout << "越界" << endl;
- }
- }
- inline value back() {
- --finish;
- return *(start + finish);
- }
- value operator[](int n) {
- if (n <= finish) {
- return *(start + n);
- }
- else {
- cout << "取值错误<" << endl;
- }
- }
- #include
- using namespace std;
-
- //MyVector的类模板
- template<typename T>
- class MyVector {
- public:
- //内嵌类型表
- typedef T value;
- typedef T* vIter;
-
- public:
- MyVector(int nLen = 0) :m_nLen(nLen), m_Data(nullptr), finish(0) {
- if (nLen > 0) {
- m_Data = new T[nLen];
- start = m_Data;
- end_of_element = nLen;
- }
- }
- ~MyVector()
- {
-
- }
-
- void push_back(const value& x) {
- if (end_of_element != finish) {
- *(start + finish) = x;
- ++finish;
- }
- else {
- cout << "越界" << endl;
- }
- }
-
- inline value back() {
- --finish;
- return *(start + finish);
- }
-
- value operator[](int n) {
- if (n <= finish) {
- return *(start + n);
- }
- else {
- cout << "取值错误<" << endl;
- }
- }
-
- protected:
- vIter m_Data;//数组头指针
- int m_nLen;//数组长度
- vIter start;//数组的起始地址
- int finish;//数组的满位标志
- int end_of_element;//数组的末尾标志
- };
-
- int main() {
- int x;
- MyVector<int>v(10);
- v.push_back(100);
- v.push_back(200);
- v.push_back(300);
- x = v.back();
- cout << "x= " << x << endl;
- cout << v[0] << endl;
- cout << v[1] << endl;
- cout << v[2] << endl;
- //cout << v[3] << endl;
- return 0;
- }

- template<typename T>
- struct MyList_node {
- MyList_node
* prev; - MyList_node
* next; - T data;
- };
- //同时将设计类型表和迭代器整合在一起
-
- //迭代器
- template<typename T>
- struct MyList_iterator {
- //类型表
- typedef MyList_iterator
iterator; - typedef MyList_node
* link_type; - //结点指针
- link_type node;
- //构造函数
- MyList_iterator(link_type x) :node(x) {
-
- }
- MyList_iterator() :node(nullptr) {
-
- }
- //重载++,使得我们可以获得下个结点的地址
- iterator& operator++() {
- node = node->next;
- //返回本对象,同时指向下一个
- return *this;
- }
- iterator& operator++(int) {
- //保存自增运算前的指针值
- iterator temp = *this;
- //本对象加一
- ++* this;
- return temp;
- }
- //重载--
- iterator& operator--() {
- node = (node)->prev;
- return *this;
- }
-
- iterator& operator--(int) {
- iterator temp = *this;
- --* this;
- return temp;
- }
- iterator& operator=(iterator x) {
- node = x.node;
- return *this;
- }
- T& operator*()const {
- //返回结点数据
- return node->data;
- }
- //迭代器
- template<typename T>
- struct MyList_iterator {
- //类型表
- typedef MyList_iterator
iterator; - typedef MyList_node
* link_type; - //结点指针
- link_type node;
- //构造函数
- MyList_iterator(link_type x) :node(x) {
-
- }
- MyList_iterator() :node(nullptr) {
-
- }
-
- //重载++,使得我们可以获得下个结点的地址
- iterator& operator++() {
- node = node->next;
- //返回本对象,同时指向下一个
- return *this;
- }
- iterator& operator++(int) {
- //保存自增运算前的指针值
- iterator temp = *this;
- //本对象加一
- ++* this;
- return temp;
- }
- //重载--
- iterator& operator--() {
- node = (node)->prev;
- return *this;
- }
-
- iterator& operator--(int) {
- iterator temp = *this;
- --* this;
- return temp;
- }
-
- iterator& operator=(iterator x) {
- node = x.node;
- return *this;
- }
-
- T& operator*()const {
- //返回结点数据
- return node->data;
- }
- };
- //MyList
- template<typename T>
- class MyList {
- public:
- //数据类型表
- //将MyList_iterator
声明为内部类型Iterator - typedef MyList_iterator
iterator; -
- protected:
- MyList_node
* node; - size_t length;
- MyList() :length(0) {
- node = new MyList_node
; - node->next = node;
- node->prev = node;
- }
- ~MyList(){
-
- }
- //返回链表的头函数
- iterator begin() {
- return node->next;
- }
- //返回链表尾地址
- iterator end() {
- return node;
- }
- iterator insert(const iterator& position, const T& x) {
- MyList_node
* temp = new MyList_node; - temp->data = x;
- temp->prev = position.node->prev;
- temp->next = position.node;
- position.node->prev->next = temp;
- position.node->prev = temp;
- ++length;
- return temp;
- }
- void erase(const iterator& position) {
- position.node->prev->next = position.node->next;
- position.node->next->prev = position.node->prev;
- --length;
- }
- //push_front()
- void push_front(const T& x) {
- insert(begin(), x);
- }
-
- //push_back()
- void push_back(const T& x) {
- insert(end(), x);
- }
-
- //pop_front()
- void pop_front() {
- erase(begin());
- }
-
- //pop_back(){
- void pop_back() {
- erase(--end());
- }
- //MyList
- template<typename T>
- class MyList {
- public:
- //数据类型表
- //将MyList_iterator
声明为内部类型Iterator - typedef MyList_iterator
iterator; - public:
- MyList() :length(0) {
- node = new MyList_node
; - node->next = node;
- node->prev = node;
- }
- ~MyList(){
-
- }
- //返回链表的头函数
- iterator begin() {
- return node->next;
- }
- //返回链表尾地址
- iterator end() {
- return node;
- }
-
- //push_front()
- void push_front(const T& x) {
- insert(begin(), x);
- }
-
- //push_back()
- void push_back(const T& x) {
- insert(end(), x);
- }
-
- //pop_front()
- void pop_front() {
- erase(begin());
- }
-
- //pop_back(){
- void pop_back() {
- erase(--end());
- }
-
- iterator insert(const iterator& position, const T& x) {
- MyList_node
* temp = new MyList_node; - temp->data = x;
- temp->prev = position.node->prev;
- temp->next = position.node;
- position.node->prev->next = temp;
- position.node->prev = temp;
- ++length;
- return temp;
- }
-
- void erase(const iterator& position) {
- position.node->prev->next = position.node->next;
- position.node->next->prev = position.node->prev;
- --length;
- }
-
- protected:
- MyList_node
* node; - size_t length;
- };
- int main() {
- MyList<int>myList1;
- myList1.push_front(10);
- myList1.push_front(20);
- myList1.push_front(30);
-
- MyList<int>::iterator iter;
- iter = myList1.begin();
- cout << *iter << endl;
- cout << *++iter << endl;
- cout << *++iter << endl;
- cout << endl;
-
- myList1.push_back(100);
- myList1.push_back(200);
- myList1.push_back(300);
- iter = myList1.end();
- cout << *--iter << endl;
- cout << *iter-- << endl;
- cout << *iter << endl;
- cout << endl;
-
- myList1.pop_front();
- myList1.pop_back();
- cout << *myList1.begin() << endl;
- cout << *--myList1.end() << endl;
-
- return 0;
- }
