[C++] 队列基本操作(队列初始化、清空队列、入队、出队等) →→→→→进入此内容的聊天室

来自 , 2021-04-11, 写在 C++, 查看 137 次.
URL http://www.code666.cn/view/f4a4da9a
  1. #include<iostream.h>
  2. #include<stdlib.h>
  3. const int maxsize=50;
  4. typedef int elemtype;
  5. struct queue
  6. {
  7.         elemtype queue[maxsize];
  8.         int front,rear;
  9. };
  10.  
  11. void initqueue ( queue &q )
  12. {
  13.         q.front=q.rear=0;
  14. }
  15.  
  16. void clearqueue ( queue &q )
  17. {
  18.         q.front=q.rear=0;
  19. }
  20. int queueempty ( queue &q )
  21. {
  22.         return q.front==q.rear;
  23. }
  24.  
  25. elemtype qfront ( queue &q )
  26. {
  27.         if ( q.front==q.rear )
  28.         {
  29.                 cerr<<"queue is empty!"<<endl;
  30.                 exit ( 1 );
  31.         }
  32.         return q.queue[ ( q.front+1 ) %maxsize];
  33. }
  34.  
  35. void qinsert ( queue &q,elemtype x )
  36. {
  37.         int k= ( q.rear+1 ) %maxsize;
  38.         if ( k==q.front )
  39.         {
  40.                 cerr<<"queue overflowe!"<<endl;
  41.                 exit ( 1 );
  42.         }
  43.         q.rear=k;
  44.         q.queue[k]=x;
  45. }
  46.  
  47. elemtype qdelete ( queue &q )
  48. {
  49.         if ( q.front==q.rear )
  50.         {
  51.                 cerr<<"queue is empty!"<<endl;
  52.                 exit ( 1 );
  53.         }
  54.         q.front= ( q.front+1 ) %maxsize;
  55.         return q.queue[q.front];
  56. }
  57.  
  58. int queuefull ( queue &q )
  59. {
  60.         return ( q.rear+1 ) %maxsize==q.front;
  61. }
  62.  
  63. int queuesize ( queue &q )
  64. {
  65.         return ( q.rear-q.front ) %maxsize;
  66. }
  67.  
  68. void lookqueue ( queue &q )
  69. {
  70.         if ( q.front==q.rear )
  71.         {
  72.                 cerr<<"queue is empty!"<<endl;
  73.                 exit ( 1 );
  74.         }
  75.         int k= ( q.front+1 ) %maxsize;
  76.         while ( 1 )
  77.         {
  78.                 cout<<q.queue[k]<<"  ";
  79.                 if ( k==q.rear )
  80.                         break;
  81.                 k= ( k+1 ) %maxsize;
  82.         }
  83.         cout<<endl;
  84. }
  85.  
  86. void main()
  87. {
  88.         queue q;
  89.         initqueue ( q );
  90.         for ( int i=0; i<6; i++ )
  91.         {
  92.                 int x=rand() %100;
  93.                 int y=rand() %100;
  94.                 if ( !queuefull ( q ) )
  95.                 {
  96.                         qinsert ( q,x );
  97.                         cout<<x<<"  in queue,  " ;
  98.                 }
  99.                 if ( !queuefull ( q ) )
  100.                 {
  101.                         qinsert ( q,y );
  102.                         cout<<y<<"  in queue" ;
  103.                 }
  104.                 cout<<endl;
  105.                 cout<<"queuesize is:  "<<queuesize ( q ) <<endl;
  106.                 cout<<qdelete ( q ) <<"  out queue"<<endl;
  107.                 cout<<"queuesize is:  "
  108.                     <<queuesize ( q ) <<endl;
  109.         }
  110.         cout<<endl;
  111.         lookqueue ( q );
  112.         while ( !queueempty ( q ) )
  113.         {
  114.                 cout<<qdelete ( q ) <<endl;
  115.                 cout<<"queuesize is:  "
  116.                     <<queuesize ( q ) <<endl;
  117.         }
  118. }
  119.  

回复 "队列基本操作(队列初始化、清空队列、入队、出队等)"

这儿你可以回复上面这条便签

captcha