#include<iostream>usingnamespacestd;
typedefint ElemType;
constint MAXSIZE = 10;
typedefstruct {
int front; // 队首int rear; // 队尾int length; // 队长度
ElemType elemList[MAXSIZE];
}Deque;
void initDeque(Deque *De) // 初始化队列
{
De->front = 0;
De->rear = -1;
De->length=0;
}
int isEmpty(Deque *De) // 判断队列是否为空
{
return De->length==0;
}
int isFull(Deque *De)
{
return De->length == MAXSIZE;
}
void Enqueue(Deque *De, ElemType x) // 入队列
{
if (isFull(De))
{
cout << "Have been Filled!\n";
exit(1);
}
else
{
De->elemList[++De->rear] = x;
De->length++;
}
}
ElemType Dequeue(Deque *De) // 出队列
{
if (isEmpty(De))
{
cout << "Empty!\n";
exit(1);
}
De->length--;
return De->elemList[De->front++];
}
ElemType Front(Deque *De) // 取队首元素
{
return De->elemList[De->front];
}
ElemType Rear(Deque *De) // 取队尾元素
{
return De->elemList[De->rear];
}
void print(Deque *De) // 打印队列
{
cout << "Now the Deque contents: " << endl;
for (int i = De->front;i <= De->rear;i++)
{
cout << De->elemList[i] << " ";
}
cout << endl;
}
int main()
{
const ElemType date[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Deque* myDeque = new Deque; // 初始化队列
initDeque(myDeque);
if (isEmpty(myDeque))
cout << "Empty Deque, now enqueue some values... " << endl;
for (int i = 0;i < 9;i++)
{
Enqueue(myDeque, date[i]);
}
print(myDeque);
cout << "Enqueue a number to the Deque: \n";
ElemType number;
cin >> number;
Enqueue(myDeque, number);
print(myDeque);
cout << "Dequeue twice from the Deque...\n";
Dequeue(myDeque);
Dequeue(myDeque);
print(myDeque);
cout << "The Front value of the Deque now is: " << Front(myDeque) << endl;
cout << "The Rear value of the Deque now is: " << Rear(myDeque) << endl;
system("pause");
return0;
}
操作运行结果
Empty Deque, now enqueue some values...
Now the Deque contents:
123456789
Enqueue a numbertothe Deque:
15
Now the Deque contents:
12345678915
Dequeue twice fromthe Deque...
Now the Deque contents:
345678915
The Front value ofthe Deque now is: 3
The Rear value ofthe Deque now is: 15