The use of linked lists to implement the queue has its unique conditions, linked list of flexible node deletion and increase operations, especially for the implementation of the queue is a piece of cake. The use of sequential tables to implement the queue also has to be used for efficient use of space for the loop operation, that is, there will still be overflow phenomenon, so, or the list to the refreshing!
Don't say anything, on the code
/////////////////////////// /// /// /// /////////// LinkQueue.h#include "stdafx.h"#include <cassert>#include <iomanip>#include <iostream>usingnamespace Std;Const interror=-1;Const intok=1; typedefintStatus;//Indicates the status of the result of the Operation/// Non-circular chain data structure C + + descriptionTemplate<typename Elemtype>class linkqueue{ Public:classLinknode { Public: Elemtype data; Linknode * NEXT; }; typedef linknode* Nodepointer; Linkqueue (); ~linkqueue ();voidRandlinkqueue ();voidDisplay ();voidClear (); Status deQueue (Elemtype &e); Status enQueue (Elemtype &e);Private: Nodepointer Front; Nodepointer rear;};/////////////////////////// /// /// /// /////Automatically call constructors and destructorsTemplate<typename Elemtype>linkqueue<elemtype>::linkqueue () {}template <typename ElemType> Linkqueue<elemtype>::~linkqueue () {}template<typename elemtype>voidLinkqueue<elemtype>::randlinkqueue () {srand (Unsigned (Time (NULL)));intN Elemtype elem[ One]; Nodepointer P,s; N=rand ()%Ten+1; cout<<"The resulting random array is:"<<endl; for(inti =0; I < n; i++) {Elem[i]=rand ()% -+1; COUT<<SETW (6) <<Elem[i]; } front=null; for(inti =0; I < n; i++) {s=New(Linknode); ASSERT (s!=0); s->data=elem[i]; s->next=null;if(Front==null) Front=rear=s;Else{rear->next=s; rear=rear->next; }} display ();} Template<typename elemtype>voidLinkqueue<elemtype>::d isplay () {nodepointer p;intn=0; P=front; cout<<endl<<"The resulting non-circular chain is:"<<endl; while(P!=null) {COUT<<SETW (6) <<p->data; p=p->next; n++; } cout<<endl; COUT<<SETW (6) <<"↑"; for(inti =0; I < n2; i++) {COUT<<SETW (6) <<" "; } COUT<<SETW (6) <<"↑"<<endl; cout<<endl; COUT<<SETW (6) <<"Front"; for(inti =0; I < n2; i++) {COUT<<SETW (6) <<" "; } COUT<<SETW (6) <<"Rear"<<endl;} Template<typename elemtype>status linkqueue<elemtype>::d equeue (Elemtype &e) {NodePointer p; P=front;if(P==null)returnERROR; e=p->data; front=p->next; Delete p;returnOK;} Template<typename elemtype>status linkqueue<elemtype>::enqueue (elemtype &e) {NodePointer s; s=New(Linknode); ASSERT (s!=0); s->data=e; s->next=null;if(Front==null) Front=rear=s; rear->next=s;returnOK;}
//LinkQueueTest.cpp: Defines the entry point of the console application. //#include "stdafx.h"#include "LinkQueue.h"#include <iostream>using namespace STD;int_tmain (intARGC, _tchar* argv[]) {linkqueue<int> LQ;intA Lq.randlinkqueue (); Lq.dequeue (a); Lq.display ();cout<<"Enter the element to enter the queue:"<<endl;Cin>>a; Lq.enqueue (a); Lq.display (); System"Pause");return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Non-cyclic chain class C + + definition