📜  C++ 中列表的优先级队列及示例

📅  最后修改于: 2022-05-13 01:55:23.853000             🧑  作者: Mango

C++ 中列表的优先级队列及示例

优先队列

优先级队列是一种容器适配器,专门设计为队列的第一个元素是队列中所有元素中最大的,并且元素是非递增顺序的(因此我们可以看到队列的每个元素都有一个优先级{固定命令})。

与优先队列一起使用的函数:

  • empty():该函数返回队列是否为空。
  • size():此函数返回队列的大小。
  • top():返回对队列最顶层元素的引用。
  • push(x):此函数在队列末尾添加元素“x”。

列表

列表是允许非连续内存分配的序列容器。与向量相比,列表的遍历速度较慢,但一旦找到位置,插入和删除都很快。通常,当我们说 List 时,我们谈论的是双向链表。为了实现单链表,我们使用前向列表。

与列表一起使用的函数:

  • push_front(x):在列表的开头添加一个新元素“x”。
  • push_back(x):在列表末尾添加一个新元素“x”。

转发列表

STL中的前向列表实现了单链表。从 C++11 引入,前向列表在插入、删除和移动操作(如排序)中比其他容器更有用,并且允许时间常数插入和删除元素。它与列表的不同之处在于,前向列表仅跟踪下一个元素位置,而列表同时跟踪下一个和前一个元素,从而增加了存储每个元素所需的存储空间。前向列表的缺点是它不能向后迭代,并且它的各个元素不能直接访问。
当只需要前向遍历时,前向列表优于列表(就像单链表优于双向链表一样),因为我们可以节省空间。一些示例情况是,散列中的链接,图的邻接表表示等。

与转发列表一起使用的函数:

  • push_front(x):在列表的开头添加一个新元素“x”。

本文重点介绍如何在 C++ 中使用前向列表和列表的优先级队列。在设计复杂的数据结构时,列表和转发列表的优先级队列非常有用。

下面是前向列表的优先级队列的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
  
// Function to print priority queue
// contents. Deliberately passing it
// call by value since we don't want
// to remove elements from the priority
// queue
void print(priority_queue > priorityQueue)
{
    while (!priorityQueue.empty()) {
        // Each element of the priority
        // queue is a forward list itself
        forward_list st = priorityQueue.top();
  
        cout << "[ ";
  
        // Print the forward list elements
        for (auto element : st)
            cout << element << ' ';
        cout << ']';
        cout << '\n';
  
        // Pop out the topmost forward
        // list
        priorityQueue.pop();
    }
}
  
// Driver code
int main()
{
    // Declaring a priority queue of
    // forward list
    priority_queue > priorityQueue;
  
    // Declaring a forward list
    forward_list forwardList1;
  
    // Inserting into the forward list
    forwardList1.push_front(2);
    forwardList1.push_front(4);
    forwardList1.push_front(6);
  
    // Inserting the forward list into
    // the priority queue
    priorityQueue.push(forwardList1);
  
    // Declaring another forward list
    forward_list forwardList2;
  
    // Inserting into the forward list
    forwardList2.push_front(1);
    forwardList2.push_front(3);
    forwardList2.push_front(7);
  
    // Inserting the forward list into
    // the priority queue
    priorityQueue.push(forwardList2);
  
    // Declaring another forward list
    forward_list forwardList3;
  
    // Inserting into the forward list
    forwardList3.push_front(11);
    forwardList3.push_front(22);
    forwardList3.push_front(33);
  
    // Inserting the forward list into
    // the priority queue
    priorityQueue.push(forwardList3);
  
    // Calling print function
    print(priorityQueue);
  
    return 0;
}


C++
// C++ program to implement
// the above approach
#include 
using namespace std;
  
// Function to print priority queue
// contents. Deliberately passing it
// call by value since we don't want
// to remove elements from the priority
// queue
void print(priority_queue > priorityQueue)
{
    while (!priorityQueue.empty()) {
        // Each element of the priority
        // queue is a list itself
        list st = priorityQueue.top();
  
        cout << "[ ";
  
        // Print the list elements
        for (auto element : st)
            cout << element << ' ';
  
        cout << ']';
        cout << '\n';
  
        // Pop out the topmost list
        priorityQueue.pop();
    }
}
  
// Driver code
int main()
{
    // Declaring a priority queue of
    // lists
    priority_queue > priorityQueue;
  
    // Declaring a list
    list list1;
  
    // Inserting into the list
    // Pushing at the front in the
    list1.push_front(2);
  
    // Pushing at the back in the
    // list
    list1.push_back(4);
    list1.push_back(6);
  
    // Inserting the list into the
    // priority queue
    priorityQueue.push(list1);
  
    // Declaring another list
    list list2;
  
    // Inserting into the list
    // Pushing at the front in the
    list2.push_front(2);
  
    // Pushing at the back in the
    // list
    list2.push_back(4);
    list2.push_back(7);
  
    // Inserting the list into the
    // priority queue
    priorityQueue.push(list2);
  
    // Declaring another list
    list list3;
  
    // Inserting into the list
  
    // Pushing at the front in the
    list3.push_front(11);
  
    // Pushing at the back in the
    // list
    list3.push_back(22);
    list3.push_back(33);
  
    // Inserting the list into the
    // priority queue
    priorityQueue.push(list3);
  
    // Calling print function
    print(priorityQueue);
  
    return 0;
}


输出
[ 33 22 11 ]
[ 7 3 1 ]
[ 6 4 2 ]

下面是列表优先级队列的实现:

C++

// C++ program to implement
// the above approach
#include 
using namespace std;
  
// Function to print priority queue
// contents. Deliberately passing it
// call by value since we don't want
// to remove elements from the priority
// queue
void print(priority_queue > priorityQueue)
{
    while (!priorityQueue.empty()) {
        // Each element of the priority
        // queue is a list itself
        list st = priorityQueue.top();
  
        cout << "[ ";
  
        // Print the list elements
        for (auto element : st)
            cout << element << ' ';
  
        cout << ']';
        cout << '\n';
  
        // Pop out the topmost list
        priorityQueue.pop();
    }
}
  
// Driver code
int main()
{
    // Declaring a priority queue of
    // lists
    priority_queue > priorityQueue;
  
    // Declaring a list
    list list1;
  
    // Inserting into the list
    // Pushing at the front in the
    list1.push_front(2);
  
    // Pushing at the back in the
    // list
    list1.push_back(4);
    list1.push_back(6);
  
    // Inserting the list into the
    // priority queue
    priorityQueue.push(list1);
  
    // Declaring another list
    list list2;
  
    // Inserting into the list
    // Pushing at the front in the
    list2.push_front(2);
  
    // Pushing at the back in the
    // list
    list2.push_back(4);
    list2.push_back(7);
  
    // Inserting the list into the
    // priority queue
    priorityQueue.push(list2);
  
    // Declaring another list
    list list3;
  
    // Inserting into the list
  
    // Pushing at the front in the
    list3.push_front(11);
  
    // Pushing at the back in the
    // list
    list3.push_back(22);
    list3.push_back(33);
  
    // Inserting the list into the
    // priority queue
    priorityQueue.push(list3);
  
    // Calling print function
    print(priorityQueue);
  
    return 0;
}
输出
[ 11 22 33 ]
[ 2 4 7 ]
[ 2 4 6 ]

默认情况下,优先级队列是一个最大堆,因此对于优先级队列内部的两个列表/转发列表,具有较大第一个元素的列表/转发列表是最顶部的元素。如果第一个元素相等,则比较列表/前向列表的第二个值,依此类推。