当前位置:网站首页>The use of list and Its Simulation Implementation
The use of list and Its Simulation Implementation
2022-07-07 11:06:00 【Exy-】
One .list iterator Use
int ar[] = { 1,2,3,4,5,6,7,8,9,0 };
int n = sizeof(ar) / sizeof(ar[0]);
//list<int> mylist(10,5);// Two way circular list with leading node
list<int> mylist(ar, ar + n);
for (const auto& e : mylist)
{
cout << e << " ";
}
cout << endl;
list<int>::iterator it = mylist.begin();
while (it != mylist.end())
{
cout << *it << " ";
++it;
}
cout << endl;
list<int>::reverse_iterator rit = mylist.rbegin();
while (rit != mylist.rend())
{
cout << *rit << " ";
++rit;
}
cout << endl;3.list Iterator failure problem for
Two .list Built in functions for
list<int> mylist;
mylist.empty testing list Is it empty , Is to return true, Otherwise return to false
mylist.size return list Number of valid nodes in
mylist.front return list The reference to the value in the first node of
mylist.back return list Reference to the value of the last node of

3、 ... and .list Simulation Implementation
namespace ljx
{
template<class T>
struct __list_node
{
__list_node<T>* _next;
__list_node<T>* _prev;
T _data;
__list_node(const T& x=T())
:_data(x)
,_next(nullptr)
,_prev(nullptr)
{}
};
template<class T,class Ref,class Ptr>
struct __list_iterator
{
typedef __list_node<T>node;
typedef __list_iterator<T, Ref, Ptr>Self;
node* _node;
__list_iterator(node* node)
:_node(node)
{}
T* operator->()
{
return &_node->_data;
}
T& operator*()
{
return _node->_data;
}
Self& operator++()
{
_node = _node->_next;
return *this;
}
Self& operator++(int)
{
Self tmp(*this);
//_node = _node->_next;
++(*this);
return tmp;
}
Self& operator--()
{
_node = _node->_prev;
return *this;
}
Self& operator--(int)
{
__list_iterator<T>tmp(*this);
--(*this);
return tmp;
}
bool operator!=(const Self& it)
{
return _node != it._node;
}
bool operator==(const Self& it)
{
return _node == it._node;
}
};
template<class T>
class list
{
typedef __list_node<T> node;
public:
typedef __list_iterator<T, T&, T*>iterator;
typedef __list_iterator<T,const T&,const T*> const_iterator;
// Take the lead in two-way circular list
//typedef __list_iterator<T,const T&,const T * > const_iterator;
iterator begin()
{
return iterator(_head->_next);
}
const_iterator begin()const
{
return const_iterator(_head->_next);
}
iterator end()
{
return iterator(_head);
}
const_iterator end()const
{
return const_iterator(_head);
}
list()
{
_head = new node;
_head->_next = _head;
_head->_prev = _head;
}
list(const list<T>& lt)
{
_head = new node;
_head->_next = _head;
_head->_prev = _head;
//const_iterator it = lt.begin();
//while (it != lt.end())
//{
// push_back(*it);
// ++it;
//}
for (auto e : lt)
{
push_back(e);
}
}
//list <T>& operator=(const list<T>& lt)
//{
// if (this != <)
// {
// clear();
// for (auto e : lt)
// {
// push_back(e);
// }
// }
// return *this;
//}
list<T>& operator=(list<T> lt)
{
swap(_head, lt._head)
return *this;
}
~list()
{
clear();
delete _head;
_head = nullptr;
}
void clear()
{
node* p = _head->_next;
while (p != _head)
{
delete(p);
}
}
void push_back(const T& x)
{
//node* tail = _head->_prev;
//node* newnode = new node(x);
//tail->_next = newnode;
//newnode->_prev = tail;
//newnode->_next = _head;
//_head->_prev = newnode;
insert(end(), x);
}
void push_front(const T& x);
void insert(iterator pos, const T& x)
{
node* cur = pos._node;
node* prev = cur->_prev;
node* newnode = new node(x);
prev->_next = newnode;
newnode->_prev = prev;
newnode->_next = cur;
cur->_prev = newnode;
}
void erase(iterator pos)
{
assert(pos != end());
{
node* cur = pos._node;
node* prev = cur->_prev;
node* next = cur->_next;
delete cur;
prev->_next = next;
next->_prev = prev;
}
}
void pop_back()
{
erase(iterator(_head->_prev));
erase(--end());
}
private:
node* _head;
};
}边栏推荐
- 【C#】WinForm运行缩放(变糊)的解决方法
- 变量的解构赋值
- 【安装系统】U盘安装系统教程,使用UltraISO制作U盘启动盘
- How to play video on unityui
- When do you usually get grades in the soft exam? Online pedaling?
- 【推薦系統 01】Rechub
- Use load_ decathlon_ Datalist (Monai) fast loading JSON data
- The eighth training assignment
- The sixth training assignment
- [recommendation system 01] rechub
猜你喜欢

软考中级,软件设计师考试那些内容,考试大纲什么的?

打算参加安全方面工作,信息安全工程师怎么样,软考考试需要怎么准备?

PHP \ newline cannot be output

shardingsphere分库分表示例(逻辑表,真实表,绑定表,广播表,单表)

Still cannot find RPC dispatcher table failed to connect in virtual KD

VR development optimization

2021 summary and 2022 outlook

【安装系统】U盘安装系统教程,使用UltraISO制作U盘启动盘

Operation method of Orange Pie orangepi 4 lts development board connecting SATA hard disk through mini PCIe

【推薦系統 01】Rechub
随机推荐
uniapp 在onLaunch中跳转页面后,点击事件失效解决方法
1324: [example 6.6] integer interval
高级软考(网络规划设计师)该如何备考?
Idea shortcut keys
The gun startles the dragon, and the crowd "locks" Zhou Zhi
【推薦系統 01】Rechub
The difference between monotonicity constraint and anti monotonicity constraint
1323: [example 6.5] activity selection
The eighth training assignment
【OneNote】无法连接到网络,无法同步问题
Transaction rolled back because it has been marked as rollback-only解决
What are the test preparation materials and methods for soft exam information processing technicians?
Go-Redis 中间件
Mpx 插件
Operation method of Orange Pie orangepi 4 lts development board connecting SATA hard disk through mini PCIe
Seata 1.3.0 four modes to solve distributed transactions (at, TCC, Saga, XA)
[untitled]
Unity websocket client
Is the gold content of intermediate e-commerce division in the soft exam high?
【PyTorch 07】 动手学深度学习——chapter_preliminaries/ndarray 习题动手版