顺序表与链表(双向带头循环链表)的优缺点对比:
顺序表的优点
1.按下标去进行随机访问,可能存在一定的空间浪费。
2.cpu高速缓存,(cpu的预加载+顺序表物理空间连续)-》命中率比较高。(cpu访问数据时,在cpu缓存内,命中。不在cpu缓存内,不命中。)
顺序表缺点:
1.空间不够,需要增容。(一定程序的的性能消耗)
2.头部或者中间插入删除数据时,需要挪动数据,效率比较低-》O(N)
链表优点:
1.按需申请内存,需要存一个数据,就申请一块内存,不存在程序的性能消耗,也不存在空间浪费
2.任意位置以O(1)时间复杂度进行对数据的增删查改。
链表缺点:
1.不支持下标的随机访问
2.低命中,缓存污染。
顺序表与链表这两个数据结构之间是相辅相成的,互相弥补对方的缺点。后续会讲dequeue(双端队列)
//Node* cur 和 iterator的比较
//当他们都指向同一个节点,那么在物理内存中他们都存的是这个节点地址,物理上是一样的
//但是他们的类型不一样,他们的意义就不一样,比如*cur是一个指针的解引用,取到的值是节点
//比如*it是去调用迭代器的operator*,返回值是节点中存的值
//类型决定了对空间的解释权
#pragma once
namespace yqx
{
template<class T>
struct __list_node
{
__list_node<T>* _next;
__list_node<T>* _prev;
T _data;
__list_node(const T& val = T())
:_data(val)
, _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)
{}
//拷贝构造和赋值重载
//析构函数
//默认生成的就可以,不需要我们实现
Ref operator*()
{
return _node->_data;
}
Ptr operator->()
{
return &_node->_data;
}
//it++ -> it.operator++()
Self& operator++()//前加加
{
_node = _node->_next;
return *this;
}
//it++ -> it.operator++(0)
Self operator++(int)//后置加加
{
Self tmp(*this);
//_node = _node->_next;
++(*this);
return tmp;
}
Self& operator--()//前加加
{
_node = _node->_prev;
return *this;
}
Self& operator--(int)//后置减减
{
Self tmp(*this);
//_node = _node->_prev;
--(*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
{
//思考:const_iterator是如何实现的
public:
typedef __list_node<T> Node;
typedef __list_iterator<T, T&, T*> iterator;
typedef __list_iterator<T, const T&, const T*> const_iterator;
iterator begin()
{
return iterator(_head->_next);
}
iterator end()
{
return iterator(_head);
}
const_iterator begin()const
{
return const_iterator(_head->_next);
}
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+清除头结点
{
clear();
delete _head;
_head = nullptr;
}
void clear()//不清除头节点只清除数据
{
iterator it = begin();
while (it != end())
{
erase(it++);
}
}
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)
{
insert(begin(), x);
}
void pop_back()
{
erase(--end());
//erase(iterator(_head->_prev));
}
void pop_front()
{
erase(begin());
}
iterator 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;
return iterator(newnode);//匿名对象
/*iterator ret(newnode);
return ret;*/
/*return newnode;*/ //单参的隐式类型转换
}
iterator erase(iterator pos)
{
assert(pos != end());//不能删头结点!!!
Node* cur = pos._node;
Node* next = cur->_next;
Node* prev = cur->_prev;
delete cur;
prev->_next = next;
next->_prev = prev;
return iterator(next);
}
size_t size()
{
size_t n = 0;
iterator it = begin();
while (it != end())
{
++it;
++n;
}
return n;
}
bool empty()
{
return begin() == end();//当链表为空时,只有一个头结点,此时begin()==end();
}
private:
Node* _head;
};
//const迭代器
void print_list(const list<int>& lt)
{
list<int>::const_iterator it = lt.begin();
while (it != lt.end())
{
cout << *it << " ";
++it;
}
cout << endl;
}
void test_list1()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
lt.pop_back();
lt.pop_front();
print_list(lt);
/*while (it != lt.end())
{
cout << *it << " ";
++it;
}
cout << endl;*/
}
struct Date
{
int _year = 0;
int _month = 1;
int _day = 1;
};
void test_list2()
{
list<Date> lt;
lt.push_back(Date());
lt.push_back(Date());
list<Date>::iterator it = lt.begin();
while (it != lt.end())
{
cout << it->_year << "-" << it->_month << "-" << it->_day << " ";
cout << (*it)._year << "-" << (*it)._month << "-" << (*it)._day << " ";
//这里原本应该是it->->_year,但为了可读性
++it;
}
cout << endl;
}
void test_list3()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
list<int> ll;
ll = lt;
print_list(ll);
}
}