#include<iostream>
const int Maxsize = 20;
using namespace std;
struct QNode {
int * Data;
int head1,head2;
int Maxsize;
};
typedef QNode * Queue;
Queue CreatQ(int n)
{
Queue Q = new struct QNode;
Q->Data = new int[n];
Q->head1 = Q->head2 = 0;
Q->Maxsize = n;
return Q;
}
bool Isfull(Queue Q)
{
return(Q->head2 + 1==Maxsize);
}
bool IsEmpty(Queue Q)
{
return(Q->head1 == Q->head2);
}
void Push(int a, Queue Q,int flag)
{
if (Isfull(Q))
{
cout << "is full" << endl;
return;
}
else
{
if (flag == 0) //插在头上head1
{
for (int i = Maxsize - 2; i >= 0; i--)
{
Q->Data[i + 1] = Q->Data[i];
}
Q->Data[0] = a;
Q->head2++;
}
else //插在另一个头上head2
{
Q->Data [++Q->head2]=a;
}
}
}
int Pop(Queue Q,int flag)
{
if (IsEmpty(Q))
{
cout << "is empty" << endl;
return false;
}
else
{
if (flag == 1)
{
int a = Q->Data[0];
for (int i = 0; i < Maxsize - 1; i++)
{
Q->Data[i] = Q->Data[i + 1];
}
Q->head2--;
return a;
}
else
{
int k = Q->Data[Q->head2];
Q->head2--;
return k;
}
}
}
void main()
{
Queue Q = CreatQ(Maxsize);
Q->head1 = 0;
Q->head2 = Q->Maxsize - 6;
for (int i = 0; i < Q->Maxsize - 5; i++)
Q->Data[i] = i;
Push(1000, Q, 0);
for (int i = 0; i < Q->Maxsize - 1; i++)
cout << Q->Data[i] << " ";
cout << endl;
Push(1000, Q, 1);
for (int i = 0; i < Q->Maxsize - 1; i++)
cout << Q->Data[i] << " ";
cin.get();
}
没有用循环队列,只是一个简单的移位