用链表实现队列操作,代码如下:

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <malloc.h> using namespace std; //函数状态码定义
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2 typedef int Status;
typedef int QElemType; 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 = (QueuePtr)malloc(sizeof(QNode));
if (!Q.front) exit(OVERFLOW);
Q.front->next = NULL;
return OK;
} Status DestroyQueue(LinkQueue &Q) {
while (Q.front) {
cout << Q.front->data << endl;
Q.rear = Q.front->next;
free(Q.front);
Q.front = Q.rear;
}
return OK;
} Status EnQueue(LinkQueue &Q, QElemType e) {
Q.rear->next = (QueuePtr)malloc(sizeof(QNode));
if (!Q.rear->next) exit(OVERFLOW);
Q.rear->data = e;
Q.rear = Q.rear->next;
Q.rear->next = NULL;
return OK;
} Status DeQueue(LinkQueue &Q, QElemType &e) {
if (Q.front == Q.rear)
return ERROR;
e = Q.front->data;
QueuePtr p = Q.front;
Q.front = Q.front->next;
free(p);
return OK;
} bool EmptyQueue(LinkQueue &Q) {
return Q.front == Q.rear;
} int main()
{
LinkQueue A, B;
InitQueue(A);
InitQueue(B);
int n, m;
cin >> n;
for (int i = ; i < n; ++i) {
cin >> m;
if (m & )
EnQueue(A, m);
else
EnQueue(B, m);
}
bool ok = false;
while (!EmptyQueue(A) && !EmptyQueue(B)) {
DeQueue(A, m);
if (ok)
cout << ' ' << m;
else
cout << m; if (!EmptyQueue(A)) {
DeQueue(A, m);
cout << ' ' << m;
}
DeQueue(B, m);
cout << ' ' << m;
ok = true;
}
while (!EmptyQueue(A)) {
DeQueue(A, m);
if (ok)
cout << ' ' << m;
else
cout << m;
ok = true;
}
while (!EmptyQueue(B)) {
DeQueue(B, m);
if (ok)
cout << ' ' << m;
else
cout << m;
ok = true;
}
return ;
}
05-28 11:49