简体   繁体   English

访问堆栈中的元素的时间复杂度是多少? 还是有可能做到这一点?

[英]What is the time complexity of accessing an element in a Stack? Or is it even possible to do it?

One of the tutorials I've watched said that accessing a stack implemented in either a linked list or array takes O(1) time complexity but he also said that 'searching' in unsorted data is not applicable. 我看过的其中一篇教程说,访问在链表或数组中实现的堆栈会花费O(1)的时间复杂度,但他也说“搜索”未排序的数据不适用。 Now I'm confused if 'searching' is the same with 'accessing'? 现在我很困惑“搜索”与“访问”是否相同?

One of the tutorials I've watched said that accessing a stack implemented in either a linked list or array takes O(1) time complexity 我看过的其中一篇教程说,访问以链表或数组形式实现的堆栈会花费O(1)的时间复杂度

The tutorial is correct about array-based stacks. 本教程关于基于数组的堆栈是正确的。 Assuming that the stack API permits it, you can get the N from top or N from bottom element of an array-based stack in O(1). 假设堆栈API允许,您可以从O(1)中基于数组的堆栈的顶部元素中获得N或从底部元素中获得N。 It is just an array lookup. 这只是一个数组查找。

For linked-list based stacks, it is more complicated. 对于基于链表的堆栈,它更为复杂。 You can get the top (or maybe bottom) element in O(1). 您可以在O(1)中获得顶部(或底部)元素。 But getting the N'th from top element is an O(N) operation. 但是从顶部元素获取第N个对象是O(N)操作。 You have to follow N references to get to the element you need. 您必须遵循N个引用才能获得所需的元素。

but he also said that 'searching' in unsorted data is not applicable. 但他还说,“搜索”未排序的数据不适用。 Now I'm confused if 'searching' is the same with 'accessing'? 现在我很困惑“搜索”与“访问”是否相同?

They are different things. 他们是不同的东西。 Consider this: 考虑一下:

String[] array = new String[] {"A", "B", "C"};

Accessing an element of array is array[i] . 访问array一个元素是array[i] For an array that is an O(1) operation. 对于是O(1)运算的数组。 (For a linked list, the equivalent is O(N) ) (对于链表,等效值为O(N)

Searching for an element of an array is like this: 搜索数组的元素是这样的:

for (String s : array) {
   if (s.equals("Fred")) {
       // found it!
   }
}

As I have used these words, "accessing" and "searching" clearly mean different things. 当我使用这些词时,“访问”和“搜索”显然意味着不同的含义。

Now as Erwin states, the meanings of terms like "searching" and "accessing" will vary from one author to another. 现在,正如欧文所说,“搜索”和“访问”等术语的含义将因作者而异。 But "searching" has a clear implication of needing to look for something ... rather than knowing where it will be. 但是“搜索”具有明显的含义,即需要寻找某物……而不是知道它在哪里。


It is not entirely clear what the tutorial you are reading might mean by "'searching' in unsorted data is not applicable" . 目前尚不清楚您正在阅读的教程对“不搜索未排序的数据不适用”可能意味着什么。 It is certainly possible to search in non-sorted data, but that will be an O(N) operation ... unless you have done something to organize the data beforehand. 当然可以搜索未排序的数据,但这将是一个O(N)操作...除非您事先做过一些整理数据的工作。 (Se my example code above.) (请参见上面的示例代码。)

But then again a stack data structure is typically optimized for the special case of accessing the top of the stack. 但是,然后再次针对访问栈顶的特殊情况通常优化栈数据结构。 Not accessing the N'th element, or searching for an element with a particular value. 不访问第N个元素,或搜索具有特定值的元素。

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

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