队列的顺序存储
#include<iostream>
using namespace std;
#define MAXSize 10
typedef int Elemtype;
typedef struct
{
Elemtype data[MAXSize];
int front, rear;
}SqQueue;
bool IsEmpty(SqQueue& Q);
void initSqQueue(SqQueue& Q);
bool CreatQueue(SqQueue& Q);
void printSqQueue(SqQueue& Q);
bool EnQueue(SqQueue& Q);
bool OutSqQueue(SqQueue& Q);
int main()
{
SqQueue Q;
initSqQueue(Q);
CreatQueue(Q);
printSqQueue(Q);
EnQueue(Q);
printSqQueue(Q);
OutSqQueue(Q);
printSqQueue(Q);
system("pause");
return 0;
}
//初始化队列
void initSqQueue(SqQueue& Q)
{
Q.front = Q.rear = 0;
}
//判断队空
bool IsEmpty(SqQueue& Q)
{
if (Q.front == Q.rear)
return true;
else
return false;
}
//创建队列
bool CreatQueue(SqQueue& Q)
{
cout << "输入入队元素个数:" << endl;
int i;
cin >> i;
cout << "依次输入入队元素:" << endl;
Elemtype e;
if (i > MAXSize - 1)
{
cout << "队列以满:" << endl;
return false;
}
while (i!=0)
{
cin >> e;
Q.data[Q.rear] = e;
Q.rear = Q.rear + 1;
i--;
}
return true;
}
//显示队列元素
void printSqQueue(SqQueue& Q)
{
int i = Q.front;
cout << "队列元素为:" << endl;
while (i!=Q.rear)
{
cout << Q.data[i] << " ";
i++;
}
cout << endl;
}
//入队
bool EnQueue(SqQueue& Q)
{
if (Q.front ==(Q.rear + 1) % MAXSize)
{
cout << "队列已满 " << endl;
return false;
}
cout << "请输入入队元素 " << endl;
Elemtype e;
cin >> e;
Q.data[Q.rear] = e;
Q.rear = (Q.rear + 1) % MAXSize;
return true;
}
//出队
bool OutSqQueue(SqQueue& Q)
{
if (Q.front == Q.rear)
{
cout << "队列已满:" << endl;
return false;
}
Elemtype e = Q.data[Q.front];
Q.front = (Q.front + 1) % MAXSize;
cout << "元素出队成功!" << endl;
return true;
}