Deques and Randomized Queues

本文探讨了两种数据结构实现:双向队列(Deque)采用双向链表确保所有操作的时间复杂度为O(1);随机队列(RandomizedQueue)通过数组实现,保证了dequeue、sample和next等操作的均摊常数时间复杂度。此外,还介绍了如何使用RandomizedQueue来实现随机选择k个元素的Permutation功能。
摘要由CSDN通过智能技术生成

Deques and Randomized Queues

Deque.java

双向队列,因为要求所有操作的复杂度为 O ( 1 ) O(1) O(1),采用双向链表进行实现。

import java.util.Iterator;
import java.util.NoSuchElementException;

public class Deque<Item> implements Iterable<Item> {
   
    private Node first = null;
    private Node last = null;
    private int size = 0;

    // construct an empty deque
    public Deque() {
   }

    // is the deque empty?
    public boolean isEmpty() {
   
        return first == null;
    }

    // return the number of items on the deque
    public int size() {
   
        return size;
    }

    // add the item to the front
    public void addFirst(Item item) {
   
        if (item == null) {
   
            throw new IllegalArgumentException();
        }
        size++;
        Node old = first;
        first = new Node();
        first.item = item;
        // 若原队列为空需要进行特殊处理
        if (old == null) {
   
            last = first;
            return;
        }
        first.next = old;
        old.prev = first;
    }

    // add the item to the back
    public void addLast(Item item) {
   
        if (item == null) {
   
            throw new IllegalArgumentException();
        }
        size++;
        Node old = last;
        last = new Node();
        last.item = item;
        // 若原队列为空需要进行特殊处理
        if (old == null) {
   
            first = last;
            return;
        }
        old.next = last;
        last.prev = old;
    }

    // remove and return the item from the front
    public Item removeFirst() {
   
        if (isEmpty()) {
   
            throw new NoSuchElementException();
        }
        Node temp = first;
        first = first.next;
        // 若移除后为空队列需要进行特殊处理
        if (first == null) 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,我们需要定义一个线程安全的队列类。然后,我们可以将这10个deque中的元素,平均分给10个线程。每个线程获取到自己分配的deque之后,即可在其中处理元素。以下是一个可能的实现: ``` #include <iostream> #include <deque> #include <thread> #include <mutex> #include <condition_variable> using namespace std; template<typename T> class SafeDeque { public: void push(T item) { unique_lock<mutex> lock(_mutex); _deque.push_back(item); _cond.notify_one(); } bool pop(T& item) { unique_lock<mutex> lock(_mutex); _cond.wait(lock, [=]{ return !_deque.empty(); }); if(_deque.empty()) { return false; } item = _deque.front(); _deque.pop_front(); return true; } private: deque<T> _deque; mutex _mutex; condition_variable _cond; }; void worker(int id, SafeDeque<deque<int>* >& deques) { deque<int>* myDeque = nullptr; while(deques.pop(myDeque)) { cout << "Thread " << id << " processing deque of size " << myDeque->size() << endl; // Process items in the deque delete myDeque; } } int main() { // Create 10 deques with some elements const int numDeques = 10; deque<int> deques[numDeques]; for(int i = 0; i < numDeques; ++i) { for(int j = 0; j < i*100; ++j) { deques[i].push_back(j); } } // Create a queue of deques, and push them into the queue SafeDeque<deque<int>* > queue; for(int i = 0; i < numDeques; ++i) { queue.push(new deque<int>(deques[i])); } // Create 10 worker threads to process the deques const int numThreads = 10; thread workers[numThreads]; for(int i = 0; i < numThreads; ++i) { workers[i] = thread(worker, i, ref(queue)); } // Join the worker threads for(int i = 0; i < numThreads; ++i) { workers[i].join(); } return 0; } ``` 在主函数中,我们首先创建了10个deque并填充一些元素。然后我们将这10个deque分别存到一个线程安全的队列中,然后创建10个worker线程,从队列中获取自己分配的deque进行处理。要注意的是,我们需要在每个线程结束时,将它们占用的deque内存释放掉,以避免内存泄漏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值