版权声明:本文章刊载的内容,多数为读者作者笔记,笔记内容来自于潭州教育提供的学习资源以及读者本人阅读的文章,特此声明! https://blog.csdn.net/qq_41453285/article/details/84990871
一、理论知识
待续
二、代码实现
1.队列结构体定义
typedef struct lQueue
{
int* pBase;
int size;
int length;
int front;
int rear;
}lQueue,*plQueue;
2.初始化队列
void InitlQueue(plQueue q, int size)
{
q->pBase = (int*)malloc(sizeof(int)*size);
q->size = size;
q->length = 0;
q->front = 0;
q->rear = 0;
}
3.判断队列是否已满
bool isFull(plQueue q)
{
return q->length == q->size;
}
4.判断队列是否为空
bool isEmpty(plQueue q)
{
return q->length == 0;
}
5.压队列
void PushQueue(plQueue q,int data)
{
if (!isFull(q))
{
q->pBase[q->rear++] = data;
q->length++;
if (q->rear == q->size)
q->rear = 0;
}
else
{
cout << "队列已满"<< endl;
}
}
6.出队列
void PopQueue(plQueue q)
{
if (!isEmpty(q))
{
q->front++;
q->length--;
if (q->front == q->size)
q->front = 0;
}
else
{
cout << "队列为空!"<< endl;
}
}
7.打印队列
void ShowQueue(plQueue q)
{
if (!isEmpty(q))
{
for (int i = q->front; i != q->rear; ++i)
{
cout << q->pBase[i]<<"->";
if (i == q->size)
i = -1;
}
}
else
{
cout << "顺序表为空!"<< endl;
}
}