189 8069 5689

c++模板实现队列-创新互联

队列形象的说就是大家放学去餐厅买饭要排队一样,先去的人就能先吃到,first in first out

成都创新互联公司于2013年开始,是专业互联网技术服务公司,拥有项目网站制作、成都网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元丹寨做网站,已为上家服务,为丹寨各地企业和个人服务,联系电话:028-86922220

说再多都是多余的,还是直接上代码吧(ps.简单粗暴的我,哈哈哈)

.h

#include

using namespace std;

template

struct Node

{

Node* _next;

T  _data;

//这个不能忘

Node( T data)

:_next(NULL)

,_data(data)

{}

};

template

class queue

{

public:

//构造函数

queue()

:_head(NULL)

,_tail(NULL)

{}

//拷贝构造函数

queue(const queue& q)

:_head(NULL)

,_tail(NULL)

{

 Node*cur=q._head;

 while(cur)

 {

 this->push(cur->_data);

 cur=cur->_next;

 }

}

//赋值运算符重载

queue& operator=(const queue& q)

{

if(this!=&q)

{

delete [] q;

 Node*cur=q._head;

 while(cur)

 {

 this->push(cur->_data);

 cur=cur->_next;

 }

 return *this;

}

}

//析构函数

~queue()

{

Node* cur=_head;

 if(cur)

{

Node* del=cur;

cur=cur->_next;

delete del;

del=NULL;

}

}

//入队,相当于尾插函数

void push(const T& x)

{

Node* newNode=new Node(x);

if(_head==NULL)

{

_head=newNode;

_tail=_head;

}

else

{

_tail->_next=newNode;

_tail=newNode;

}

}

//出队,相当于头插函数

void pop()

{

if(_head!=NULL)

{

  Node* del=_head;

 _head=_head->_next;

  delete del;

}

}

//打印队列元素

void print()

{

Node* cur=_head;

if(_head==NULL)

{

 return;

}

else

{

while(cur)

{

cout<_data<<" ";

cur=cur->_next;

}

cout<<"over"<

}

}

//

T&Front()输出对头元素

{

if(!Empty)

{

 return _head->_data;

}

}

//输出队尾元素

T& Back()

{

if(!Empty)

{

 return _tail->_data;

}

}

//判断队列是否为空

bool Empty()

{

return (_head==NULL);

}

protected:

Node*_head;

Node*_tail;

};

.cpp

void TestQueue()

{

queue q1;

q1.push(1);

q1.push(2);

q1.push(3);

q1.push(4);

queue q2(q1);

queue q3=q2;

q1.print();

q2.print();

q3.print();

}

int main()

{

TestQueue();

system("pause");

return 0;

}

运行结果

c++模板实现队列

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享名称:c++模板实现队列-创新互联
转载来源:http://cdxtjz.cn/article/cdcdcc.html

其他资讯