数据结构与算法分析c语言描述(Mark Allen)--循环队列ADT数组实现

版权声明:请注明转载地址 https://blog.csdn.net/OCEANtroye https://blog.csdn.net/OCEANtroye/article/details/83280866

循环队列ADT数组实现


  • 使用数组存储
  • 操作集合
    • 入队
    • 出队
    • 清空
    • 初始化
    • 返回队前元素
    • 打印

重点注意!
对于一个循环队列
front == rear时候队列可能是空可能是满的
解决方案:
1.使用一个额外变量标记Size表示当前的元素个数或者
Tag(删除是0 插入是1,当rear==front时候,判断最后一次操作是1还是0即可判断是空还是满)
2.只使用n-1个数组空间

这里使用第二种实现方案


源文件

#include <stdio.h>
#include <stdlib.h>
typedef int ElementType;

//最大的存储元素个数
const int MaxSize = 16;

//重点注意!
//对于一个循环队列
//front == rear时候队列可能是空可能是满的
//解决方案:
//1.使用一个额外变量标记Size表示当前的元素个数或者
//Tag(删除是0 插入是1,当rear==front时候,判断最后一次操作是1还是0即可判断是空还是满)
//2.只使用n-1个数组空间

//这里使用第二种实现方案
//结构体
//数组实现 循环队列
struct QNode
{
    ElementType *Data;
    int rear;
    int front;
    int MaxSize;
};

typedef struct QNode *Queue;

#ifndef _Queue_H

//打印
void PrintQ(Queue Q);

//建一个空的队
Queue CreatQueue(int MaxSize);

//判断队列Q是否已经满了
int IsFullQ(Queue Q, int MaxSize);

//将数据元素item插入到队列中
void AddQ(Queue Q, ElementType X);

//判断队列是否是空的
int IsEmptyQ(Queue Q);

//删除队头元素并且返回
ElementType DeleteQ(Queue Q);

#endif // !_Queue_H

int main(int argc, char const *argv[])
{
    Queue Q = nullptr;
    int choose;
    printf("\t\t\tplease intput a int to do something.\n");
    printf("\t\t\t1.push\n");
    printf("\t\t\t2.pop.\n");
    printf("\t\t\t3.isfull?\n");
    printf("\t\t\t4.isempty?\n");
    printf("\t\t\t5.show.\n");
    Q = CreatQueue(MaxSize);
    int num;
    while (~scanf("%d", &choose))
    {
        if (choose == 0)
        {
            break;
        }

        switch (choose)
        {
        case 1:
            printf("\t\t\tinput a int.\n");
            scanf("%d", &num);
            AddQ(Q, num);
            PrintQ(Q);
            break;

        case 2:
            printf("the front element is %d\n", DeleteQ(Q));
            PrintQ(Q);
            break;

        case 3:
            if (IsFullQ(Q,Q->MaxSize))
                printf("full!\n");
            else
                printf("not yet.\n");
            break;

        case 4:
            if(IsEmptyQ(Q))
            {
                printf("it is empty.\n");
            }
            else
            {
                printf("no yet.\n");
            }
            break;

        case 5:
            PrintQ(Q);
            break;
        }
    }
    system("pause");
    return 0;
}

//建一个空的队
Queue CreatQueue(int MaxSize)
{
    Queue Q = (Queue)malloc(sizeof(struct QNode));
    Q->Data = (ElementType *)malloc(sizeof(ElementType) * MaxSize);
    Q->rear = Q->front = 0;
    Q->MaxSize = MaxSize;
    return Q;
}

//判断队列Q是否已经满了
int IsFullQ(Queue Q, int MaxSize)
{
    return ((Q->rear + 1) % Q->MaxSize == Q->front);
}

//将数据元素item插入到队列中
void AddQ(Queue Q, ElementType X)
{
    if ((Q->rear + 1) % Q->MaxSize == Q->front)
    {
        printf("full queue.\n");
        return;
    }
    //插入后rear向后挪动一个位置
    Q->rear = (Q->rear + 1) % Q->MaxSize;
    Q->Data[Q->rear] = X;
}

//判断队列是否是空的
int IsEmptyQ(Queue Q)
{
    return Q->rear == Q->front;
}

//删除队头元素并且返回
ElementType DeleteQ(Queue Q)
{
    if (IsEmptyQ(Q))
    {
        printf("queue is empty.\n");
        return -1;
    }
    else
    {
        Q->front = (Q->front + 1) % Q->MaxSize;
        return Q->Data[Q->front];
    }
}

//打印
void PrintQ(Queue Q)
{
    if (Q->rear < Q->front)
    {
        for (int i = Q->front; i <= Q->MaxSize - 1; i++)
        {
            printf("%d-", Q->Data[i]);
        }
        for (int i = 0; i <= Q->rear; i++)
        {
            printf("%d-", Q->Data[i]);
        }
    }
    else
    {
        for (int i = Q->front + 1; i <= Q->rear; i++)
        {
            printf("%d-", Q->Data[i]);
        }
    }
    putchar('\n');
}

猜你喜欢

转载自blog.csdn.net/OCEANtroye/article/details/83280866