顺序队列
#include <iostream>
#include <fstream>
using namespace std;
#define MAXQSIZE 100
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef char QElemType;
typedef char SElemType;
typedef int Status;
typedef struct {
QElemType *base;
int front;
int rear;
} SqQueue;
Status InitQueue(SqQueue &Q) {
Q.base = new QElemType[MAXQSIZE];
if (!Q.base)
exit(OVERFLOW);
Q.front = Q.rear = 0;
return OK;
}
int QueueLength(SqQueue Q) {
return (Q.rear - Q.front + MAXQSIZE) % MAXQSIZE;
}
Status EnQueue(SqQueue &Q, QElemType e) {
if ((Q.rear + 1) % MAXQSIZE == Q.front)
return ERROR;
Q.base[Q.rear] = e;
Q.rear = (Q.rear + 1) % MAXQSIZE;
return OK;
}
Status DeQueue(SqQueue &Q, QElemType &e) {
if (Q.front == Q.rear)
return ERROR;
e = Q.base[Q.front];
Q.front = (Q.front + 1) % MAXQSIZE;
return OK;
}
SElemType GetHead(SqQueue Q) {
if (Q.front != Q.rear)
return Q.base[Q.front];
}
int main() {
int choose, flag = 0;
SqQueue Q;
QElemType e, j;
cout << "1.初始化\n";
cout << "2.入队\n";
cout << "3.读队头元素\n";
cout << "4.出队\n";
cout << "0.退出\n\n";
choose = -1;
while (choose != 0) {
cout << "请选择:";
cin >> choose;
switch (choose) {
case 1:
if (InitQueue(Q)) {
flag = 1;
cout << "成功对队列进行初始化\n\n";
} else
cout << "初始化队列失败\n\n";
break;
case 2: {
if (flag == 1) {
cout << "请输入入队元素:\n";
cin >> e;
EnQueue(Q, e);
cout << "元素:" << e << " 入队成功!" << endl;
} else
cout << "队列未建立,请重新选择\n\n";
}
break;
case 3:
if (Q.front != Q.rear)
cout << "队头元素为:\n" << GetHead(Q) << endl << endl;
else
cout << "队列中无元素,请重新选择\n" << endl;
break;
case 4:
cout << "依次弹出的队头元素为:\n";
while (Q.front != Q.rear) {
DeQueue(Q, e);
cout << e << " ";
}
cout << endl << endl;
break;
}
}
return 0;
}
链式队列
#include<iostream>
#include<fstream>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef char QElemType;
typedef int Status;
typedef char SElemType;
typedef struct QNode {
QElemType data;
struct QNode *next;
} QNode, *QueuePtr;
typedef struct {
QueuePtr front;
QueuePtr rear;
} LinkQueue;
Status InitQueue(LinkQueue &Q) {
Q.front = Q.rear = new QNode;
Q.front->next = NULL;
return OK;
}
Status EnQueue(LinkQueue &Q, QElemType e) {
QueuePtr p;
p = new QNode;
p->data = e;
p->next = NULL;
Q.rear->next = p;
Q.rear = p;
return OK;
}
Status DeQueue(LinkQueue &Q, QElemType &e) {
QueuePtr p;
if (Q.front == Q.rear)
return ERROR;
p = Q.front->next;
e = p->data;
Q.front->next = p->next;
if (Q.rear == p)
Q.rear = Q.front;
delete p;
return OK;
}
SElemType GetHead(LinkQueue Q) {
if (Q.front != Q.rear)
return Q.front->next->data;
}
int main() {
int choose, flag = 0;
LinkQueue Q;
QElemType e, j;
cout << "1.初始化\n";
cout << "2.入队\n";
cout << "3.读队头元素\n";
cout << "4.出队\n";
cout << "0.退出\n\n";
choose = -1;
while (choose != 0) {
cout << "请选择:";
cin >> choose;
switch (choose) {
case 1:
if (InitQueue(Q)) {
flag = 1;
cout << "成功对队列进行初始化\n\n";
} else
cout << "初始化队列失败\n\n";
break;
case 2: {
if (flag == 1) {
cout << "请输入入队元素:\n";
cin >> e;
EnQueue(Q, e);
cout << "元素:" << e << " 入队成功!" << endl;
} else
cout << "队列未建立,请重新选择\n\n";
}
break;
case 3:
if (Q.front != Q.rear)
cout << "队头元素为:\n" << GetHead(Q) << endl << endl;
else
cout << "队列中无元素,请重新选择\n" << endl;
break;
case 4:
cout << "依次弹出的队头元素为:\n";
while (Q.front != Q.rear) {
DeQueue(Q, e);
cout << e << " ";
}
cout << endl << endl;
break;
}
}
return 0;
}