参考STL的list,C++实现int类型元素链表,节点包含前后双指针。
#include <iostream>
#include <algorithm>
struct ListNode
{
int element;
ListNode* prev;
ListNode* next;
ListNode() :element(0), prev(nullptr), next(nullptr) {}
ListNode(int element, ListNode* prev, ListNode* next) : element(element), prev(prev), next(next) {}
};
class List
{
public:
class iterator {
public:
iterator() :current(nullptr) {}
iterator(ListNode* node) : current(node) {}
int& operator*() {
return current->element;
};
iterator operator++() { //前置
current = current->next;
return *this;
}
iterator operator--() {//前置
current = current->prev;
return *this;
}
bool operator==(const iterator& rhs) const {
return current == rhs.current;
}
bool operator!=(const iterator& rhs) const {
return current != rhs.current;
}
iterator operator++(int) {//后置
iterator old = *this;
current = current->next;
return old;
}
iterator operator--(int) {//后置
iterator old = *this;
current = current->prev;
return old;
}
private:
ListNode* current;
friend List;
};
public:
List() :theSize(0) {
head = new ListNode;
tail = new ListNode;
head->next = tail;
tail->prev = head;
}
~List() {
clear();
delete head;
delete tail;
}
List(const List& obj) :theSize(0) {
head = new ListNode;
tail = new ListNode;
head->next = tail;
tail->prev = head;
for (auto& e : obj) {
push_back(e);
}
}
List& operator=(const List& obj) { //返回引用支持连续赋值
List temp = obj;
std::swap(*this, temp);
return *this;
}
List(List&& obj) : theSize(obj.theSize), head(obj.head), tail(obj.tail) {
obj.theSize = 0;
obj.head = nullptr;
obj.tail = nullptr;
}
List& operator=(List&& obj) {
std::swap(*this, obj);
return *this;
}
int size() {
return theSize;
}
bool empty() {
return theSize == 0;
}
iterator end() const {
return iterator(tail);
}
iterator begin() const {
return iterator(head->next);
}
void push_front(int data) {
insert(begin(), data);
}
void push_back(int data) {
insert(end(), data);
}
int back() {
return *(--end());
}
int front() {
return *begin();
}
void pop_back() {
remove(--end());
}
void pop_front() {
remove(begin());
}
void clear() {
while (!empty())
pop_back();
}
private:
void insert(iterator it, int data) {
ListNode* pos = it.current;
pos->prev->next = new ListNode(data, pos->prev, pos);
pos->prev = pos->prev->next;
++theSize;
}
void remove(iterator it) {
ListNode* pos = it.current;
pos->prev->next = pos->next;
pos->next->prev = pos->prev;
--theSize;
delete pos;
}
private:
ListNode* head;
ListNode* tail;
int theSize;
};
int main() {
List list;
std::cout << std::boolalpha;
for (int i = 0; i < 10; ++i) {
list.push_back(i);
}
std::cout << "list: ";
for (int& it : list) {
std::cout << it << " ";
}
std::cout << std::endl;
std::cout << "size: " << list.size() << std::endl;
list.push_front(100);
std::cout << "list: ";
for (int& it : list) {
std::cout << it << " ";
}
std::cout << std::endl;
std::cout << "front: " << list.front() << std::endl;
std::cout << "back: " << list.back() << std::endl;
list.pop_back();
list.pop_front();
std::cout << "list: ";
for (int& it : list) {
std::cout << it << " ";
}
std::cout << std::endl;
List list1(list);
std::cout << "list1: ";
std::for_each(list1.begin(), list1.end(), [](int elem) {
std::cout << elem << " ";
});
std::cout << std::endl;
List list2 = list1; //调用拷贝构造函数?
std::cout << "list2: ";
for (auto& e : list2) {
std::cout << e << " ";
}
std::cout << std::endl;
List list3(std::move(list1));
std::cout << "list3: ";
for (auto& e : list3) {
std::cout << e << " ";
}
std::cout << std::endl;
List list4 = std::move(list2);//调用移动构造函数?
std::cout << "list4: ";
for (auto& e : list4) {
std::cout << e << " ";
}
std::cout << std::endl;
std::cout << "list: ";
for (List::iterator it = list.begin(); it != list.end(); it++)
{
std::cout << *it << " ";
}
std::cout << std::endl;
list.clear();
std::cout << "empty: " << list.empty() << std::endl;
return 0;
}
输出:
list: 0 1 2 3 4 5 6 7 8 9
size: 10
list: 100 0 1 2 3 4 5 6 7 8 9
front: 100
back: 9
list: 0 1 2 3 4 5 6 7 8
list1: 0 1 2 3 4 5 6 7 8
list2: 0 1 2 3 4 5 6 7 8
list3: 0 1 2 3 4 5 6 7 8
list4: 0 1 2 3 4 5 6 7 8
list: 0 1 2 3 4 5 6 7 8
empty: true