我正在阅读http://www.docjar.com/html/api/java/util/LinkedList.java.html
在Java中声明队列时
Queue<Integer> queue = new LinkedList<Integer>();
Run Code Online (Sandbox Code Playgroud)
现场背后会发生什么?因为我看到队列是只有方法签名的接口,和LinkedList不直接实现它,所以它是如何覆盖这些方法(add(),peek(),poll(),offer(),和remove()),并做多态性这样呢?我的意思是,你只能访问一些特定的方法,但并非所有的人都从LinkedList例如public void add(int index, E element)不再可用,因为它使明显的类型队列中.还没有我们需要施展吗?
我构建了这个代码,以队列方式显示数组的所有条目.问题是,我被告知这可以在没有for声明的情况下完成,将内容倒i回到0,但我无法弄清楚如何.
怎么可以做到这一点?
public void display()
{
int i = frontIndex;
while(true)
{
if (i == numberOfItems)
i = 0;
System.out.print(array[i++] + " ");
if (i == rearIndex + 1)
break;
}
}
Run Code Online (Sandbox Code Playgroud) 这似乎是一个非常基本的问题,但我已经被困了几个小时.
我们什么时候使用方法入队/出队?什么时候使用offer/poll?!
我想用void enqueue(int x, int p)和int dequeue()方法创建一个整数PQ,我该如何声明这样的队列?
谢谢.
我只是尝试实现最好的第一次搜索,我不确定该算法是否有任何LIFO或FIFO属性.如果是的话我应该使用哪一个?我需要使用它吗?
我无法弄明白,这是我到目前为止所拥有的.
但该函数Enqueue()不会更改main方法中的Head&Tail对象.
我该怎么做呢?
#include <stdio.h>
typedef struct queuenode{
int data;
struct queuenode *next;
} Queue_type;
void Enqueue( Queue_type *head, Queue_type *tail , int item)
{
Queue_type *temp;
temp->data = item;
temp->next = NULL;
if (tail == NULL){//if first node
head=temp;
tail=temp;
}
else{
tail->next = temp;
tail = temp;
}
}
void main(void)
{
Queue_type *Head;
Queue_type *Tail;
Head = NULL;
Tail = NULL;
printf("\n[*]Enter Number to Enqueue : ");
scanf("%d", &item);
Enqueue(Head, Tail, …Run Code Online (Sandbox Code Playgroud) 我有一个Java的队列,表现出奇怪的行为.如果我调用q.size(),After元素被推入队列,只有前半部分项目从同一队列中弹出.但是,如果我q.size()在弹出任何项目之前存储返回的值,则会弹出所有项目q.这是为什么?在官方文件说:
public int size()返回此列表中的元素数.
这是我正在使用的代码.我使用了三种不同的编译器:JDK 6.0_31,JDK 7.0_7和Eclipse Compiler 0.A48.结果是一样的.
import java.util.*;
public class StrangeQueueTest{
public static void main(String[] args){
Queue<String> q = new LinkedList<String>();
String[] testData = {"1: one", "2: two", "3: three", "4: four", "5: five",
"6: six", "7: seven", "8: eight", "9: nine", "10: ten", "11: eleven",
"12: twelve", "13: thirteen", "14: fourteen", "15: fifteen",
"16: sixteen", "17: seventeen", "18: eighteen", "19: nineteen",
"20: twenty"};
//Push items into the …Run Code Online (Sandbox Code Playgroud) 我正在努力解决这段代码:
std::queue<char> output_queue;
std::string output_string
// put stuff into output_queue
while (!output_queue.empty())
{
output_string.insert(0,(output_queue.front()));
output_queue.pop();
}
Run Code Online (Sandbox Code Playgroud)
我不知道怎么办不行,因为它std::queue<char>::front()会返回一个char&,我不能把它放进去std::string.
因此,我有一个可以在堆栈或队列中的列表。两者之间是否有任何性能差异?
我也注意到java.utils实现扩展了Vector。制作自己的实现会更快,还是直接使用Vector会更快?
任何人都可以帮助我理解为什么for loops下面两个输出产生不同的输出?
我的眼睛是相同的,但原始Dictionary对象for loop输出其所有9个成员,但Queue对象for loop只输出前5个.
void test()
{
Dictionary<int, string> d = new Dictionary<int, string>();
d.Add(0, "http://example.com/1.html");
d.Add(1, "http://example.com/2.html");
d.Add(2, "http://example.com/3.html");
d.Add(3, "http://example.com/4.html");
d.Add(4, "http://example.com/5.html");
d.Add(5, "http://example.com/6.html");
d.Add(6, "http://example.com/7.html");
d.Add(7, "http://example.com/8.html");
d.Add(8, "http://example.com/9.html");
Queue<KeyValuePair<int, string>> requestQueue = new Queue<KeyValuePair<int, string>>();
// build queue
foreach (KeyValuePair<int, string> dictionaryListItem in d)
{
requestQueue.Enqueue(dictionaryListItem);
Console.WriteLine(dictionaryListItem.Value);
}
Console.WriteLine(" ");
for (int i = 0; i < requestQueue.Count; i++)
{
Console.WriteLine(requestQueue.Peek().Value);
requestQueue.Dequeue();
}
}
Run Code Online (Sandbox Code Playgroud)