当前位置:网站首页>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;
};
}
边栏推荐
- Arduino board description
- Unity determines whether the mouse clicks on the UI
- I plan to take part in security work. How about information security engineers and how to prepare for the soft exam?
- [pytorch 07] hands on deep learning chapter_ Preliminaries/ndarray exercises hands-on version
- Ping tool ICMP message learning
- 【推荐系统 02】DeepFM、YoutubeDNN、DSSM、MMOE
- PR Lecture Notes
- What does intermediate software evaluator test
- Arduino receives and sends strings
- 2022.7.3DAY595
猜你喜欢
When do you usually get grades in the soft exam? Online pedaling?
Vscode 尝试在目标目录创建文件时发生一个错误:拒绝访问【已解决】
Différences entre les contraintes monotones et anti - monotones
【C#】WinForm运行缩放(变糊)的解决方法
Transaction rolled back because it has been marked as rollback only
在线硬核工具
Basic introduction of yarn and job submission process
2021-05-21
How to prepare for the advanced soft test (network planning designer)?
[recommendation system 02] deepfm, youtubednn, DSSM, MMOE
随机推荐
[pyqt] the cellwidget in tablewidget uses signal and slot mechanism
uniapp 在onLaunch中跳转页面后,点击事件失效解决方法
从色情直播到直播电商
软考一般什么时候出成绩呢?在线蹬?
Find the greatest common divisor and the least common multiple (C language)
Laya common script commands
[machine learning 03] Lagrange multiplier method
What is an intermediate network engineer? What is the main test and what is the use?
[untitled]
Operation method of Orange Pie orangepi 4 lts development board connecting SATA hard disk through mini PCIe
单调性约束与反单调性约束的区别 monotonicity and anti-monotonicity constraint
【亲测可行】error while loading shared libraries的解决方案
Deconstruction and assignment of variables
SQL Server knowledge gathering 9: modifying data
Deep understanding of Apache Hudi asynchronous indexing mechanism
"Dream Cup" 2017 Jiangsu information and future primary school summer camp it expert PK program design questions
Openinstall and Hupu have reached a cooperation to mine the data value of sports culture industry
【PyTorch 07】 动手学深度学习——chapter_preliminaries/ndarray 习题动手版
China Southern Airlines pa3.1
[untitled]