新聞中心
這里有您想知道的互聯(lián)網營銷解決方案
c++第一次實現(xiàn)雙向鏈表附迭代器-創(chuàng)新互聯(lián)
雙向鏈表,下一步就是類模板參數(shù)和迭代器實現(xiàn)一些簡單算法!!
成都創(chuàng)新互聯(lián)是一家專業(yè)提供北辰企業(yè)網站建設,專注與成都網站設計、成都網站建設、外貿網站建設、H5頁面制作、小程序制作等業(yè)務。10年已為北辰眾多企業(yè)、政府機構等服務。創(chuàng)新互聯(lián)專業(yè)網站設計公司優(yōu)惠進行中。
代碼量等知識儲備夠了再優(yōu)化
異常還理解不了
#ifndef LIST_H_
#define LIST_H_
#includeusing std::underflow_error;
namespace LIST
{
//結點
templatestruct Node
{
inline Node() { this->front = nullptr, this->rear = nullptr; }
//前驅
Node* front;
//值
Ty value;
//后繼
Node* rear;
};
templateclass List
{
public:
//默認
List();
//n個type
List(const int, const Type&);
//copy
List(const List&);
//析構
~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:
//頭結點指針
Node* m_forward;
//尾節(jié)點指針
Node* m_back;
//自由前節(jié)點指針
Node* m_freed_forward;
Node* m_freed_back;
//容器占用
int m_size;
//輔助函數(shù)
//頭尾結點指針歸位
inline void launch();
};
//默認構造
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;
}
}
//析構
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é)點
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é)點
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)建新的頭結點
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("容器為空,刪除失敗");
//改變頭結點
this->m_forward = this->m_forward->rear;
//清除
delete this->m_freed_forward;
//更新占用并歸位
this->m_freed_forward = this->m_forward;
--this->m_size;
}
//輔助,頭尾結點歸位
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;
}
//雜項函數(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)定的海外服務器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調度確保服務器高可用性,企業(yè)級服務器適合批量采購,新人活動首月15元起,快前往官網查看詳情吧
名稱欄目:c++第一次實現(xiàn)雙向鏈表附迭代器-創(chuàng)新互聯(lián)
網站網址:http://www.dlmjj.cn/article/ccjchs.html