数据结构和算法:队列

05_队列

标签(空格分隔): 数据结构和算法


  • 队列的定义
  • 队列的链式存储结构
    • 创建一个队列
    • 入队列操作
    • 出队列操作
    • 销毁一个队列
    • 运用
  • 队列的顺序存储结构
    • 循环队列定义
    • 初始化循环队列
    • 入队列操作
    • 出队列操作

5.1 队列的定义

  • 队列(queue)是只允许在一端进行插入操作,而在另一端进行删除操作的线性表
  • 与栈相反,队列是一种先进先出(First In First Out, FIFO)的线性表
  • 与栈相同的是,队列也是一种重要的线性结构,实现一个队列同样需要顺序表或链表作为基础
<–出队列 A1 A2 A3 …… An <–入队列
队头 队尾

5.2 队列的链式存储结构

  • 队列既可以用链表实现,也可以用顺序表实现
  • 跟栈相反的是,栈一般我们用顺序表实现,而队列常用链表来实现,简称为链队列
typedef struct{
    ElemType data;
    struct  QNode *next;
} QNode, *QueuePrt;
typedef struct 
{
    QueuePrt front, rear; //队头、尾指针
} LinkQueue;
  • 我们将队头指针指向链队列的头结点,而队尾指针指向终端结点。(注:头结点不是必要的,但为了方便操作,我们加上了。)
头结点 –> A1 –> A2 –> –> An ^
front rear
  • 空队列时,front和rear都指向头结点
头结点 ^
front rear

5.2.1 创建一个队列

  • 创建一个队列需要完成两个任务:
    • 一是在内存中创建一个头结点
    • 二是将队列的头指针和尾指针都指向这个生成的头结点,因为此时是空队列
InitQueue(LinkQueue *q)
{
    q->front = q->rear = (QueuePrt)malloc(sizeof(QNode));
    if( !q->front )
        exit(0);
    q->front->next = NULL;
}

5.2.2 入队列操作

  • 入队列的操作过程如下:
头结点 –> e1 e2
front rear p
头结点 –> e1 –> e2
front rear p
头结点 –> e1 –> e2
front rear
InsertQueue(LinkQueue *q, ElemType e)
{
    QueuePrt p;
    p = (QueuePrt)malloc(sizeof(QNode));
    if( p == NULL )
        exit(0);
    p->data = e;
    p->next = NULL;
    q->rear->next = p;
    q->rear = p;
}

5.2.3 出队列操作

  • 出队列操作是将队列中的第一个元素移除,队头指针不发生改变,改变头结点的next指针即可
  • 出队列的操作过程如下:
头结点 –> e1 –> e2
front rear
头结点 e1 e2
front *e = e1 rear
  • 如果原队列只有一个元素,那么应该处理一下队尾指针
头结点 –> e1
front rear
头结点 ^ e1
front rear *e = e1
DeleteQueue(LinkQueue *q, ElemType *e)
{
    QueuePrt p;
    if( q->front == q->rear )
        return;
    p = q->front->next;
    *e = p->data;
    q->front->next = p->next;
    if( q->rear == p )
        q->rear = q->front;
    free(p);
}

5.2.4 销毁一个队列

  • 由于链队列建立在内存的动态区,因此当一个队列不再有用时应当把它及时销毁掉,以免过多地占用内存空间。
DestroyQueue(LinkQueue *q)
{
    while( q->front )
    {
        q->rear = q->front->next;
        free( q->front );
        q->front = q->rear;
    }
}

5.2.5 运用

  • 编写一个链队列,任意输入一串字符,以 # 作为结束标志,然后将队列中的元素打印出来。
#include <stdio.h>
#include <stdlib.h>


typedef char ElemType;

typedef struct{
    ElemType data;
    struct  QNode *next;
} QNode, *QueuePrt;

typedef struct 
{
    QueuePrt front, rear; //队头、尾指针
} LinkQueue;

