Class template implementation of chain queue for [data structure] Queue

Source: Internet
Author: User

Chained queue is a kind of storage representation based on single linked list

The head pointer of the queue points to the first node of the single-linked list, and the tail pointer points to the last node in the single-linked list.

Exiting an element removes the node from the head pointer, adding the element adds a node at the end of the queue

Conditions of Use: data element changes relatively large situation. There is no overflow condition


Abstract base class for queues:

#ifndef queue#define queue//Queue abstract base class Template<class T>class Queue{public:    queue () {}    ~queue () {}    virtual bool EnQueue (const t& x) =0;    virtual bool DeQueue (t& x) =0;    virtual bool Getfront (t& x) const=0;    virtual bool IsEmpty () const=0;    virtual bool Isfull () const=0;    virtual int getsize () const=0;}; #endif

The specific implementation of the chain queue:


#include "Stack.h" #include <iostream>//#include <cstdlib> #include <cassert      >using namespace Std;template<class t> struct Linknode//List node class {T data;        linknode<t>* link; Linknode (linknode<t>* ptr=null): Link (ptr) {} linknode (const t& item,linknode<t>* ptr=null):d ATA (item ), link (ptr) {}}; Template<class t>class linkedqueue:public queue<t>{public:linkedqueue (): Front (null), rear (null) {} ~Link    Edqueue () {makeempty ();}    Linkedqueue (const linkedqueue<t>& RHS);    linkedqueue<t>& operator= (const linkedqueue<t>& RHS);    BOOL EnQueue (const t& x);    BOOL DeQueue (t& x);    BOOL Getfront (t& x) const;    BOOL IsEmpty () Const{return (front==null)? True:false;} BOOL Isfull () Const{return false;}    Meaningless, can never be full int getsize () const;    void Makeempty (); Friend ostream& operator<< <T> (ostream& os,const linkedqueue<t>& rhs);p ROtected:linknode<t> *front,*rear;};    Template<class t>void Linkedqueue<t>::makeempty () {linknode<t>* p;        while (front!=null) {p=front;        front=front->link;    Delete p; } Rear=null;} Template<class t>bool linkedqueue<t>::enqueue (const t& x) {if (front==null) {front=rear=new LinkN        Ode<t> (x); if (front==null| |    Rear==null)//A detection of the new return value, the security mechanism return false;        } else{rear->link=new linknode<t> (x);        if (rear->link==null) return false;    rear=rear->link; } return true;    Template<class T>bool linkedqueue<t>::D equeue (t& x) {if (IsEmpty ()) return false;    linknode<t>* P=front;        if (front==rear) {//Only one node is in the case front=front->link;    Rear=null;    }else{front=front->link;    } x=p->data;    Delete p; return true;} Template<class T>bool Linkedqueue<t>::getfront (t& x) const{    if (IsEmpty ()) return false;    x=front->data; return true;}    Template<class t>int linkedqueue<t>::getsize () const{int k=0;    linknode<t>* P=front;        while (p!=null) {++k;    p=p->link; } return k;} Template<class t>ostream& operator<< (ostream& os,const linkedqueue<t>& rhs) {os<    < "size:" <<rhs.getsize () <<endl;    Linknode<t> *p=rhs.front;    os<< "elements:";          while (p!=null) {os<<p->data<< "";    p=p->link;    } os<<endl; return OS;} Template<class t>linkedqueue<t>::linkedqueue (const linkedqueue<t>& RHS) {LinkNode<T> *    Src=rhs.front;    Linknode<t> *dest=nullptr,*newnode=nullptr;    Front=nullptr;        while (src!=null) {newnode=new linknode<t> (src->data);            if (front==nullptr) {//Chain list is a single linked list without additional head nodes, it is more special front=newnode;        Dest=newnode;          }else{  dest->link=newnode;        Dest=newnode;    } src=src->link; } rear=dest;} Template<class t>linkedqueue<t>& linkedqueue<t>::operator= (const LinkedQueue<T>&    RHS) {makeempty ();    Linknode<t> *src=rhs.front;    Linknode<t> *dest=nullptr,*newnode=nullptr;    Front=nullptr;        while (src!=nullptr) {newnode=new linknode<t> (src->data);            if (front==null) {front=newnode;        Dest=newnode;            }else{dest->link=newnode;        Dest=newnode;    } src=src->link;    } rear=dest; return *this;}

Test function:


int main (int argc, char* argv[]) {    linkedqueue<int> s;    int a=1,b=2,c=3,d=4,e=0;    S.enqueue (a);    S.enqueue (b);    S.enqueue (c);    S.enqueue (d);    cout<<s;    S.dequeue (e);    S.dequeue (e);    cout<<s;    cout<< "GetSize ():" <<s.getsize () <<endl;    cout<<boolalpha;    cout<< "IsEmpty ():" <<s.isempty () <<endl;    cout<< "Isfull ():" <<s.isfull () <<endl;    cout<<noboolalpha;    S.getfront (e);    cout<< "Getfront ():" <<e<<endl;        cout<<endl;    cout<< "Copy and copy assignment" <<endl;    linkedqueue<int> S1 (s), S2;    cout<<s1;    S2=s;    cout<<s2;    System ("pause");    return 0;}

The test results are as follows:


Size:4elements:1 2 3 4size:2elements:3 4getSize (): 2IsEmpty (): Falseisfull (): Falsegetfront (): 3copy and copy Assignment Size:2elements:3 4size:2elements:3 4 Please press any key to continue ...


Precautions:

1. To detect the returned pointer value, such as when using new to detect the return value, to see if the memory is allocated successfully. This is a security mechanism anytime, anywhere.

2. The linked list used for this implementation is a single-linked list with no additional head nodes, so it is important to note that the copy constructor and copy copy functions are written.

3. Think clearly in writing code, otherwise there are many loopholes.











Class template implementation of chain queue for [data structure] 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.