The archive
1 #include "iostream.h" 2 #include "stdlib.h" 3 #define max 20 4 typedef char elemtype; 5 #include "queue.h" 6 void main() 7 { 8 elemtype e; 9 queue q; 10 cout<<"(1) initialize queue q"<<endl; 11 initqueue(q); 12 cout<<"(2) <<(queueEmpty (q)? Empty ":" not empty ")<<endl; 13 cout<<"(3) Enter a sequence of letters ending with '#' :"<<endl; 14 cin>>e; 15 while(e! ='#') 16 { 17 enqueue(q,e); 18 cin>>e; 19} 20 cout<<"(4) <<(queueEmpty (q)? Empty ":" not empty ")<<endl; 21 e=dequeue(q); 22 cout<<"(5a) queue an element dequeue() :"<<e<<endl; 23 if(dequeue1(q,e)) 24 cout<<"(5b) queue1() is "<<e<<endl; <<queuelength(q)<<endl; 26 cout<<"(7) empty queue "<<endl; 27 clearqueue(q); <<queuelength(q)<<endl; 29 cout<<"(9) character ABC is queued "<<endl; 30 enqueue(q,'a'); 31 enqueue(q,'b'); 32 enqueue(q,'c'); 33 e=gethead(q); 34 cout<<"(10a) gethead() = "<<e<<endl; 35 if (gethead1(q,e)) 36 cout<<"(10b) gethead1 <<endl; 37 cout<<"(11) number of elements in queue :"<<queuelength(q)<<endl; 38 cout<<"(12) all elements out of queue :"; 39 while(! queueempty(q)) 40 cout<<dequeue(q)<<" "; 41 cout<<endl; <<queuelength(q)<<endl; 43 cout<<"(14) release queue "<<endl; 44 destoryqueue(q); 45}Copy the code
1 typedef struct 2 { 3 elemtype *base; // allocate storage space dynamically 4 int front; // the header pointer points to the queue header element if the queue is not empty. If the queue is not empty, it points to the next element at the end of the queue. 7 void initQueue (queue &q) 8 {9 // Initialize the queue 10 q.case =new elemType [Max]; // Allocate storage space. Q.case) 12 {13 cout<<" queue allocation failed \n"; 14 exit(-2); 15 } 16 else 17 q.front=q.rear=0; 19 void clearQueue (queue &q) 20 {21 // Clear the queue without destroying it. Q.ear =0; 24} 25 int queueempty(queue q) 26 {27 if(q.front== q.ear)// If (q.front== q.ear)// If (q.front== q.ear) 30 else 31 return 0; 32} 33 int queuelength(queue q) 34 {35 return (q.ear -q.front+ Max)% Max; 38 void enqueue(queue &q, elemType e) 39 {40 // The queue entry operation 41 if((q.ear +1)% Max ==q.front)// The queue is full 42 {43 Cout <<" Queue full, unable to insert new element!" <<endl; 44 exit(-2); 45 } 46 else 47 { 48 q.base[q.rear]=e; Q.ear =(q.ear +1)% Max; q.ear =(q.ear +1)% Max; Elemtype dequeue(queue&q) 53 {54 // Indicates the operation to exit the queue. 55 if(q.front== q.ear)// Indicates that the queue cannot exit. 56 {57 // Indicates that the queue is empty Cout <<" Empty queue, can't delete header element!" <<endl; 59 exit(-2); 60 } 61 else 62 { 63 elemtype e=q.base[q.front]; // The current queue header element is returned. 64 q.front=(q.front+1)% Max; Return e; 66} 67} 68 int dequeue1(queue &q, elemType &e) 69 {70 if(q.front== q.ear)// The queue is empty. 72 {73 // The queue is empty Cout <<" Empty queue, can't delete header element!" <<endl; 75 return 0; 76 } 77 else 78 { 79 e=q.base[q.front]; // The current queue header element is returned 80 q.front=(q.front+1)% Max; // return 1; 82} 83} 84 elemType gethead(queue q) 85 {86 if(q.front== q.ear)// No queue can be read 88 {89 // Queue is empty 90 Cout <<" empty queue, headless element "<<endl; 91 exit(-2); 92 } 93 else 94 return q.base[q.front]; 95} 96 void destoryQueue (queue &q) 97 {99 delete q.case; // Free the contiguous storage space 100 q.case =NULL; // Base address set to null 101 q.front=0; // set the header pointer to 0 102 q.ear =0; If (q.column == q.ear)// if(q.column == q.ear)// if(q.column == q.ear)// if(q.column == q.ear) Cout <<" empty queue, headless element "<<endl; 111 return 0; 112 } 113 else 114 e=q.base[q.front]; // The array subscript of the queue header is front itself 115 return 1; 116}Copy the code
The running results are as follows: