The 4th chapter of the practical data structure, question 6.1
Algorithm design problem. Requirements: Set a loop queue, only the head pointer front, no tail pointer, and another set of elements containing the number of loggers count,//try to write the corresponding team and out of the algorithm #include <iostream># include <stdlib.h> #include <time.h>using namespace std; #define maxlen 10#define Status int#define datatype int #define ok 1#define y 1#define N 0typedef struct{ datatype s[maxlen]; int front; int count;} seqqueue;//Initialize Status initseqqueue (seqqueue &sq) { sq.front=-1; //team first pointer to the previous position of the first element sq.count=0; //count to 0 Return ok;} Team Full Status isfull (seqqueue &sq) {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSp;if (Sq.count==maxlen) return y; //If the team is full, return to 1 else return N; //otherwise return 0}//team empty status isempty (SEQQUEUE&NBSP;&SQ) { if (sq.count==0) return Y; //If the team is empty, return to 0 else Return n;} Queue Status inqueue (seqqueue &sq,datatype x) { if (isfull (SQ)) return 0; //team full srand (Time (NULL)); int a= Rand ()%10; sq.s[sq.count]=x; sQ.count++; return ok;} Team Status outqueue (SEQQUEUE&NBSP;&SQ,DATATYPE&NBSP;&X) { if (IsEmpty (SQ)) return 0; //Team Air sq.front++; x=sq.s[sq.front ]; sq.count--; //queue Element reduction of 1 return 1;} Display queue element Status showqueue (seqqueue sq) { if (IsEmpty (SQ) ) return 0; //If the team is empty, return to 0 while (sq.count!=0) { cout<<sq.s[sq.front+1]<< " "; sq.front++; sq.count--; } cout<<endl;} Read team first element Status readqueue (seqqueue sq,datatype &x) { if (IsEmpty (SQ)) return 0; //If the team is empty, return to 0 x=sq.s[sq.front+1]; return 1;} Int main () { seqqueue sq; initseqqueue (SQ); cout<< "into the stack" < <endl; inqueue (sq,1); inqueue (SQ,2); inqueue (sq,3); InQueue (sq,4); inqueue (sq,5); cout<< "Show queue elements:" <<endl; showqueue ( SQ); cout<< "out team:" <<endl; datatype temp; outqueue (SQ,temp); cout<< "Show queue elements:" <<endl; showqueue (SQ); cout<< "show team first element:" <<endl; readqueue (sq,temp); cout<<temp≪<endl; return ok;} [[email protected] queue]$ ./app stack Display queue element:1 2 3 4 5 : Show queue elements: 2 3 4 5 Display Team first element: 2[[email protected] queue]$
This article is from "My Me" blog, please be sure to keep this source http://237085.blog.51cto.com/227085/1765144
Data structure-Queue exercises