隊(duì)列形象的說就是大家放學(xué)去餐廳買飯要排隊(duì)一樣,先去的人就能先吃到,first in first out
成都創(chuàng)新互聯(lián)公司于2013年開始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站制作、成都網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元丹寨做網(wǎng)站,已為上家服務(wù),為丹寨各地企業(yè)和個人服務(wù),聯(lián)系電話:028-86922220說再多都是多余的,還是直接上代碼吧(ps.簡單粗暴的我,哈哈哈)
.h
#include<iostream>
using namespace std;
template<class T>
struct Node
{
Node<T>* _next;
T _data;
//這個不能忘
Node( T data)
:_next(NULL)
,_data(data)
{}
};
template<class T>
class queue
{
public:
//構(gòu)造函數(shù)
queue()
:_head(NULL)
,_tail(NULL)
{}
//拷貝構(gòu)造函數(shù)
queue(const queue<T>& q)
:_head(NULL)
,_tail(NULL)
{
Node<T>*cur=q._head;
while(cur)
{
this->push(cur->_data);
cur=cur->_next;
}
}
//賦值運(yùn)算符重載
queue<T>& operator=(const queue<T>& q)
{
if(this!=&q)
{
delete [] q;
Node<T>*cur=q._head;
while(cur)
{
this->push(cur->_data);
cur=cur->_next;
}
return *this;
}
}
//析構(gòu)函數(shù)
~queue()
{
Node<T>* cur=_head;
if(cur)
{
Node<T>* del=cur;
cur=cur->_next;
delete del;
del=NULL;
}
}
//入隊(duì),相當(dāng)于尾插函數(shù)
void push(const T& x)
{
Node<T>* newNode=new Node<T>(x);
if(_head==NULL)
{
_head=newNode;
_tail=_head;
}
else
{
_tail->_next=newNode;
_tail=newNode;
}
}
//出隊(duì),相當(dāng)于頭插函數(shù)
void pop()
{
if(_head!=NULL)
{
Node<T>* del=_head;
_head=_head->_next;
delete del;
}
}
//打印隊(duì)列元素
void print()
{
Node<T>* cur=_head;
if(_head==NULL)
{
return;
}
else
{
while(cur)
{
cout<<cur->_data<<" ";
cur=cur->_next;
}
cout<<"over"<<endl;
}
}
//
T&Front()輸出對頭元素
{
if(!Empty)
{
return _head->_data;
}
}
//輸出隊(duì)尾元素
T& Back()
{
if(!Empty)
{
return _tail->_data;
}
}
//判斷隊(duì)列是否為空
bool Empty()
{
return (_head==NULL);
}
protected:
Node<T>*_head;
Node<T>*_tail;
};
.cpp
void TestQueue()
{
queue<int> q1;
q1.push(1);
q1.push(2);
q1.push(3);
q1.push(4);
queue<int> q2(q1);
queue<int> q3=q2;
q1.print();
q2.print();
q3.print();
}
int main()
{
TestQueue();
system("pause");
return 0;
}
運(yùn)行結(jié)果
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)站標(biāo)題:c++模板實(shí)現(xiàn)隊(duì)列-創(chuàng)新互聯(lián)
網(wǎng)頁網(wǎng)址:http://muchs.cn/article22/cdcdcc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、網(wǎng)頁設(shè)計(jì)公司、搜索引擎優(yōu)化、Google、ChatGPT、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容