C++循环队列基本操作

 1 #include <iostream>
 2 #define MAXSIZE 50
 3 #define QUEUEELEMENTTYPE int
 4 using namespace std;
 5 
 6 struct SeqQueue
 7 {
 8     QUEUEELEMENTTYPE element[MAXSIZE];
 9     int front;
10     int rear;
11 };
12 
13 void InitQueue(SeqQueue * Q)
14 {
15     Q->front = Q->rear = 0;
16 }
17 
18 int EnterQueue(SeqQueue * Q , QUEUEELEMENTTYPE x)
19 {
20     if((Q->rear+1)%MAXSIZE == Q->front)
21         return false;
22     Q->element[Q->rear] = x;
23     Q->rear = (Q->rear+1)%MAXSIZE;
24     return true;
25 }
26 
27 int DeleteQueue(SeqQueue * Q , QUEUEELEMENTTYPE &x)
28 {
29     if(Q->front == Q->rear)
30         return false;
31     x = Q->element[Q->front];
32     Q->front = (Q->front+1)%MAXSIZE;
33     return true;
34 }
35 
36 int IsEmpty(SeqQueue * Q)
37 {
38     if(Q->front==Q->rear)
39         return true;
40     return false;
41 }
42 
43 int IsFull(SeqQueue * Q)
44 {
45     if((Q->rear+1)%MAXSIZE == Q->front)
46         return true;
47     return false;
48 }
49 
50 void PrintQueue(SeqQueue * Q)
51 {
52     if(Q->rear-Q->front>=0)
53     {
54         for(int k=Q->front;k<Q->rear;k++)
55             cout << Q->element[k] << " ";
56     }
57     else
58     {
59         for(int i=Q->front;i<MAXSIZE;i++)
60             cout << Q->element[i] << " ";
61         for(int j=0;j<Q->rear;j++)
62             cout << Q->element[j] << " ";
63     }    
64     cout << endl;
65 }
66 
67 int main()
68 {
69     SeqQueue *Q=new SeqQueue;
70     InitQueue(Q);
71     cout << "before insert datas is empty " << IsEmpty(Q) << endl;
72     for(int i=0;i<10;i++)
73         EnterQueue(Q,i);
74     cout << "after insert datas is empty " << IsEmpty(Q) << endl;
75     cout << "after insert 10 datas isfull " << IsFull(Q) << endl;
76     PrintQueue(Q);
77     int x=-1;
78 
79     for(int j=0;j<5;j++)
80     {
81         DeleteQueue(Q,x);
82         cout << x << endl;
83     }
84     PrintQueue(Q);
85     for(int k=0;k<60;k++)
86         EnterQueue(Q,k);
87     cout << "after insert 60 datas isfull " << IsFull(Q) << endl;
88     PrintQueue(Q);
89     return 0;
90 }
原文地址:https://www.cnblogs.com/xxdfly/p/4388444.html