简体   繁体   English

向量的基于数组的实现

[英]Array Based Implementation of a Vector

Homework Assistance 作业协助

Describe an array based implementation of a vector such that inserting and removing at beginning and end of the vector can be done in constant time. 描述向量的基于数组的实现,以便可以在恒定时间内完成向量开头和结尾的插入和删除操作。 Argue convincingly. 令人信服地争论。

Obviously this is impossible with a straight-up array. 显然,这对于直接阵列是不可能的。 If you remove from the front, there will be a hole that needs to be filled in order to maintain the vector property. 如果从前面移开,将需要填充一个孔以保持vector属性。 Of course, if we grab the next element over, we will need to do this n times, so the runtime will be linear, not constant. 当然,如果我们抓住下一个元素,则需要执行n次,因此运行时将是线性的,而不是恒定的。

Another way would be to grab the last element and stick it in the front, but what good is a data structure that scrambles your data? 另一种方法是获取最后一个元素并将其粘贴在前面,但是扰乱数据的数据结构有什么用呢?

What I have done so far is to create an array. 到目前为止,我所做的就是创建一个数组。 The odd number indices are behind some point in the array (preferably the middle for size purposes, but it can be anywhere), then the even number indices are before that point. 奇数索引位于数组中某个点的后面(出于大小的考虑,最好位于中间,但可以在任何位置),然后偶数索引位于该点之前。 That takes up a whole bunch of memory and has lots of open slots if that special point is not the centre point. 如果那个特殊点不是中心点,那么它将占用一整堆内存并有很多空槽。 Worst case being 2n. 最坏的情况是2n。 However, it acts like there are no holes because it will always fill the next element out. 但是,它的作用就像没有孔,因为它将始终填充下一个元素。

Insertion: 插入:

private int front = 0;
private int back = 0;
public void insertAtFront(int element)
{
    (front+1));
        dataArray[2*(front + 1) + 1] =  element;
        front++;
}

public void insertAtBack(int element)
{
    dataArray[2*(back+1)] = element;
    back++;
}

For removal, just decrement the front or the back. 要移除,只需减小正面或背面。 Then when accessing the array, only allow the values between front and back to be shown. 然后,在访问数组时,仅允许显示正反之间的值。

First, does this meet the requirements of a vector? 首先,这是否满足向量的要求? Second, when removing, I am having some major issues figuring out how to get past that special centre point. 其次,在删除时,我遇到了一些主要问题,以解决如何越过该特殊中心点的问题。 Say you want to remove the entire array from the front, when you added everything from the back. 假设您要从背面添加所有内容时,要从正面移除整个阵列。

Thank you for any assistance. 感谢您的协助。

The secret is to use two arrays. 秘诀是使用两个数组。 The end of the first array is the "front". 第一个数组的末尾是“ front”。 The end of the second array is the "back". 第二个数组的末尾是“ back”。

I don't understand what you're trying to do with even and odd indices. 我不明白您要使用偶数和奇数索引做什么。 But having a start index and an end index is basically the way to go - leave space empty at the front so that you can add elements there, and increment the start index again if you remove an element. 但是基本上要有一个开始索引和一个结束索引-在前面保留空白,以便您可以在此处添加元素,如果删除元素,则可以再次增加开始索引。

Another option is to use a circular array to allow you to add/remove both at the front and at the end efficiently. 另一种选择是使用圆形数组,使您可以在前端和后端高效地添加/删除。

There are other variations that could be applied: can you also find an implementation such that inserting/removing at the start, the end and (exactly) in the middle is efficient and has O(1) time? 还可以应用其他变体:您是否还能找到一种实现方式,使得在开始,结尾和中间(准确地)插入/删除是高效的,并且具有O(1)时间?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM