首页
友链
关于

数据结构与算法 课堂笔记 4:队列(Queue)和优先队列(Priority Queue)

03 / 20 / 2023 (最后编辑于 03 / 20 / 2023)
预计阅读时间 4 分钟

Defination

ADT

Value:
A sequence of items that belong to some data type ITEM_TYPE.

Operations of Queue q:

  1. Boolean IsEmpty()
    Postcondition: If the queue is empty, return true, otherwise return false
  2. Boolean IsFull()
    Postcondition: If the queue is full, return true, otherwise return false
  3. ITEM_TYPE Dequeue() /*take out the front one and return its value*/
    Precondition: q is not empty
    Postcondition: The front item in q is removed from the sequence and returned
  4. Void Enqueue(ITEM_TYPE e) /*to append one item to the rear of the queue*/
    Precondition: q is not full
    Postcondition: e is added to the sequence as the rear one

Implementation

Array

Use circulated array to minify allocating times, thus improving efficiency. Caution: When using array circulated-ly, make sure rear pointer doesn’t catch with front! In that case, isEmpty() will return true!

Copying When rear <\lt front (2 Ways)

  1. Move front to size - 1 first, then copy 0 to rear
  2. Copy 0 to rear as before, then copy front to size - 1 to the end of the new array

Linked List

Application

Priority Queue