队列顺序【数组】储存C语言代码+详解

队列(Quene):具有一定约束关系的线性表。 插入和删除:只能在一段插入,在另一端删除。
特点:1.先来先服务 2.先进先出(FIFO)。

顺序队列

基本结构

typedef struct Qnode *Quene;
struct Qnode{
    ElementType *Data;
    int front;//指向第一个元素的前一个位置
    int rear;
    int Maxsize;
    int size;
};

顺序队列

对于一个普通队列,当我们按照如上图所示的顺序队列进行元素储存时,会出现队尾已无法再放入元素,而队首却仍有空余位置的情形,为改变顺序队列储存元素的劣势,我们将顺序队列改成如下图所示的循环队列。

循环队列

在这里插入图片描述
对于循环队列,队列为满或者为空时,均有rear=front,所以我们需要稍微改变队列的结构,以判断队列状态。

三种改变方式:
1.在队列结构里加入size,时刻记录队列的元素个数。(操作集中代码一律为此方法)。
2.加入标记tag,一旦队列进行元素删除,则tag=1,若有元素加入tag=0。当rear=front时,若tag=1,则为空队,若tag=0,则为满队。
3.当队列中存n-1个元素时,则认为队列已满(浪费一个空间)。(文末附此操作代码)

如上图最多储存六个元素的循环队列,当rear为第六个位置(a[5])且队首有空间,下一个入队的元素应该在第一个位置a[0]。所以要用到取余操作Q->rear=(Q->rear+1)%(Q->Maxsize);来确定rear的位置。

注:此博文中rear代表队列此时最后入队一个元素的下标,front表示队列中最先入队的元素下标的前一个位置

基本结构

typedef struct Qnode *Quene;
struct Qnode{
    ElementType *Data;
    int front;//指向第一个元素的前一个位置
    int rear;
    int Maxsize;
    int size;
};

操作集:

1.Quene CreatQuene(int Maxsize);//生成长度为Maxsize的空队列
2.int IsFullQ(Quene Q,int Maxsize);//判断队列是否已满
3.int IsEmptyQ(Quene Q);//判断队列是否为空
4.void AddQ(Quene Q,ElementType item);//将数据item入队
5.ElementType DeleteQ(Queue Q );//将队头数据元素从队列中删除并返回
1.创建队列

Quene CreatQuene(int Maxsize)
{
    Quene Q;
    Q=(Quene)malloc(sizeof(Quene));
    Q->Maxsize=Maxsize;
    Q->Data=(ElementType*)malloc(sizeof(ElementType)*Maxsize);
    Q->rear=-1;
    Q->front=-1;
    Q->size=0;
}

2.判断队列是否已满

int IsFullQ(Quene Q,int Maxsize)
{
    if(Q->size==Maxsize)
      return 1;
    else
      return 0;
    
}

3.判断队列是否为空

int IsEmptyQ(Quene Q)
{
    if(Q->size==0)
     return 1;
    else
     return 0;
}


4.入队

void AddQ(Quene Q,ElementType item)
{
    if(IsFullQ)
    {
        printf("队列已满\n");
    }
    else
    {
        Q->rear=(Q->rear+1)%(Q->Maxsize);
        Q->size++;//时刻记录队列状态
        Q->Data[Q->rear]=item;
    }
}

5.出队

ElementType DeleteQ(Quene Q )
{
    if(IsEmptyQ)
    {
        printf("队列空,无法删除\n");
        return ERROR;
    }
    else
    {
        Q->front=(Q->front++)%(Q->Maxsize);//取余操作
        Q->size--;
        return Q->Data[Q->front];
    }  
}

第三种改变方式:
3.当队列中存n-1个元素时,则认为队列已满(浪费一个空间)。

代码如下

typedef int Position;
struct QNode {
    ElementType *Data;     /* 存储元素的数组 */
    Position Front, Rear;  /* 队列的头、尾指针 */
    int MaxSize;           /* 队列最大容量 */
};
typedef struct QNode *Queue;
 
Queue CreateQueue( int MaxSize )
{
    Queue Q = (Queue)malloc(sizeof(struct QNode));
    Q->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType));
    Q->Front = Q->Rear = 0;
    Q->MaxSize = MaxSize;
    return Q;
}
 
bool IsFull( Queue Q )
{
    return ((Q->Rear+1)%Q->MaxSize == Q->Front);
}
 
bool AddQ( Queue Q, ElementType X )
{
    if ( IsFull(Q) ) {
        printf("队列满");
        return false;
    }
    else {
        Q->Rear = (Q->Rear+1)%Q->MaxSize;
        Q->Data[Q->Rear] = X;
        return true;
    }
}
 
bool IsEmpty( Queue Q )
{
    return (Q->Front == Q->Rear);
}
 
ElementType DeleteQ( Queue Q )
{
    if ( IsEmpty(Q) ) { 
        printf("队列空");
        return ERROR;
    }
    else  {
        Q->Front =(Q->Front+1)%Q->MaxSize;
        return  Q->Data[Q->Front];
    }
}

猜你喜欢

转载自blog.csdn.net/qq_45955041/article/details/106968584