void InitQueue(LinkQueue *q)
{
    q->front = q->rear = (QueuePrt)malloc(sizeof(QNode));
    if( !q->front )
        exit(0);
    q->front->next = NULL;
}

void InsertQueue(LinkQueue *q, ElemType e)
{
    QueuePrt p;
    p = (QueuePrt)malloc(sizeof(QNode));
    if( p == NULL )
        exit(0);
    p->data = e;
    p->next = NULL;
    q->rear->next = p;
    q->rear = p;
}

void DeleteQueue(LinkQueue *q, ElemType *e)
{
    QueuePrt p;
    if( q->front == q->rear )
        return;
    p = q->front->next;
    *e = p->data;
    q->front->next = p->next;
    if( q->rear == p )
        q->rear = q->front;
    free(p);
}

void DestroyQueue(LinkQueue *q)
{
    while( q->front )
    {
        q->rear = q->front->next;
        free( q->front );
        q->front = q->rear;
    }
}

int main()
{
    LinkQueue q;
    ElemType c;

    InitQueue(&q);

    printf("请输入字符并以#作为结束标志: \n");
    scanf("%c", &c);

    while( c != '#' )
    {
        InsertQueue(&q, c);
        scanf("%c", &c);
    }
    printf("输出的字符串为: \n");  
    while( q.front != q.rear )
    {
        DeleteQueue(&q, &c);
        printf("%c", c);
    }
    printf("\n");

    return 0;
}

5.3 队列的顺序存储结构

  • 假设一个队列有n个元素,则顺序存储队列需建立一个大于n的存储单元,并把队列的所有元素存储在数组的前n个单元,数组下标为0的一端则是队头
  • 入队列操作其实就是在队尾追加一个元素,不需要任何移动,时间复杂度为O(1)
  • 出队列则不同,因为已经假设下标为0的位置是队列的队头,因此每次出队列操作所有元素都要向前移动,时间复杂度为O(n)
  • 如果不限制队头一定要在下标为0的位置,那么出队列的操作就不需要移动全体元素
  • 但是这样也会出现一些问题,会出现数组越界的错误,即假溢出

5.3.1 循环队列定义

  • 要解决假溢出的办法就是如果后面满了,就再从头开始,也就是
    头尾相接的循环
  • 循环队列的容量是固定的,并且它的队头和队尾指针都可以随着元素入出队列而发生改变,这样循环队列逻辑上就好像是一个环形存储空间
  • 但要注意的是,在实际的内存当中,不可能有真的环形存储区,我们只是用顺序表模拟出来的逻辑上的循环
  • 循环队列的实现只需要灵活改变front和rear指针即可
  • 也就是让front和rear指针不断加1,即使超出了地址范围,也会自动从头开始。可以采取取模运算处理:
    • (rear+1) % QueueSize
    • (front+1) % QueueSize
#define MAXSIZE 100

typedef struct 
{
    ElemType *base;//用于存放分配基地址
    int front;
    int rear;
} cycleQueue;

5.3.2 初始化循环队列

InitQueue(cycleQueue *q)
{
    q->base = (ElemType *)malloc(MAXSIZE * sizeof(ElemType));
    if( !q->base )
        exit(0);
    q->front = q->rear = 0;
}

5.3.3 入队列操作

InsertQueue(cycleQueue *q, ElemType e)
{
    if( (q->rear+1)%MAXSIZE == q->front )
        return; //队列已满
    q->base[q->rear] = e;
    q->rear = (q->rear+1) % MAXSIZE;
}

5.3.4 出队列操作

DeleteQueue(cycleQueue *q, ElemType *e)
{
    if( q->front == q->rear )
        return; //队列为空
    *e = q->base[q->front];
    q->front = (q->front+1) % MAXSIZE;
}

猜你喜欢

转载自blog.csdn.net/weixin_42061048/article/details/80808978