雙向鏈表,下一步就是類模板參數(shù)和迭代器實(shí)現(xiàn)一些簡單算法!!
成都創(chuàng)新互聯(lián)是一家專業(yè)提供北辰企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、H5頁面制作、小程序制作等業(yè)務(wù)。10年已為北辰眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站設(shè)計(jì)公司優(yōu)惠進(jìn)行中。代碼量等知識儲備夠了再優(yōu)化
異常還理解不了
#ifndef LIST_H_
#define LIST_H_
#includeusing std::underflow_error;
namespace LIST
{
//結(jié)點(diǎn)
templatestruct Node
{
inline Node() { this->front = nullptr, this->rear = nullptr; }
//前驅(qū)
Node* front;
//值
Ty value;
//后繼
Node* rear;
};
templateclass List
{
public:
//默認(rèn)
List();
//n個type
List(const int, const Type&);
//copy
List(const List&);
//析構(gòu)
~List();
//賦值
void assign(const int, const Type&);
void assign(const List&);
//尾部部操作
void push_back(const Type&);
void pop_back();
//頭部操作
void push_fornt(const Type&);
void pop_fornt();
//賦值
List& operator=(const List&);
//清空
void rease();
//是否為空
const bool empty();
//占用
const int size();
//迭代器
class Iterator
{
public:
Iterator()
{
this->it_begin = nullptr;
this->it_end = nullptr;
this->it_move = nullptr;
}
Iterator& operator++()
{
if (this->it_move == this->it_end->rear)
throw underflow_error("無效的訪問:鏈表已到底");
this->it_move = this->it_move->rear;
return *this;
}
Iterator& operator--()
{
if (this->it_move ==this->it_begin)
throw underflow_error("無效的訪問:鏈表已到頭");
this->it_move = this->it_move->front;
return *this;
}
Type& operator*()
{
if (this->it_begin == this->it_end->front)
throw underflow_error("鏈表為空,無法訪問");
return this->it_move->value;
}
Node* it_begin;
Node* it_move;
Node* it_end;
};
//返回迭代器
const Iterator begin()const
{
List::Iterator it{};
it.it_begin = this->m_forward->rear;
it.it_end = this->m_back;
it.it_move = it.it_begin;
return it;
}
const Iterator end()const
{
List::Iterator it{};
it.it_begin = this->m_forward->rear;
it.it_end = this->m_back;
it.it_move = it.it_end;
return it;
}
private:
//頭結(jié)點(diǎn)指針
Node* m_forward;
//尾節(jié)點(diǎn)指針
Node* m_back;
//自由前節(jié)點(diǎn)指針
Node* m_freed_forward;
Node* m_freed_back;
//容器占用
int m_size;
//輔助函數(shù)
//頭尾結(jié)點(diǎn)指針歸位
inline void launch();
};
//默認(rèn)構(gòu)造
templateLIST::List::List() { this->launch(); }
//n個type
templateLIST::List::List(const int n, const Type&Ty)
{
this->launch();
while (n != this->m_size)
this->push_back(Ty);
}
//copy
templateLIST::List::List(const List&li)
{
this->launch();
List::Iterator it = li.begin();
for (int i = 0; i< li.m_size; ++i)
{
this->m_back->value =*it;
this->m_back = new Node;
this->m_freed_back->rear = this->m_back;
this->m_back->front = this->m_freed_back;
this->m_freed_back = this->m_back;
++this->m_size;
++it;
}
}
//析構(gòu)
templateLIST::List::~List()
{
while(this->m_size!=0)
this->pop_back();
delete this->m_forward;
delete this->m_back;
}
//類賦值
templatevoid LIST::List::assign(const int n, const Type& Ty)
{
while (this->m_size != 0)
this->pop_back();
while (this->m_size != n)
this->push_back(Ty);
}
templatevoid LIST::List::assign(const List&li)
{
while(this->m_size!=0)
this->pop_back();
List::Iterator it = li.begin();
for (int i = 0; i< li.m_size; ++i)
{
this->m_back->value = *it;
this->m_back = new Node;
this->m_freed_back->rear = this->m_back;
this->m_back->front = this->m_freed_back;
this->m_freed_back = this->m_back;
++this->m_size;
++it;
}
}
templateList& LIST::List::operator=(const List&li)
{
while(this->m_size!=0)
this->pop_back();
List::Iterator it = li.begin();
for (int i = 0; i< li.m_size; ++i)
{
this->m_back->value = *it;
this->m_back = new Node;
this->m_freed_back->rear = this->m_back;
this->m_back->front = this->m_freed_back;
this->m_freed_back = this->m_back;
++this->m_size;
++it;
}
return *this;
}
//尾插
templatevoid LIST::List::push_back(const Type& Ty)
{
//存值
this->m_back->value = Ty;
//創(chuàng)建新的尾節(jié)點(diǎn)
this->m_back = new Node;
//鏈接
this->m_freed_back->rear = this->m_back;
this->m_back->front = this->m_freed_back;
//更新占用并歸位
this->m_freed_back = this->m_back;
++this->m_size;
}
//尾刪
templatevoid LIST::List::pop_back()
{
if (this->empty())
throw underflow_error("容器為空,刪除失敗");
//改變尾節(jié)點(diǎn)
this->m_back = this->m_back->front;
//清除
delete this->m_freed_back;
//更新占用,歸位
this->m_freed_back = this->m_back;
--this->m_size;
}
//頭插
templatevoid LIST::List::push_fornt(const Type& Ty)
{
//存值
this->m_forward->value = Ty;
//創(chuàng)建新的頭結(jié)點(diǎn)
this->m_forward = new Node;
//鏈接
this->m_freed_forward->front = this->m_forward;
this->m_forward->rear = this->m_freed_forward;
//更新占用并歸位
this->m_freed_forward = this->m_forward;
++this->m_size;
}
//頭刪
templatevoid LIST::List::pop_fornt()
{
if (this->empty())
throw underflow_error("容器為空,刪除失敗");
//改變頭結(jié)點(diǎn)
this->m_forward = this->m_forward->rear;
//清除
delete this->m_freed_forward;
//更新占用并歸位
this->m_freed_forward = this->m_forward;
--this->m_size;
}
//輔助,頭尾結(jié)點(diǎn)歸位
templatevoid LIST::List::launch()
{
//初始化指針和占用
this->m_forward = new Node;
this->m_back = new Node;
this->m_freed_forward = this->m_forward;
this->m_freed_back = this->m_back;
this->m_size = 0;
//鏈接
this->m_forward->rear = this->m_back;
this->m_back->front = this->m_forward;
}
//雜項(xiàng)函數(shù)
templateconst bool LIST::List::empty()
{
if (this->m_size)
return false;
return true;
}
templateconst int LIST::List::size()
{
return this->m_size;
}
templatevoid LIST::List::rease()
{
while(this->m_size!=0)
this->pop_back();
}
}
#endif // !LIST_H_
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