Data structure-Queue exercises

Source: Internet
Author: User

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;&AMP;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;&AMP;SQ,DATATYPE&NBSP;&AMP;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&Lt;<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

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.