我的朋友面对IT公司的采访他们问他给每个数据结构的实际例子如何将这种数据结构用于计算机研究?
数据结构
(例如,在维护进程队列的操作系统中[使用队列数据结构]对于所有其他队列)
与软件实现和计算机科学,操作系统等相关的示例
希望得到积极的回应
我有一个用Ruby编写的Http客户端,可以对URL进行同步请求.但是,为了快速执行多个请求,我决定使用Eventmachine.我们的想法是将所有请求排队并使用eventmachine执行它们.
class EventMachineBackend
...
...
def execute(request)
$q ||= EM.Queue.new
$q.push(request)
$q.pop {|request| request.invoke}
EM.run{EM.next_tick {EM.stop}}
end
...
end
Run Code Online (Sandbox Code Playgroud)
原谅我使用全局队列变量.我稍后会重构它.我正在以EventMachineBackend#execute正确的方式使用Eventmachine队列吗?
我在实现中看到的一个问题是它本质上是同步的.我推送一个请求,弹出并执行请求并等待它完成.
任何人都可以建议更好的实施.
我正在阅读的其中一本书是"莫里斯·赫利希和尼尔·沙维特的多处理器编程艺术".在其中,有一个"无等待"队列(在一些语言自适应之后)在线程环境中完美地运行测试和逻辑 - 至少,即使在分布在5个线程上的10,000,000个项目上也没有冲突逻辑检查出来.
(如果无法获取项目,我编辑队列返回false,而不是抛出异常.代码如下所示).
但是,它有一个警告; 队列无法增长.粗略的逻辑检查指出它不能在不锁定队列的情况下增长 - 这在某种程度上否定了拥有无锁队列的重要性.
因此,目的是创建一个可以增长的无锁(或至少无饥饿锁定)队列.
所以:如果我们基本上给每个线程提供他们自己的共享队列,那么这种方式不是矛盾的(并且接受这个已被解决的高概率并且更好地用于边做边学):
WaitFreeQueue<Queue<int>> queues = new WaitFreeQueue<Queue<int>>(threadCount);
// Dequeue a queue, enqueue an item, enqueue the queue.
// Dequeue a queue, dequeue an item, enqueue the queue.
Run Code Online (Sandbox Code Playgroud)
和无等待队列(如果我做出任何重大更改,先前的代码包含在注释中):
/// <summary>
/// A wait-free queue for use in threaded environments.
/// Significantly adapted from "The Art of Multiprocessor Programming by Maurice Herlihy and Nir Shavit".
/// </summary>
/// <typeparam name="T">The type of item in the queue.</typeparam>
public class WaitFreeQueue<T>
{
/// <summary>
/// …Run Code Online (Sandbox Code Playgroud) 如何使用队列中的值更新GUI元素?如果我使用异步队列构造,textlable不会更新.这是我使用的代码示例:
- (IBAction)dbSizeButton:(id)sender {
dispatch_queue_t getDbSize = dispatch_queue_create("getDbSize", NULL);
dispatch_async(getDbSize, ^(void)
{
[_dbsizeLable setText:[dbmanager getDbSize]];
});
dispatch_release(getDbSize);
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
给出示例代码:
Comparator<Node> comparator = this.createCompartor(algorithmChoice , matrix);
this.m_openList1 = new PriorityQueue<Node>(100, comparator);
Run Code Online (Sandbox Code Playgroud)
什么是100在PriorityQueue构造函数中代表什么?
我正在尝试获取一个随机数列表并将其放入队列而不重复随机数.
int number = 40;
for (int j = 0; j<number; j++)
{
int pick = random.nextInt(number);
myQueue.add(Integer.toString(pick));
}
System.out.println("the queue size: "+myQueue.size());
Iterator it = myQueue.iterator();
while(it.hasNext()){
String iteratorValue = (String)it.next();
System.out.println("queue next value: "+iteratorValue);
}
Run Code Online (Sandbox Code Playgroud)
用上面的代码,我得到了一些重复的随机数
有谁知道怎么做?
我想创建一个Azure应用程序,它执行以下操作:
从我一直阅读的内容来看,似乎有两种方法:Windows Azure Service Bus或使用Queues.每个辅助角色还将结果存储在数据库中.
服务总线似乎更适合其发布/订阅模型,因此所有工作者角色将获得相同的命令并且大致相同的时间.队列似乎更容易使用.
在开发时,服务总线是否可以在本地与仿真器一起使用?我正在使用免费试用版,但在开发过程中无法持续保留应用程序.此外,在使用队列时,如何通知Web角色处理完成?
我有结构,我想编写一个主函数来做一些测试,但是:1 - 如何打印这个队列?2 - 我怎样才能将更多的恐龙列入?喜欢,用"for"?
我不是想在这里做一个家庭作业的答案(在有人这么说之前).我有一个考试,所以我只是想学习如何去做,如果有人可以帮助我,我将非常感激.
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 10
typedef struct Queue{
int size;
int first;
int last;
int items[MAX_SIZE];
} Queue;
Queue* createQueue() {
Queue *queue = (Queue*)malloc(sizeof(Queue));
queue->size = 0;
queue->first = 0;
queue->last = MAX_SIZE - 1;
return queue;
}
Enqueue(Queue *queue, int item) {
if(queue->size >= MAX_SIZE) {
printf("Queue is full!");
}
else {
queue->last = (queue->last + 1) % MAX_SIZE;
queue->items[queue->last] = item;
queue->size++;
}
}
Dequeue(Queue *queue) {
if(queue->size <= …Run Code Online (Sandbox Code Playgroud) 以下给出了下面提到的代码的错误.哪里出错了?
error: ‘function’ is not a member of ‘std’
Run Code Online (Sandbox Code Playgroud)
我想使用C++ std lib队列进行优先级队列,最小化队列是IceCream,它花费的时间最少.我试过实现这个 - > 用自定义比较器在c ++中声明一个priority_queue
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#include <deque>
#include <iterator>
#include <string>
#include <sstream>
using namespace std;
class IceCream
{
public:
int time_to_prep;
IceCream(int flav) {
time_to_prep = flav;
}
};
bool Compare(IceCream a, IceCream b)
{
return a.time_to_prep > b.time_to_prep;
}
int main()
{
priority_queue<IceCream, vector<IceCream>, std::function<bool(IceCream a, IceCream b)> > my_pq(Compare);
my_pq.push(IceCream(4)); …Run Code Online (Sandbox Code Playgroud)