一、队列的基本概念:
1、队列(queue):是一个特殊的有序表,其插入操作都在表的一端进行,而删除操作都在表的另一端进行。
例如:Q=(a0, a1, a2, ……,an)
2、队首元素(rear):队列中要删除的一端的第一个元素,即上面的a0。
3、队尾元素(front):队列中要插入的一端的第一个元素,即上面的an。
4、入队(enqueue):将一个元素从队尾元素端插入该队列中,使其成为该队列的队尾元素。
5、出队(dequeue):将一个元素从队首元素端删除。
二、队列的特点:
1、 先进先出(FIFO)
2、插入和删除分别在不同的端。
三、队列的基本组成:
1、队首(front)
2、队尾(rear)
3、元素的个数(size)
4、数据(data)
5、容量(capacity)
四、基本操作及代码实现:
实现方式:C语言和数组的方式
1、建立一个空的队列:根据队列的特点,创建队列的时候注意对队列基本组成的初始化。
front:始终指向队首,初始化为-1;
rear:始终指向队尾,初始化为-1;
size:表明队列元素的个数,初始化为0;
//QueueCreate.cpp
#include "queue.h"
QUEUE * QueueCreate( int max_capacity)//创建一个容量为max_capacity的队列
{
if (max_capacity > QueueMaxSize)
{
printf("Error,there is no enough space!\n");
return FALSE;
}
if (max_capacity < QueueMinSize)
{
printf("Error, the capacity of queue is too small!\n");
return FALSE;
}
QUEUE * queue = (QUEUE *)calloc(1, sizeof(QUEUE *));
if (queue == NULL)
{
printf("Error, there is no address to back for queue!\n");
return FALSE;
}
queue->Capacity = max_capacity;
queue->rear = -1;
queue->front = -1;
queue->size = 0;
queue->data = (int*)calloc(max_capacity, sizeof(int*));
if (queue->data == NULL )
{
printf("Error, there is no address to back for the data of queue!\n");
return FALSE;
}
return queue;
}
2、判断是否空队列:因为front指向队首,rear指向队尾,如果是空队列,他们必定相等。
//QueueIsEmpty.cpp
#include "queue.h"
bool QueueIsEmpty(QUEUE * queue)
{
if (queue->front == queue->rear)
{
printf("The queue is empty!\n");
return TRUE;
}
else
{
return FALSE;
}
}
3、判断队列是否满:如果指向队尾的rear大小和capacity-1相等,则队列满了。
//QueueIsFull.cpp
#include "queue.h"
bool QueueIsFull(QUEUE * queue)
{
if (queue->rear == (queue->Capacity - 1) )
{
printf("The queue is full!\n");
return TRUE;
}
else
{
return FALSE;
}
}
4、数据入列:数据只能从队尾加入,所以只能改变rear的值和size的值。
//QueueEnqueue.cpp
#include "queue.h"
void QueueEnqueue(QUEUE * queue, int element)//入列
{
if (QueueIsFull(queue))
{
return ;
}
queue->data[++(queue->rear)] = element;
(queue->size)++;
if (queue->front < 0)
{
(queue->front)++;
}
}
5、数据出列:数据只能从队首出列,注意数据出列后front的值是增加的,size是减小的。
//QueueDequeue.cpp
#include "queue.h"
void QueueDequeue(QUEUE * queue)//出列
{
if (QueueIsEmpty(queue))
{
return ;
}
queue->data[queue->front] = 0;
(queue->front)++;
(queue->size)--;
}
五、附件:
1、头文件:queue.h
// queue.h
#ifndef __QUEUE__H
#define __QUEUE__H
#include <iostream>
using namespace std;
/************************************************************************/
#define TRUE 1
#define FALSE 0
#define QueueMaxSize 1000
#define QueueMinSize 1
typedef struct
{
int Capacity;
int front;
int rear;
int size;
int * data;
}QUEUE;
/************************************************************************/
bool QueueIsEmpty(QUEUE * queue);
bool QueueIsFull(QUEUE * queue);
QUEUE * QueueCreate( int max_capacity);//创建一个容量为max_capacity的队列
void QueueEnqueue(QUEUE * queue, int element);//入列
void QueueDequeue(QUEUE * queue);//出列
void QueueShow( QUEUE * queue );
#endif
2、主函数:main.cpp
//main.cpp
#include "queue.h"
int data[10]={1, 2, 4, 5, 3, 7, 9, 6, 8, 0};
int main()
{
int i;
int queueMaxSize = 10;
QUEUE * queue = QueueCreate(queueMaxSize);
for ( i = 0; i < queueMaxSize; i++)
{
QueueEnqueue(queue, data[i]);
}
cout<<"队列中原始数据:"<<endl;
QueueShow(queue);
cout<<"出列后的数据:"<<endl;
QueueDequeue(queue);
QueueShow(queue);
return 0;
}
3、显示函数:QueueShow.cpp
//QueueShow.cpp
#include "queue.h"
void QueueShow( QUEUE * queue )
{
if (QueueIsEmpty(queue))
{
return ;
}
int i;
for ( i = queue->front; i <= queue->rear; i++)
{
cout<<queue->data[i]<<" ";
}
cout<<endl;
}
六、应用领域: