Class C ++ definition of non-Cyclic Chain, and loop Definition
Using a linked list to implement queues has its unique advantages. The linked list allows you to flexibly Delete and add nodes. This is especially true for queues. To use ordered tables to implement queues, you have to perform cyclic operations to effectively use the space. That is to say, overflow still occurs. Therefore, the chain table is refreshing!
Let's not talk about anything. Go to the code
//////////////////////////////////////// //// // LinkQueue. h # include "stdafx. h "# include <cassert> # include <iomanip> # include <iostream> using namespace std; const int ERROR =-1; const int OK = 1; typedef int Status; // indicates the status of the operation result // C ++ description template of the non-Cyclic Chain data Structure <typename ElemType> class LinkQueue {public: class LinkNode {public: ElemType data; linkNode * next;}; typedef LinkNode * NodePointer; LinkQueue ();~ LinkQueue (); void randLinkQueue (); void display (); void clear (); Status deQueue (ElemType & e); Status enQueue (ElemType & e); private: NodePointer front; nodePointer rear ;}; //////////////////////////////////////// /automatically call the constructor and destructor template <typename ElemType> LinkQueue <ElemType>:: LinkQueue () {}template <typename ElemType> LinkQueue <ElemType> ::~ LinkQueue () {} template <typename ElemType> void LinkQueue <ElemType>: randLinkQueue () {srand (unsigned (time (NULL); int n; elemType Elem [11]; NodePointer p, s; n = rand () % 10 + 1; cout <"generates a random array:" <endl; for (int I = 0; I <n; I ++) {Elem [I] = rand () % 100 + 1; cout <setw (6) <Elem [I];} front = NULL; for (int I = 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> void LinkQueue <ElemType >:: display () {NodePointer p; int n = 0; p = front; cout <endl <"produces a non-Cyclic Chain:" <endl; while (p! = NULL) {cout <setw (6) <p-> data; p = p-> next; n ++;} cout <endl; cout <setw (6) <"handle"; for (int I = 0; I <N-2; I ++) {cout <setw (6) <"" ;}cout <setw (6) <"external" <endl; cout <setw (6) <"front "; for (int I = 0; I <N-2; I ++) {cout <setw (6) <";}cout <setw (6) <"rear" <endl;} template <typename ElemType> Status LinkQueue <ElemType>: deQueue (ElemType & e) {NodePointer p; p = front; if (p = NULL) retur N ERROR; e = p-> data; front = p-> next; delete p; return OK;} 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; return OK ;}
// LinkQueueTest. cpp: defines the entry point of the console application. // # Include "stdafx. h "# include" LinkQueue. h "# include <iostream> using namespace std; int _ tmain (int argc, _ TCHAR * argv []) {LinkQueue <int> SCSI; int a; SCSI. randLinkQueue (); SCSI. deQueue (a); SCSI. display (); cout <"input elements to enter the queue:" <endl; cin> a; SCSI. enQueue (a); SCSI. display (); system ("pause"); return 0 ;}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.