#include <iostream>
#include<stdexcept>
using namespace std;
template<typename T>
class List{
public:
//构造函数 和 析构函数
List():m_head(NULL), m_tail(NULL){}
~List(){
clear();
}
//拷贝构造 和 赋值构造
List(const List& that):m_head(NULL), m_tail(NULL){
for(Node* node = that.m_head; node; node = node->m_next){
this->push_back(node->m_data);
}
}
List& operator=(const List& that){
if(this != &that){
List temp = that;
swap(m_head, temp.m_head);
swap(m_tail, temp.m_tail);
}
return *this;
}
//清空链表
void clear(void){
for(Node* node = m_head, *next; node; node = next){
next = node->m_next;
delete node;
}
m_head = NULL;
m_tail = NULL;
}
//是否为空
bool empty(void) const {
return !m_head && !m_tail;
}
//元素个数
size_t size(void)const{
size_t count = 0;
for(Node* node = m_head; node; node = node->m_next){
count++;
}
return count;
}
//得到首元素
T& front(void){
if(empty()){
throw underflow_error("list under flow");
}
return m_head->m_data;
}
const T& front(void) const{
return const_cast<List*>(this)->front();
}
//插入首元素
void push_front(const T& data){
m_head = new Node(data, NULL, m_head);
if(m_head->m_next){
m_head->m_next->m_prev = m_head;
}else{
m_tail = m_head;
}
}
//删除首元素
void pop_front(void){
if(empty()){
throw underflow_error("list under flow");
}
Node* temp = m_head;
m_head = m_head->m_next;
delete temp;
temp = NULL;
if(m_head){
m_head->m_prev = NULL;
}else{
m_tail = NULL;
}
}
//得到尾元素
T& back(void){
if(empty()){
throw underflow_error("list under flow");
}
return m_tail->m_data;
}
const T& back(void) const {
return const_cast<List*>(this)->back();
}
//插入尾元素
void push_back(const T& data){
m_tail = new Node(data, m_tail, NULL);
if(m_tail->m_prev){
m_tail->m_prev->m_next = m_tail;
}else{
m_head = m_tail;
}
}
//删除尾元素
void pop_back(void){
if(empty()){
throw underflow_error("list under flow");
}
Node* temp = m_tail;
m_tail = m_tail->m_prev;
delete temp;
temp = NULL;
if(m_tail){
m_tail->m_next = NULL;
}else{
m_head = NULL;
}
}
//删除某个特定的元素
void remove(const T& data){
for(Node* node = m_head, *next; node; node = next){
next = node->m_next;
if(data == node->m_data){
if(node->m_prev){
node->m_prev->m_next = node->m_next;
}else{
m_head = node->m_next;
}
if(node->m_next){
node->m_next->m_prev = node->m_prev;
}else{
m_tail = node->m_prev;
}
delete node;
}
}
}
//打印整个链表
friend ostream& operator<<(ostream& os, const List& list){
for(Node* node = list.m_head; node; node = node->m_next){
os << *node;
}
return os;
}
// 节点的定义
private:
class Node{
public:
Node(const T& data, Node* prev = NULL, Node* next = NULL):m_data(data),m_prev(prev),m_next(next){}
friend ostream& operator<<(ostream& os, const Node& node){
return os << "["<< node.m_data << "]";
}
T m_data;
Node* m_prev;
Node* m_next;
};
Node* m_head;
Node* m_tail;
// 迭代器定义
public:
class Iterator{
private:
Node* m_head;
Node* m_tail;
Node* m_node;
public:
Iterator(Node* head = NULL, Node* tail = NULL, Node* node = NULL):m_head(head),m_tail(tail),m_node(node){}
bool operator==(const Iterator& that){
return m_node == that.m_node;
}
bool operator!=(const Iterator& that){
return !(*this==that);
}
Iterator& operator++(){
if(m_node)
m_node = m_node->m_next;
else
m_node = m_head;
return *this;
}
const Iterator& operator++(int){
Iterator temp = *this;
++(*this);
return temp;
}
Iterator& operator--(){
if(m_node)
m_node = m_node->m_prev;
else
m_node = m_tail;
return *this;
}
const Iterator& operator--(int){
Iterator temp = *this;
--(*this);
return temp;
}
T& operator*()const{
return m_node->m_data;
}
T* operator->()const{
//return &**this;
return &((*this).m_node->m_data);
}
};
const Iterator begin(){
return Iterator(m_head, m_tail, m_head);
}
const Iterator end(){
return Iterator(m_head, m_tail, NULL);
}
};
void test1(){
List<int> list;
for(int i = 0; i< 10; i++)
list.push_back(i);
cout << list << endl;
for(List<int>::Iterator it = list.begin(); it != list.end(); ++it){
cout << *it << endl;
}
}
int main(){
test1();
return 0;
}
使用模板类实现List容器&迭代器
最新推荐文章于 2023-04-12 23:22:18 发布