C + + Template implementation queue

Source: Internet
Author: User


The image of the queue is that everyone from school to the restaurant to buy food to queue, the first person can eat, first

Say more is redundant, or directly on the code bar (Ps. Simple rough me, haha ha)

. h

#include <iostream>

using namespace Std;

Template<class t>

struct Node

{

node<t>* _next;

T _data;

This can't be forgotten.

Node (T data)

: _next (NULL)

, _data (data)

{}

};

Template<class t>

Class Queue

{

Public

constructor function

Queue ()

: _head (NULL)

, _tail (NULL)


{}

Copy constructor

Queue (const queue<t>& q)

: _head (NULL)

, _tail (NULL)

{

node<t>*cur=q._head;

while (cur)

{

This->push (Cur->_data);

cur=cur->_next;

}

}

Assignment operator overloading

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;

}

}

Destructors

~queue ()

{

node<t>* Cur=_head;

if (cur)

{

node<t>* del=cur;

cur=cur->_next;

Delete del;

Del=null;

}

}

Queue, equivalent to the tail interpolation function

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;

}

}

Out of the team, equivalent to the head interpolation function

void Pop ()

{

if (_head!=null)

{

node<t>* Del=_head;

_head=_head->_next;

Delete del;

}

}

Print queue elements

void print ()

{

node<t>* Cur=_head;

if (_head==null)

{

Return

}

Else

{

while (cur)

{

cout<<cur->_data<< "";

cur=cur->_next;

}

cout<< "Over" <<endl;

}

}

//

T&front () outputs the HEAD element

{

if (! Empty)

{

Return _head->_data;

}

}

Output Team Tail element

t& back ()

{

if (! Empty)

{

Return _tail->_data;

}

}

Determine if the queue is empty

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;

}

Run results

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7E/CB/wKioL1cI7q7TBHMkAAAnHfrUk6c212.png "title=" Capture 4. PNG "alt=" Wkiol1ci7q7tbhmkaaanhfruk6c212.png "/>


C + + Template implementation queue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.