C言語_.データ構造_キュー(巡回キュー)
1949 ワード
#include
#include
using namespace std;
typedef int Status;
#define QueueElemType int
#define MAX_QUEUE_SIZE 10
#define QUEUE_INCREMNET 5
typedef struct { //
QueueElemType *base;
int front; //
int rear; //
}SqQueue;
Status InitQueue(SqQueue &Q) {
Q.base = (QueueElemType*) new QueueElemType[MAX_QUEUE_SIZE];
if (Q.base == NULL) {
cout << "
";
return 0;
}
Q.front = Q.rear = 0;
return 1;
}
Status QueueEmpty(SqQueue &Q) {
if (Q.front == Q.rear)
return 1;
else
return 0;
}
int QueueLength(SqQueue &Q) {
return (Q.rear - Q.front + MAX_QUEUE_SIZE) % MAX_QUEUE_SIZE;
}
Status EnQueue(SqQueue &Q, QueueElemType &e) {
if ((Q.rear + 1) % MAX_QUEUE_SIZE == Q.front) {
cout << "
";
return 0;
}
Q.base[Q.rear] = e;
Q.rear = (Q.rear + 1) % MAX_QUEUE_SIZE;
return 1;
}
Status GetHead(SqQueue &Q, QueueElemType &e) {
if (Q.front == Q.rear) {
cout << "
";
return 0;
}
e = Q.base[Q.front];
return 1;
}
Status DeQueue(SqQueue &Q, QueueElemType &e) {
if (Q.front == Q.rear) {
cout << " ";
return 0;
}
e = Q.base[Q.front];
Q.front = (Q.front + 1) % MAX_QUEUE_SIZE;
return 1;
}
Status DestroyQueue(SqQueue &Q) {
delete[]Q.base;
return 1;
}
int queuemain() {
SqQueue qu;
InitQueue(qu);
if (QueueEmpty(qu))
cout << "kong
";
for (int i = 0; i != 7; i++) {
int tmp=i;
EnQueue(qu, tmp);
}
for (int i = 0; i != 2; i++) {
int tmp;
DeQueue(qu, tmp);
cout << tmp << "______";
cout << endl;
}
for (int i = 0; i != 7; i++) {
int tmp = i;
EnQueue(qu, tmp);
}
cout << QueueLength(qu) << endl;
for (int i = 0; i != 10; i++) {
int tmp;
DeQueue(qu, tmp);
cout << tmp << ' ';
}
cout << endl;
return 1;
}