目录
2.3.1 数据类型表:将MyList_iterator声明为内部类型Iterator
2.3.6 基于insert()、erase()设计push_back()/push_front()/pop_back()/pop_front()
1.Vector容器仿真
1.1 定义内嵌类型表
public:
//内嵌类型表
typedef T value;
typedef T* vIter;
1.2 定义相关属性
protected:
vIter m_Data;//数组头指针
int m_nLen;//数组长度
vIter start;//数组的起始地址
int finish;//数组的满位标志
int end_of_element;//数组的末尾标志
1.3 push_back()
void push_back(const value& x) {
if (end_of_element != finish) {
*(start + finish) = x;
++finish;
}
else {
cout << "越界" << endl;
}
}
1.4 back()
inline value back() {
--finish;
return *(start + finish);
}
1.5 下标操作[]
value operator[](int n) {
if (n <= finish) {
return *(start + n);
}
else {
cout << "取值错误<" << endl;
}
}
1.6 总体代码与测试案例
#include <iostream>
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;
}
2.List仿真
2.1 定义结点
template<typename T>
struct MyList_node {
MyList_node<T>* prev;
MyList_node<T>* next;
T data;
};
2.2 设计迭代器(将类型表与迭代器整合)
//同时将设计类型表和迭代器整合在一起
//迭代器
template<typename T>
struct MyList_iterator {
//类型表
typedef MyList_iterator<T> iterator;
typedef MyList_node<T>* link_type;
//结点指针
link_type node;
//构造函数
MyList_iterator(link_type x) :node(x) {
}
MyList_iterator() :node(nullptr) {
}
2.2.1 重载++
//重载++,使得我们可以获得下个结点的地址
iterator& operator++() {
node = node->next;
//返回本对象,同时指向下一个
return *this;
}
iterator& operator++(int) {
//保存自增运算前的指针值
iterator temp = *this;
//本对象加一
++* this;
return temp;
}
2.2.2 重载--
//重载--
iterator& operator--() {
node = (node)->prev;
return *this;
}
iterator& operator--(int) {
iterator temp = *this;
--* this;
return temp;
}
2.2.3 重载=
iterator& operator=(iterator x) {
node = x.node;
return *this;
}
2.2.4 重载*(返回结点数据)
T& operator*()const {
//返回结点数据
return node->data;
}
2.2.5 迭代器总体代码:
//迭代器
template<typename T>
struct MyList_iterator {
//类型表
typedef MyList_iterator<T> iterator;
typedef MyList_node<T>* 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;
}
};
2.3 设计MyList
2.3.1 数据类型表:将MyList_iterator声明为内部类型Iterator
//MyList
template<typename T>
class MyList {
public:
//数据类型表
//将MyList_iterator<T>声明为内部类型Iterator
typedef MyList_iterator<T> iterator;
protected:
MyList_node<T>* node;
size_t length;
2.3.2 构造函数初始化
MyList() :length(0) {
node = new MyList_node<T>;
node->next = node;
node->prev = node;
}
~MyList(){
}
2.3.3 设计begin()/end()
//返回链表的头函数
iterator begin() {
return node->next;
}
//返回链表尾地址
iterator end() {
return node;
}
2.3.4 设计插入元素 insert()
iterator insert(const iterator& position, const T& x) {
MyList_node<T>* temp = new MyList_node<T>;
temp->data = x;
temp->prev = position.node->prev;
temp->next = position.node;
position.node->prev->next = temp;
position.node->prev = temp;
++length;
return temp;
}
2.3.5 设计删除 erase()
void erase(const iterator& position) {
position.node->prev->next = position.node->next;
position.node->next->prev = position.node->prev;
--length;
}
2.3.6 基于insert()、erase()设计push_back()/push_front()/pop_back()/pop_front()
//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());
}
2.3.7 MyList整体代码
//MyList
template<typename T>
class MyList {
public:
//数据类型表
//将MyList_iterator<T>声明为内部类型Iterator
typedef MyList_iterator<T> iterator;
public:
MyList() :length(0) {
node = new MyList_node<T>;
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<T>* temp = new MyList_node<T>;
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<T>* node;
size_t length;
};
2.4 测试案例
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;
}