slist概述
slist
和list
共同具有的一个相同特色是,它们的插人(insert) 、移除(erase)、接合(splice) 等操作并不会造成原有的迭代器失效(当然啦,指向被移除元素的那个迭代器,在移除操作发生之后肯定是会失效的)。
根据STL的习惯,插入操作会将新元素插入于指定位置之前。作为一个单向链表,slist
没有任何方便的方法可以找到该节点前一个节点的位置,因此必须从头开始找。换句话说,除了slist
起点处附近的区域之外,在其他位置插入删除的效率很低。这就是slist
和list
之下的大缺点。为此,slist
特别提供了insert_after
和erase_after
供灵活使用。
基于效率考虑,slist
不提供push_back()
,只提供push_front()
。因此slist
的元素次序会和元素插入进来的次序相反。
构造基类语法
在面向对象编程中,可以通过在子类的构造函数中显式调用基类的构造函数来传递基类的构造参数。这个过程称为构造基类。
构造基类的语法为:
class Derived : public Base {
public:
Derived(args) : Base(args), member(args) {
// Derived constructor body
}
private:
MemberType member;
};
在这个例子中,Derived
是一个继承自 Base
的子类。Derived
的构造函数中的参数 args
用于初始化 Derived
对象的成员变量以及调用基类 Base
的构造函数。
Base(args)
表示调用基类 Base
的构造函数,使用参数 args
进行初始化。这个语法可以确保在构造 Derived
对象时,先构造基类 Base
,然后再构造子类 Derived
。这是因为在派生类的对象构造过程中,基类的构造函数必须先被调用,以确保基类的成员变量被正确初始化。
构造基类的过程是自动完成的,如果没有显式调用基类的构造函数,则会默认调用基类的无参构造函数。但是,如果基类没有无参构造函数,或者需要使用不同的构造参数进行初始化,则必须在子类构造函数中显式调用基类的构造函数。
需要注意的是,在一个子类构造函数中,只能调用其直接基类的构造函数,不能调用其他基类的构造函数。如果一个子类有多个基类,则需要为每个基类分别调用构造函数。
常量成员函数
在成员函数的参数列表后使用const
修饰。
- 该函数不会修改任何成员变量的值。
- 常量成员函数可以被常量对象和非常量对象调用。
- 对于常量对象,只能调用常量成员函数,因为常量对象的成员变量不能被修改。
c++类的访问权限和继承
访问权限:public 可以被任意实体访问,protected 只允许子类(无论什么继承方式)及本类的成员函数访问,private 只允许本类的成员函数访问。
继承方式:public 继承,protect 继承,private 继承。
- 派生类内不管是 public、protected、private 继承,总是可以 public、protected 成员,基类中的 private 成员永远不能再派生类内直接访问,不论通过哪种方式。
- 派生类对象仅当 public 派生时,对基类中的 public 成员有可访问/可修改的权限,其他都为不可访问/不可修改。
另外,继承方式会改变从基类继承的成员在派生类的访问权限:
- public 继承不改变基类成员的访问权限;
- protected 继承将基类中 public 成员变为子类的 protected 成员,其它成员的访问权限不变;
- private 继承使得基类所有成员在子类中的访问权限变为 private。
//
//
///测试c++的继承方式
#include "string"
#include "iostream"
using namespace std;
class Base {
public:
string m_Base_pub = "m_Base_Pub";
protected:
string m_Base_pro = "m_Base_pro";
private:
string m_Base_pri = "m_Base_pri";
};
class Derivation_pub : public Base {
public:
void show() {
cout << this->m_Base_pub << endl;
cout << this->m_Base_pro << endl;
//cout<<this->m_Base_pri<<endl; //子类中不能访问基类的私有成员
}
};
class Derivation_pro : protected Base {
public:
void show() {
cout << this->m_Base_pub << endl;
cout << this->m_Base_pro << endl;
//cout<<this->m_Base_pri<<endl; //子类中不能访问基类的私有成员
}
};
class Derivation_pri : private Base {
public:
void show() {
cout << this->m_Base_pub << endl;
cout << this->m_Base_pro << endl;
//cout<<this->m_Base_pri<<endl; //子类中不能访问基类的私有成员
}
};
int main() {
Base b;
cout << "-------------基类测试--------------" << endl;
cout << b.m_Base_pub << endl; //类外可以访问类的公有数据成员
//cout << b.m_Base_pro << endl; //类外不能访问类的保护数据成员
//cout<< b.m_Base_pri<<endl; //类外不能访问类的私有数据成员
//公有继承方式的测试
cout << "--------------public继承------------" << endl;
Derivation_pub Dpub;
Dpub.show(); //类内访问基类的公有和保护数据成员测试,可以
cout << Dpub.m_Base_pub << endl; //类外只能访问基类的公有数据成员
//保护继承方式的测试
cout << "-------------protected继承-------------" << endl;
Derivation_pro Dpro;
Dpro.show();
//cout<<Dpro.m_Base_pub<<endl; //出错,protected继承方式,基类的public和protected数据成员在子类中的权限都是protected,类外不能访问
cout << "-------------private继承-------------" << endl;
Derivation_pri Dpri;
Dpri.show();
//cout<<Dpro.m_Base_pub<<endl; //出错,private继承方式,基类的public和protected数据成员在子类中的权限都是private,类外不能访问
return 0;
}
splice_after和splice
因为单链表的节点内只有一个指针,指向下一个节点,splice_after
传入的参数是待插入链表首尾的前一个节点。splice
传入的参数是对应要插入的节点。明确起见,splice_after
的接合都是接合参数的下一个节点。splice
的接合是在pos
之前接合参数对应的节点范围。
//把两个单链表接合起来
void splice_after(iterator pos, iterator before_first, iterator before_list) {
if (before_first != before_list) {
__slist_splice_after(pos.node, before_first.node, before_list.node);
}
}
//全局函数
//参数是要插入链表首节点的前一个节点和要插入链表最后一个节点的前一个节点
inline void __slist_splice_after(
__slist_node_base *pos,
__slist_node_base *before_first,
__slist_node_base *before_last) {
if (pos != before_first && pos != before_last) {
__slist_node_base *first = before_first->next; //标记要移除的一段链表的首尾
__slist_node_base *after = pos->next;
before_first->next = before_last->next; //移除链表
pos->next = first; //插入到新位置
before_last->next = after;
}
}
//__slist_previous(list_node_base* head,list_node_base* node)是在head对应的链表中找到node的前一个节点
void splice(iterator pos, slist &x, iterator first, iterator last) {
if (first != last) {
__slist_splice_after(__slist_previous(&head, pos.node),
__slist_previous(&x.head, first.node),
__slist_previous(&x.head, last.node));
}
}
slist
的全部源码
//slist的节点:基类定义指针,子类定义数据域
//基类不是模版类,子类是模版类
struct __slist_node_base {
__slist_node_base *next;
};
template<class T>
struct __slist_node : public __slist_node_base {
T data;
};
///全局函数:已知某一个节点,插入新节点于其后
inline __slist_node_base *__slist_make_link(
__slist_node_base *prev_node,
__slist_node_base *new_node) {
new_node->next = prev_node->next;
prev_node->next = new_node;
return new_node;
}
///全局函数:单向链表的大小(元素个数)
inline size_t __slist_size(__slist_node_base *node) {
size_t result = 0;
for (; node != 0; node = node->next) {
++result;
}
return result;
}
///全局函数,找到node的前一个节点
inline __slist_node_base *__slist_previous(__slist_node_base *head, const __slist_node_base *node) {
while (head && head->next != node) {
head = head->next;
}
return head;
}
inline const __slist_node_base *
__slist_previous(const __slist_node_base *head, const __slist_node_base *node) {
while (head && head->next != node) {
head = head->next;
}
return head;
}
///全局函数,接合两个链表
//参数是要插入链表首节点的前一个节点和要插入链表最后一个节点的前一个节点
inline void __slist_splice_after(
__slist_node_base *pos,
__slist_node_base *before_first,
__slist_node_base *before_last) {
if (pos != before_first && pos != before_last) {
__slist_node_base *first = before_first->next; //标记要移除的一段链表的首尾
__slist_node_base *after = pos->next;
before_first->next = before_last->next; //移除链表
pos->next = first; //插入到新位置
before_last->next = after;
}
}
//传入本链表的插入位置和待插入链表的头节点
inline void __slist_splice_after(__slist_node_base *pos, __slist_node_base *head) {
__slist_node_base *__before_last = __slist_previous(head, 0);
if (__before_last != head) {
__slist_node_base *after = pos->next;
pos->next = head->next;
head->next = 0;
__before_last->next = after;
}
};
///全局函数,头插法反转链表
inline __slist_node_base *__slist_reverse(__slist_node_base *node) {
__slist_node_base *__result = node;
node = node->next;
__result->next = 0;
while (node) {
__slist_node_base *next = node->next;
node->next = __result;
__result = node;
node = next;
}
return __result;
}
//slist的迭代器:基类使用节点的基类定义指针,指向节点结构,完成指针的移动和比较操作
// 子类使用节点子类指针构造,完成节点数据域的取值操作和++操作
struct __slist_iterator_base {
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef forward_iterator_tag iterator_category;
__slist_node_base *node; //指向节点基本结构
__slist_iterator_base(__slist_node_base *x) : node(x) {}
void incr() { node = node->next; } //前进一个节点
//两个slist迭代器是否等同,视其__slist_node_base* node是否等同而定
bool operator==(const __slist_iterator_base &x) const {
return node == x.node;
}
bool operator!=(const __slist_iterator_base &x) const {
return node != x.node;
}
};
//单向链表的迭代器结构
template<class T, class Ref, class Ptr>
struct __slist_iterator : public __slist_iterator_base {
typedef __slist_iterator<T, T &, T *> iterator;
typedef __slist_iterator<T, const T &, const T *> const_iterator;
typedef __slist_iterator<T, Ref, Ptr> self;
typedef T value_type;
typedef Ptr pointer;
typedef Ref reference;
typedef __slist_node<T> list_node;
//构造函数
__slist_iterator(list_node *x) : __slist_iterator_base(x) {}
__slist_iterator() : __slist_iterator_base(0) {}
__slist_iterator(const iterator &x) : __slist_iterator_base(x.node) {}
reference operator*() const { return ((list_node *) node)->data; }
pointer operator->() const { return &(operator*()); }
self &operator++() {
incr(); //前进一个节点
return *this;
}
self operator++(int) {
self tmp = *this;
incr();
return tmp;
}
};
template<class T, class Alloc = alloc>
class slist {
public:
typedef T value_type;
typedef value_type *pointer;
typedef const value_type *const_pointer;
typedef value_type &reference;
typedef const value_type &const_reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef __slist_iterator<T, T &, T *> iterator;
typedef __slist_iterator<T, const T &, const T *> const_iterator;
private:
typedef __slist_node<T> list_node;
typedef __slist_node_base list_node_base;
typedef __slist_iterator_base iterator_base;
typedef simple_alloc<list_node, Alloc> list_node_allocator;
private:
static list_node *create_node(const value_type &x) {
list_node *node = list_node_allocator::allocate();
try {
construct(&node->data, x);
node->next = 0;
}
catch (...) {
list_node_allocator::deallocate(node);
}
return node;
}
static list_node *create_node() {
list_node *node = list_node_allocator::allocate();
try {
construct(&node->data);
node->next = 0;
}
catch (...) {
list_node_allocator::deallocate(node);
}
return node;
}
static void destroy_node(list_node *node) {
destroy(&node->data);
list_node_allocator::deallocate(node, 1);
}
private:
list_node_base head; //头节点。是节点基类,只有指针,没有数据域。相当于哨兵,方便访问单链表的第一个节点
public:
///构造和析构函数
//默认构造
slist() { head.next = 0; }
//有参构造
slist(size_type n, const value_type &x) {
head.next = 0; //没有用继承,就先初始化头节点
insert_after_fill(&head, n, x);
}
slist(size_type n) {
head.next = 0;
insert_after_fill(&head, n, value_type());
}
slist(const_iterator first, const_iterator last) {
head.next = 0;
insert_after_range(&head, first, last);
}
slist(const value_type *first, const value_type *last) {
head.next = 0;
insert_after_range(&head, first, last);
}
//拷贝构造
slist(const slist &x) {
head.next = 0;
insert_after_range(&head, x.begin(), x.end());
}
slist &operator=(const slist &x);
~slist() {
clear();
}
///assign替换本容器的内容
private:
void _m_fill_assign(size_type n, const value_type &val);
public:
void assign(size_type n, const value_type &val) {
_m_fill_assign(n, val);
}
public:
///迭代器操作
iterator begin() { return iterator((list_node *) head.next); } //用头节点的下一个节点构造迭代器
const_iterator begin() const { return iterator((list_node *) head.next); }
iterator end() { return iterator(0); }
const_iterator end() const { return iterator(0); }
size_type size() const { return __slist_size(head.next); }
bool empty() const { return head.next == 0; }
//返回直接head的迭代器
iterator before_begin() { return iterator((list_node *) &head); } //子类指针指向基类对象
const_iterator before_begin() const { return iterator((list_node *) &head); } //子类指针指向基类对象
size_type max_size() const { return size_type(-1); } //size_type(-1) 把-1传给无符号类型size_t,得到该类型的最大值
//两个slist互换:只要将head交换互指即可
void swap(slist &L) {
list_node_base *tmp = head.next;
head.next = L.head.next;
L.head.next = tmp;
}
//取头部元素
reference front() { return ((list_node *) head.next)->data; } //用头节点的下一个节点构造节点子类用来取数据域
const_reference front() const { return ((list_node *) head.next)->data; } //用头节点的下一个节点构造节点子类用来取数据域
///插入删除
//从头部插入元素
void push_front(const value_type &x) {
__slist_make_link(&head, create_node(x));
}
void push_front() {
__slist_make_link(&head, create_node());
}
void pop_front() {
list_node *node = (list_node *) head.next;
head.next = node->next;
destroy_node(node);
}
void clear() { _m_erase_after(&head, 0); }
//找到pos的前一个节点,时间复杂度最差为O(n)
iterator previous(const_iterator pos) {
return iterator((list_node *) __slist_previous(&head, pos.node));
}
const_iterator previous(const_iterator pos) const {
return iterator((list_node *) __slist_previous(&head, pos.node));
}
///插入
private:
list_node *_M_insert_after(list_node_base *pos, const value_type &x) {
return (list_node *) (__slist_make_link(pos, create_node(x)));
}
list_node *_M_insert_after(list_node_base *pos) {
return (list_node *) (__slist_make_link(pos, create_node()));
}
void insert_after_fill(list_node_base *pos, size_type n, const value_type &x) {
for (size_type i = 0; i < n; ++i) {
__slist_make_link(pos, create_node(x));
}
}
void insert_after_range(list_node_base *pos, const_iterator first, const_iterator last) {
while (first != last) {
pos = __slist_make_link(pos, create_node(*first));
++first;
}
}
void insert_after_range(list_node_base *pos, const value_type *first, const value_type *last) {
while (first != last) {
pos = __slist_make_link(pos, create_node(*first));
++first;
}
}
public:
//在pos之后插入一个节点
iterator insert_after(iterator pos, const value_type &x) {
return iterator(_M_insert_after(pos.node, x));
}
//在pos之后插入一个默认节点
iterator insert_after(iterator pos) {
return iterator(_M_insert_after(pos.node, value_type()));
}
//插入完成后,迭代器还是指向pos
void insert_after(iterator pos, size_type n, const value_type &x) {
insert_after_fill(pos.node, n, x);
}
void insert_after(iterator pos, const_iterator first, const_iterator last) {
insert_after_range(pos.node, first, last);
}
void insert_after(iterator pos, const value_type *first, const value_type *last) {
insert_after_range(pos.node, first, last);
}
//在pos之前插入
iterator insert(iterator pos, const value_type &x) {
return iterator(_M_insert_after(__slist_previous(&head, pos.node), x));
}
iterator insert(iterator pos) {
return iterator(_M_insert_after(__slist_previous(&head, pos.node), value_type()));
}
void insert(iterator pos, size_type n, const value_type &x) {
insert_after_fill(__slist_previous(&head, pos.node), n, x);
}
void insert(iterator pos, const_iterator first, const_iterator last) {
insert_after_range(__slist_previous(&head, pos.node), first, last);
}
void insert(iterator pos, const value_type *first, const value_type *last) {
insert_after_range(__slist_previous(&head, pos.node), first, last);
}
///erase
private:
list_node_base *_m_erase_after(list_node_base *pos) {
list_node *next = (list_node *) (pos->next);
list_node_base *next_next = next->next;
pos->next = next_next;
list_node_allocator::deallocate(next);
return next_next;
}
list_node_base *_m_erase_after(list_node_base *before_first, list_node_base *last_node) {
list_node *cur = (list_node *) (before_first->next);
while (cur != last_node) {
list_node *tmp = cur;
cur = (list_node *) cur->next;
destroy_node(tmp);
}
before_first->next = last_node;
return last_node;
}
public:
iterator erase_after(iterator pos) {
return iterator((list_node *) _m_erase_after(pos.node));
}
iterator erase_after(iterator before_first, iterator last) {
return iterator((list_node *) _m_erase_after(before_first.node, last.node));
}
iterator erase(iterator pos) {
return iterator((list_node *) _m_erase_after(__slist_previous(&head, pos.node)));
}
iterator erase(iterator first, iterator last) {
return iterator((list_node *) _m_erase_after(__slist_previous(&head, first.node), last.node));
}
void resize(size_type new_size, const value_type &x);
void resize(size_type new_size) { resize(new_size, value_type()); }
//把两个单链表接合起来
void splice_after(iterator pos, iterator before_first, iterator before_list) {
if (before_first != before_list) {
__slist_splice_after(pos.node, before_first.node, before_list.node);
}
}
//接合一个节点
void splice_after(iterator pos, iterator prev) {
__slist_splice_after(pos.node, prev.node, prev.node->next);
}
void splice_after(iterator pos, slist &x) {
__slist_splice_after(pos.node, &x.head);
}
void splice(iterator pos, slist &x) {
if (x.head.next) {
__slist_splice_after(__slist_previous(&head, pos.node), &x.head, __slist_previous(&x.head, 0));
}
}
//把x链表的it所指的元素插入到本链表的pos之前
void splice(iterator pos, slist &x, iterator i) {
__slist_splice_after(__slist_previous(&head, pos.node), __slist_previous(&x.head, i.node), i.node);
}
void splice(iterator pos, slist &x, iterator first, iterator last) {
if (first != last) {
__slist_splice_after(__slist_previous(&head, pos.node),
__slist_previous(&x.head, first.node),
__slist_previous(&x.head, last.node));
}
}
public:
void reverse() {
if (head.next) {
head.next = __slist_reverse(head.next);
}
}
void remove(const value_type &val);
void unique();
void merge(slist &x);
void sort();
};
template<class T, class Alloc>
slist<T, Alloc> &slist<T, Alloc>::operator=(const slist<T, Alloc> &x) {
if (&x != this) {
list_node_base *p1 = &head;
list_node *n1 = (list_node *) head.next;
const list_node *n2 = (const list_node *) x.head.next; //x节点不能改变
while (n1 && n2) {
n1->data = n2->data; //只赋值,不插入节点
p1 = n1; //p1是用来表示本链表操作节点的上一个节点,用于之后插入删除的标识位
n1 = (list_node *) n1->next;
n2 = (const list_node *) n2->next;
}
if (n2 == 0) {
_m_erase_after(p1, 0);
} else {
insert_after_range(p1, const_iterator((list_node *) n2), const_iterator(0));
}
}
return *this;
}
template<class T, class Alloc>
void slist<T, Alloc>::_m_fill_assign(size_type n, const value_type &val) {
list_node_base *prev = &head;
list_node *node = (list_node *) head.next;
for (; node != 0 && n > 0; --n) {
node->data = val;
prev = node;
node = (list_node *) node->next;
}
if (n > 0) {
insert_after_fill(prev, n, val);
} else {}
_m_erase_after(prev, 0);
}
//每个节点的值都比较
template<class T, class Alloc>
inline bool
operator==(const slist<T, Alloc> &sl1, const slist<T, Alloc> &sl2) {
typedef typename slist<T, Alloc>::const_iterator const_iterator;
const_iterator end1 = sl1.end();
const_iterator end2 = sl2.end();
const_iterator i1 = sl1.begin();
const_iterator i2 = sl2.begin();
while (i1 != end1 && i2 != end2 && *i1 == *i2) {
++i1;
++i2;
}
return i1 == end1 && i2 == end2;
}
template<class T, class Alloc>
inline bool
operator<(const slist<T, Alloc> &sl1, const slist<T, Alloc> &sl2) {
return lexicograpical_compare(sl1.begin(), sl1.end(), sl2.begin(), sl2.end());
}
template<class T, class Alloc>
inline bool
operator!=(const slist<T, Alloc> &sl1, const slist<T, Alloc> &sl2) {
return !(sl1 == sl2);
}
template<class T, class Alloc>
inline bool
operator>(const slist<T, Alloc> &sl1, const slist<T, Alloc> &sl2) {
return sl2 < sl1;
}
template<class T, class Alloc>
inline bool
operator<=(const slist<T, Alloc> &sl1, const slist<T, Alloc> &sl2) {
return !(sl2 < sl1);
}
template<class T, class Alloc>
inline bool
operator>=(const slist<T, Alloc> &sl1, const slist<T, Alloc> &sl2) {
return !(sl1 < sl2);
}
template<class T, class Alloc>
void slist<T, Alloc>::resize(size_type len, const value_type &x) {
list_node_base *cur = &head; //从头开始遍历
while (cur->next != 0 && len > 0) {
--len;
cur = cur->next;
}
if (cur->next) {
_m_erase_after(cur, 0);
} else {
insert_after_fill(cur, len, x);
}
}
//移除data域值为val的节点
template<class T, class Alloc>
void slist<T, Alloc>::remove(const value_type &val) {
list_node_base *cur = &head;
while (cur && cur->next) {
if (((list_node *) cur->next)->data == val) {
_m_erase_after(cur);
} else {
cur = cur->next;
}
}
}
//这个unique只针对有序链表
template<class T, class Alloc>
void slist<T, Alloc>::unique() {
list_node_base *cur = head.next;
if (cur) {
while (cur->next) {
if (((list_node *) cur)->data ==
((list_node *) cur->next)->data) {
_m_erase_after(cur);
} else {
cur = cur->next;
}
}
}
}
//合并两个有序数组,默认从小到大
template<class T, class Alloc>
void slist<T, Alloc>::merge(slist &x) {
list_node_base *n1 = &head;
while (n1->next && x.head.next) {
if (((list_node *) x.head.next)->data <
((list_node *) n1->next)->data) {
__slist_splice_after(n1, &x.head, x.head.next);
}
n1 = n1->next;
}
if (x.head.next) {
n1->next = x.head.next;
x.head.next = 0;
}
}
template<class T, class Alloc>
void slist<T, Alloc>::sort() {
if (head.next && head.next->next) { //链表为空或只有一个节点,则不需要排序
slist carry;
slist counter[64];
int fill = 0;
while (!empty()) {
__slist_splice_after(&carry.head, &head, head.next); //把头节点插入carry
int i = 0;
while (i < fill && !counter[i].empty()) {
counter[i].merge(carry);
carry.swap(counter[i]);
++i;
}
carry.swap(counter[i]);
if (i == fill) {
++fill;
}
}
for (int i = 1; i < fill; ++i) {
counter[i].merge(counter[i - 1]);
}
this->swap(counter[fill - 1]);
}
}
slist_POD_test.cpp
#include "my_slist.h"
#include "iostream"
#include "my_vector.h"
template<class T>
void print_slist(mystl::slist<T> &sl) {
typename mystl::slist<T>::iterator ite = sl.begin();
typename mystl::slist<T>::iterator ite2 = sl.end();
for (; ite != ite2; ++ite) {
std::cout << *ite << ' ';
}
std::cout << std::endl;
}
template<class T>
typename mystl::slist<T>::iterator find_slist(mystl::slist<T> &l, T x) {
for (auto it = l.begin(); it != l.end(); ++it) {
if (*it == x)
return it;
}
return l.end();
}
int main() {
//默认构造
mystl::slist<int> islist;
//有参构造
mystl::slist<int> islist1(7, 2);
mystl::slist<int> islist2(3);
int ia[9] = {0, 1, 2, 3, 4, 8, 9, 3, 5};
mystl::vector<int> vec(ia, ia + 6);
mystl::slist<int> islist3(ia, ia + 9);
mystl::slist<int> islist4(vec.begin(), vec.end());
//拷贝构造
mystl::slist<int> islist5(islist4);
print_slist(islist1); //2 2 2 2 2 2 2
print_slist(islist2); //0 0 0
print_slist(islist3); //0 1 2 3 4 8 9 3 5
print_slist(islist4); //0 1 2 3 4 8
print_slist(islist5); //0 1 2 3 4 8
std::cout << islist.empty() << std::endl; //1,是空的,还没插入元素
std::cout << "size = " << islist.size() << std::endl; //size = 0
islist.push_front(9);
islist.push_front(1);
islist.push_front(2);
islist.push_front(3);
islist.push_front(4);
std::cout << "size = " << islist.size() << std::endl; //size = 5
print_slist(islist); //4 3 2 1 9
islist.pop_front();
print_slist(islist); //3 2 1 9
std::cout << islist.front() << std::endl; //3
mystl::slist<int> sl;
sl.push_front(6);
islist.swap(sl);
print_slist(islist); //6
sl.clear();
std::cout << "size = " << sl.size() << std::endl; //size = 0
std::cout << islist.front() << std::endl; //6
islist.push_front();
print_slist(islist); //0 6
//pervious 测试
print_slist(islist3); //0 1 2 3 4 8 9 3 5
mystl::slist<int>::iterator it, it_pre;
it = find_slist(islist3, 3);
if (it != islist3.end()) {
it_pre = islist3.previous(it);
}
std::cout << *it_pre << std::endl; //2
//insert_after测试
//it的位置指向的是链表中第一个3
islist3.insert_after(it);
print_slist(islist3); //0 1 2 3 0 4 8 9 3 5
islist3.insert_after(it, 7);
print_slist(islist3); //0 1 2 3 7 0 4 8 9 3 5
islist3.insert_after(it, 3, 6);
print_slist(islist3); //0 1 2 3 6 6 6 7 0 4 8 9 3 5
islist3.insert_after(it, vec.begin(), vec.begin() + 3);
print_slist(islist3); //0 1 2 3 0 1 2 6 6 6 7 0 4 8 9 3 5
//insert
islist3.insert(it);
islist3.insert(it, 4);
print_slist(islist3); //0 1 2 0 4 3 0 1 2 6 6 6 7 0 4 8 9 3 5
islist3.insert(it, 2, 66);
print_slist(islist3); //0 1 2 0 4 66 66 3 0 1 2 6 6 6 7 0 4 8 9 3 5
islist3.insert(it, vec.begin(), vec.begin() + 2);
print_slist(islist3); //0 1 2 0 4 66 66 0 1 3 0 1 2 6 6 6 7 0 4 8 9 3 5
it = islist3.erase_after(it);
std::cout << *it << std::endl; //删除后迭代器指向删除节点的下一个节点
print_slist(islist3); //0 1 2 0 4 66 66 0 1 3 1 2 6 6 6 7 0 4 8 9 3 5
islist3.erase_after(it, islist3.end());
print_slist(islist3); //0 1 2 0 4 66 66 0 1 3 1
it = find_slist(islist3, 4);
it = islist3.erase(it); //erase之后,指向节点的迭代器失效,返回指向节点下一个节点的迭代器
print_slist(islist3); //0 1 2 0 66 66 0 1 3 1
islist3.erase(it, islist3.end());
print_slist(islist3); //0 1 2 0
islist3.splice_after(islist3.begin(), islist4.begin()); //在外部是访问不到单链表的head节点的,要自己定义
mystl::slist<int>::iterator head;
head = islist4.previous(islist4.begin());
islist3.splice_after(islist3.begin(), head);
std::cout << *islist4.begin() << std::endl; //2
print_slist(islist3); //0 0 1 1 2 0
mystl::slist<int>::iterator it1 = find_slist(islist4, 8);
std::cout << *it1 << std::endl; //8
islist3.splice_after(islist3.begin(), islist4.begin(), it1);
print_slist(islist3); //0 3 4 8 0 1 1 2 0
islist3.splice_after(islist3.begin(), islist5);
print_slist(islist3); //0 0 1 2 3 4 8 3 4 8 0 1 1 2 0
it1 = find_slist(islist3, 8);
islist3.splice(it1, islist2, islist2.begin());
print_slist(islist3); //0 0 1 2 3 4 0 8 3 4 8 0 1 1 2 0
islist3.splice(it1, islist2, islist2.begin(), islist2.end());
print_slist(islist3); //0 0 1 2 3 4 0 0 0 8 3 4 8 0 1 1 2 0
islist2 = islist3;
print_slist(islist3); //0 0 1 2 3 4 0 0 0 8 3 4 8 0 1 1 2 0
print_slist(islist2); //0 0 1 2 3 4 0 0 0 8 3 4 8 0 1 1 2 0
islist2.assign(3, 6);
print_slist(islist2); //6 6 6
islist3.reverse();
print_slist(islist3); //0 2 1 1 0 8 4 3 8 0 0 0 4 3 2 1 0 0
islist3.resize(5);
print_slist(islist3); //0 2 1 1 0
islist3.remove(1);
print_slist(islist3); //0 2 0
islist2.unique();
print_slist(islist2); //6
islist3.sort();
islist2.merge(islist3);
print_slist(islist2); //0 0 2 6
return 0;
}