数据结构---队列(顺序表实现)

版权声明:本文章刊载的内容,多数为读者作者笔记,笔记内容来自于潭州教育提供的学习资源以及读者本人阅读的文章,特此声明! https://blog.csdn.net/qq_41453285/article/details/84991791

一、理论知识

待续

二、代码实现

1.队列结构体定义

typedef int ElemType;
typedef struct Queue
{
    ElemType* pBase;
    int size;
    ElemType front;
    ElemType rear;
    int length;
}*pQueue;

2.初始化队列

void initQueue(pQueue qu,ElemType length)
{
    qu->size = 0;
    qu->length = length;
    qu->pBase = new ElemType[qu->length];
    qu->front = -1;
    qu->rear = -1;
}

3.判断队列是否为空

bool isEmpty(pQueue qu)
{
    return ((qu->front == -1) && (qu->rear == -1));
}

4.判断队列是否已满

bool isFully(pQueue qu)
{
    return (qu->rear==qu->length-1);
}

5.压队列

void pushQueue(pQueue qu, ElemType data)
{
    if (isFully(qu))
        return;
    qu->pBase[qu->rear+1] = data;
    qu->rear++;
    qu->size++;
}

6.出队列

ElemType popQueue(pQueue qu)
{
    if (isEmpty(qu))
        return -1;
    ElemType popData = qu->pBase[qu->front + 1];
    for (int i = 0; i < qu->size; ++i)
        qu->pBase[i] = qu->pBase[i + 1];
    qu->size--;
    qu->rear--;
    return popData;
}

7.打印队列

void showQueue(pQueue qu)
{
    if (isEmpty(qu))
    {
        printf("栈为空,无法打印!\n");
    return;
    }
    for (int i = 0; i < qu->size; ++i)
        printf("%d->", qu->pBase[i]);
}

8.代码演示

猜你喜欢

转载自blog.csdn.net/qq_41453285/article/details/84991791
今日推荐