C++STL容器篇之list简单模拟
其实就是双向链表,然后使用C++封装而且,底层代码就是双向链表,直接看代码就好。
.hpp文件
#pragma
#include <iostream>
using namespace std;
template <class Ty>
struct Node
{
Node<Ty>* left;
Ty data;
Node<Ty>* right;
Node(Ty data) :data(data), left(nullptr), right(nullptr){}
Ty& getData() { return this->data; }
};
template <class T>
class MyList
{
public:
MyList()
{
firstNode = nullptr;
lastNode = nullptr;
listSize = 0;
}
ostream& operator<<(ostream& out)
{
out << this->firstNode->data;
return out;
}
bool empty() { return listSize == 0; }
int size() { return listSize; }
Node<T>& front() { return *firstNode; }
Node<T>& back() { return *lastNode; }
Node<T>* first() { return firstNode; }
Node<T>* last() { return lastNode; }
void printList()
{
Node<T>* pMove = firstNode;
while (pMove!=lastNode->right)
{
cout << pMove->getData() << '\t';
pMove = pMove->right;
}
cout << endl;
}
void push_front(T data)
{
Node<T>* newNode = new Node<T>(data);
if (listSize == 0)
lastNode = newNode;
else
{
newNode->right = firstNode;
firstNode->left = newNode;
}
firstNode = newNode;
listSize++;
}
void push_back(T data)
{
Node<T>* newNode = new Node<T>(data);
if (listSize == 0)
firstNode = newNode;
else
{
newNode->left = lastNode;
lastNode->right = newNode;
}
lastNode = newNode;
listSize++;
}
void pop_front()
{
if (listSize == 0)
return;
else if (listSize == 1)
{
delete firstNode;
firstNode = nullptr;
lastNode = nullptr;
}
else
{
Node<T>* pDelete = firstNode;
firstNode = firstNode->right;
pDelete->right = nullptr;
firstNode->left = nullptr;
delete pDelete;
pDelete = nullptr;
}
listSize--;
}
void pop_back()
{
if (listSize == 0)
return;
else if (listSize == 1)
{
delete lastNode;
lastNode = nullptr;
firstNode = nullptr;
}
else
{
Node<T>* pDelete = lastNode;
lastNode = lastNode->left;
pDelete->left = nullptr;
lastNode->right = nullptr;
delete pDelete;
pDelete = nullptr;
}
listSize--;
}
class iterator
{
public:
iterator() = default;
iterator(Node<T>* pMove):pMove(pMove){}
T& operator*()
{
return this->pMove->data;
}
iterator operator++(int)
{
return this->pMove = this->pMove->right;
}
iterator operator++()
{
return this->pMove = this->pMove->right;
}
iterator operator--(int)
{
return this->pMove = this->pMove->left;
}
iterator operator--()
{
return this->pMove = this->pMove->left;
}
bool operator!=(iterator obj)
{
return this->pMove != obj.pMove;
}
protected:
Node<T>* pMove;
};
iterator begin() { return iterator(firstNode); }
iterator end() { return iterator(lastNode->right); }
protected:
Node<T>* firstNode;
Node<T>* lastNode;
int listSize;
};
主函数测试
#include <iostream>
#include "MyList.hpp"
using namespace std;
int main()
{
MyList<int> mylist;
for (int i = 0; i < 3; i++)
{
mylist.push_front(i);
}
mylist.printList();
cout << mylist.size() << endl;
mylist.pop_back();
mylist.printList();
cout << mylist.size() << endl;
for (int i = 6; i < 10; i++)
{
mylist.push_back(i);
}
for (auto v : mylist)
{
cout << v << '\t';
}
cout << endl;
cout << "=====================" << endl;
MyList<int>::iterator iter;
for (iter = mylist.begin(); iter != mylist.end(); iter++)
{
cout << *iter << '\t';
}
cout << endl;
cout << "当前链表的大小为:" << mylist.size() << endl;
while (!mylist.empty())
{
cout << mylist.front().data << '\t';
mylist.pop_front();
}
cout << endl;
cout << "当前链表的大小为:" << mylist.size() << endl;
return 0;
}
|