#include
using namespace std;
typedef int Elemtype;
#define Maxsize 5
#define ERROR 0
#define OK 1
typedef struct
{
Elemtype data[Maxsize];
int front, rear;
}SqQueue;
void InitQueue(SqQueue& Q)
{
Q.rear = Q.front = 0;
}
bool isEmpty(SqQueue Q)
{
if (Q.rear == Q.front) return OK;
else return ERROR;
}
bool EnQueue(SqQueue& Q, Elemtype x)
{
if ((Q.rear + 1) % Maxsize == Q.front)
{
cout << "堆满啦" << endl;
return ERROR;
}
Q.data[Q.rear] = x;
Q.rear = (Q.rear + 1) % Maxsize;
return OK;
}
bool DeQueue(SqQueue& Q, Elemtype &x)
{
if (Q.rear == Q.front) return ERROR;
x = Q.data[Q.front];
Q.front = (Q.front + 1) % Maxsize;
return OK;
}
int main(void)
{
SqQueue Q;
InitQueue(Q);
EnQueue(Q, 1);
EnQueue(Q, 2);
EnQueue(Q, 3);
EnQueue(Q, 4);
for (int i = 0; i < Maxsize; i++)
printf("data[%d]=%d\n", i, Q.data[i]);
int x = 0;
EnQueue(Q, 5);
DeQueue(Q, x);
EnQueue(Q, 5);
for (int i = 0; i < Maxsize; i++)
printf("data[%d]=%d\n", i, Q.data[i]);
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
