美文网首页
数据结构与算法 — 队列

数据结构与算法 — 队列

作者: SK_Wang | 来源:发表于2020-04-13 22:36 被阅读0次

队列是一种特殊的线性表,只能在头尾两端进行操作。

  • 队尾(rear):只能从队尾添加元素,一般叫做enQueue,入队
  • 对头(front):只能从队头移除元素,一般叫做deQueue, 出队
  • 先进先出的原则,First in first out, FIFO

队列的顺序存储

结构设计

typedef struct {
    QElemType data[QUEUE_INIT_SIZE];
    int front;
    int rear;
}SqQueue;

队列的创建

Status InitQueue(SqQueue *Q) {
    Q->front = 0;
    Q->rear = 0;
    return OK;
}

获取对头元素

Status GetHead(SqQueue Q, QElemType *e) {
    if (Q.front == Q.rear) {
        return ERROR;
    }
    *e = Q.data[Q.front];
    return OK;
}

入队

Status EnQueue(SqQueue *Q, QElemType e) {
    if ((Q->rear + 1) % QUEUE_INIT_SIZE == Q->front) {
        return ERROR;
    }
    
    Q->data[Q->rear] = e;
    Q->rear = (Q->rear + 1) % QUEUE_INIT_SIZE;
    return OK;
}

出队

Status DeQueue(SqQueue *Q, QElemType *e) {
    if (Q->front == Q->rear) {
        return ERROR;
    }
    
    *e = Q->data[Q->front];
    Q->front = (Q->front + 1) % QUEUE_INIT_SIZE;
    return OK;
}

队列的清空

Status ClearQueue(SqQueue *Q) {
    Q->front = 0;
    Q->rear = 0;
    return OK;
}

队列判断是否为空

Status QueueEmpty(SqQueue Q) {
    return Q.front == Q.rear;
}

队列的长度

int QueueLength(SqQueue Q) {
    return (Q.rear - Q.front + QUEUE_INIT_SIZE) % QUEUE_INIT_SIZE;
}

队列的链式存储

结构设计

typedef struct QueueNode {
    QElemType data;
    struct QueueNode *next;
}QueueNode, *QueueNodePtr;

typedef struct {
    QueueNodePtr front;
    QueueNodePtr rear;
}LinkQueue;

队列的创建

Status InitQueue(LinkQueue *Q) {
    Q->rear = Q->front = (QueueNodePtr)malloc(sizeof(QueueNode));
    if (Q->front == NULL) {
        return ERROR;
    }
    
    Q->front->next = NULL;
    Q->rear->next = NULL;
    return OK;
}

获取对头元素

Status GetHead(LinkQueue Q, QElemType *e) {
    if (Q.front == Q.rear) {
        return ERROR;
    }
    
    *e = Q.front->next->data;
    return OK;
}

入队

Status EnQueue(LinkQueue *Q, QElemType e) {
    QueueNodePtr temp = (QueueNodePtr)malloc(sizeof(QueueNode));
    if (temp == NULL) {
        return ERROR;
    }
    temp->data = e;
    temp->next = NULL;
    Q->rear->next = temp;
    Q->rear = temp;
    return OK;
}

出队

Status DeQueue(LinkQueue *Q, QElemType *e) {
    if (Q->front == Q->rear) {
        return ERROR;
    }
    
    QueueNodePtr p;
    p = Q->front->next;
    *e = p->data;
    Q->front->next = p->next;
    if (Q->rear == p) {
        Q->rear = Q->front;
    }
    
    free(p);
    return OK;
}

队列的销毁

Status DestoryQueue(LinkQueue *Q) {
    while (Q->front) {
        Q->rear = Q->front->next;
        free(Q->front);
        Q->front = Q->rear;
    }
    
    return OK;
}

队列的清除

Status ClearQueue(LinkQueue *Q) {
    QueueNodePtr p, q;
    Q->rear = Q->front;
    p = Q->front->next;
    Q->front->next = NULL;
    while (p) {
        q = p;
        p = p->next;
        free(q);
    }
    return OK;
}

队列判断是否为空

Status QueueEmpty(LinkQueue Q) {
    return Q.front == Q.rear;
}

队列的长度

int QueueLength(LinkQueue Q) {
    int i = 0;
    QueueNodePtr p = Q.front;
    while (Q.rear != p) {
        i++;
        p = p->next;
    }
    return i;
}

相关文章

  • 数据结构与算法 (队列实现篇)

    数据结构与算法 (队列实现篇) 在数据结构与算法中,队列(queue)是一种受限的线性储存结构,特殊之处在于它只允...

  • 排序算法

    什么是算法 书籍推荐 《数据结构与算法分析》 表、栈和队列 树 散列(hash) 优先队列(堆) 排序 定义 问题...

  • 集合相关数据结构与算法

    队列 栈数据结构 比较算法 Collections Collection与Collections的区别?Colle...

  • 数据结构与算法学习开篇

    数据结构与算法知识图谱 20个最常用的、最基础数据结构与算法 10个数据结构:数组、链表、栈、队列、散列表、二叉树...

  • 【数据结构与算法 - Swift实现】10 - 优先队列 (Pr

    在【数据结构与算法 - Swift实现】04 - 队列 (Queue)文章里面,我们已经讲过队列,采用先进先出的规...

  • 数据结构与算法分析:大纲]

    00数据结构与算法分析:大纲01数据结构:数组02数据结构:链表03数据结构:栈03数据结构:队列 本系列课程主要...

  • 数据结构:数组

    00数据结构与算法分析:大纲01数据结构:数组02数据结构:链表03数据结构:栈03数据结构:队列 数组 数组是一...

  • 链式队列的定义,ios基础知识篇!

    数据结构与算法-链式队列 链式队列的定义 链式队列是用链表来实现的队列,不存在队满的情况。链式队列也包里队列的特点...

  • 数据结构与算法之美-09讲队列

    数据结构与算法之美-09讲队列 特别备注 本系列非原创,文章原文摘自极客时间-数据结构算法之美[https://t...

  • 队列

    数据结构与算法 --- 队列 【转载】 原文:https://www.jianshu.com/p/c8f2524e...

网友评论

      本文标题:数据结构与算法 — 队列

      本文链接:https://www.haomeiwen.com/subject/brpgmhtx.html