您的位置:首页 > 其它

004 队列的顺序存储

2017-07-20 14:09 274 查看

队列

队列先进先出,任意一本书上都有详细的介绍,这里不废话了,直接上代码。

循环队列-顺序存储方式

#include<iostream>
using namespace std;
#define MaxSize 20
typedef int elemtype;

typedef struct
{
elemtype data[MaxSize];
int front, rear;
}SqQueue;

void InitQueue(SqQueue &q)
{
q.front = q.rear = 0;
}

bool EmptyQueue(SqQueue &q)
{
if (q.front == q.rear)
{
cout << "The Queue is empty" << endl;
return true;
}
else
{
cout << "Not empty !!" << endl;
return false;
}
}

bool EnQueue(SqQueue &q, int x)
{
if ((q.rear + 1) % MaxSize == q.front)
{
cout << "The queue is full !!" << endl;
return false;
}
q.data[q.rear] = x;
q.rear = (q.rear + 1) % MaxSize;
cout << "Adding to the queue successful !" << endl;
return true;
}

bool OutQueue(SqQueue &q)
{
if (q.front == q.rear) return false;   //队列是空的,返回错误
elemtype x;
x = q.data[q.front];
q.front = (q.front + 1) % MaxSize;
cout << "Outing from the queue successful !" << x << endl;
return true;
}

int main()
{
SqQueue q;
InitQueue(q);
EmptyQueue(q);
EnQueue(q, 22);
EmptyQueue(q);
OutQueue(q);
EmptyQueue(q);
system("pause");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: