Non-cyclic chain class C + + definition

Source: Internet
Author: User

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&LT;&LT;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&LT;&LT;SETW (6) <<p->data;        p=p->next;    n++;    } cout<<endl; COUT&LT;&LT;SETW (6) <<"↑"; for(inti =0; I < n2; i++) {COUT&LT;&LT;SETW (6) <<" "; } COUT&LT;&LT;SETW (6) <<"↑"<<endl;    cout<<endl; COUT&LT;&LT;SETW (6) <<"Front"; for(inti =0; I < n2; i++) {COUT&LT;&LT;SETW (6) <<" "; } COUT&LT;&LT;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

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.