有人可以给我一个双端队列的代码吗?
我已经为此工作了一段时间了,我想我终于破解了它,它适用于我的所有测试,但我感觉会有一些棘手的问题。这是双边队列(双端队列)的高度简化版本,每次添加值时,都会创建一个临时数组来存储所有值,然后附加新值。我相信这样解释是最简单的。如果有人可以仔细检查我是否正确并且这里没有任何明显的错误,我将非常感激。非常感谢大家! :)
public class ArrayBasedDeque<EltType> implements Deque<EltType> {
private final int CAPACITY = 10;
private int capacity;
private int end;
private EltType deque[];
public ArrayBasedDeque() {
this.capacity = CAPACITY;
deque = (EltType[]) (new Object[capacity]);
}
public EltType first() {
return deque[0];
}
public EltType last() {
return deque[end-1];
}
public boolean isEmpty() {
return end == 0;
}
public int size() {
return deque.length;
}
public boolean isFull() {
return end == capacity;
}
public void insertFirst(EltType inserted) {
if (!isEmpty()) {
EltType[] tempArray;
capacity+=1;
tempArray = (EltType[]) new Object[capacity];
for(int i=0;i<end;i++){
tempArray[i+1] = deque[i];
}
deque=tempArray;
}
deque[0] = inserted;
end++;
}
public void insertLast(EltType last) {
if (isFull()){
EltType[] tempArray;
capacity+=1;
tempArray = (EltType[]) new Object[capacity];
for (int i=0;i<end;i++) {
tempArray[i] = deque[i];
}
// System.out.print(deque[end]);
}
deque[end] = last;
end++;
}
public EltType removeFirst() {
EltType[] tempArray;
EltType returned = deque[0];
tempArray = (EltType[]) new Object[capacity];
for (int i=1;i<capacity;i++) {
tempArray[i-1] = deque[i];
}
deque = tempArray;
end--;
return returned;
}
public EltType removeLast() {
EltType[] tempArray;
EltType returned = deque[end-1];
tempArray = (EltType[]) new Object[capacity];
for (int i=0;i<capacity;i++) {
tempArray[i] = deque[i];
}
deque = tempArray;
end--;
return returned;
}
}
Been working on this for a while now and I think I've finally cracked it, it's working for all my tests, but I have a feeling there will be some niggling issues. This is a heavily simplified version of a double sided queue (deque) where every time a value is added, a temporary array is made to store all values, and then the new value appended on. It is easiest to explain this way, I believe. If someone could please just double-check I am correct and there is nothing glaringly wrong here, I would be extremely thankful. Thank you all very much ! :)
public class ArrayBasedDeque<EltType> implements Deque<EltType> {
private final int CAPACITY = 10;
private int capacity;
private int end;
private EltType deque[];
public ArrayBasedDeque() {
this.capacity = CAPACITY;
deque = (EltType[]) (new Object[capacity]);
}
public EltType first() {
return deque[0];
}
public EltType last() {
return deque[end-1];
}
public boolean isEmpty() {
return end == 0;
}
public int size() {
return deque.length;
}
public boolean isFull() {
return end == capacity;
}
public void insertFirst(EltType inserted) {
if (!isEmpty()) {
EltType[] tempArray;
capacity+=1;
tempArray = (EltType[]) new Object[capacity];
for(int i=0;i<end;i++){
tempArray[i+1] = deque[i];
}
deque=tempArray;
}
deque[0] = inserted;
end++;
}
public void insertLast(EltType last) {
if (isFull()){
EltType[] tempArray;
capacity+=1;
tempArray = (EltType[]) new Object[capacity];
for (int i=0;i<end;i++) {
tempArray[i] = deque[i];
}
// System.out.print(deque[end]);
}
deque[end] = last;
end++;
}
public EltType removeFirst() {
EltType[] tempArray;
EltType returned = deque[0];
tempArray = (EltType[]) new Object[capacity];
for (int i=1;i<capacity;i++) {
tempArray[i-1] = deque[i];
}
deque = tempArray;
end--;
return returned;
}
public EltType removeLast() {
EltType[] tempArray;
EltType returned = deque[end-1];
tempArray = (EltType[]) new Object[capacity];
for (int i=0;i<capacity;i++) {
tempArray[i] = deque[i];
}
deque = tempArray;
end--;
return returned;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一些评论:
T
或E
作为类型参数的名称,而不是EltType
CAPACITY
更改为DEFAULT_CAPACITY
,并将其设为静态。first()
也会返回一个值last()
、removeLast()
和removeFirst()<如果
end
为 0,/code> 应该抛出适当的异常removeFirst
中并且removeLast
循环边界是capacity
而不是end
System.arraycopy
作为复制数组 在insertLast
中没有分配给deque
- 因此您在注释中看到了异常。我不确定我是否看到这样做比仅使用 ArrayList有什么好处......拥有单独的
Deque
实现的要点是添加到头部和尾部都很便宜......这里我们两者都没有!...或者当然只是使用 ArrayDeque 或 LinkedList :)
A few comments:
T
orE
as the name of the type parameter, rather thanEltType
CAPACITY
toDEFAULT_CAPACITY
, and make it static.first()
will return a value even if the deque is logically emptylast()
,removeLast()
andremoveFirst()
should throw an appropriate exception ifend
is 0removeFirst
andremoveLast
your loop bound iscapacity
instead ofend
System.arraycopy
as a simpler way to copy arraysdeque
ininsertLast
- hence the exception you're seeing in the comments.I'm not sure I see the benefit of having this over just using
ArrayList<T>
though... the main point of having a separateDeque
implementation would be to make adding to both head and tail cheap... here we have neither!... or of course just use
ArrayDeque
orLinkedList
:)我建议
I would suggest