【问题标题】:What is stack ? arrayStack ? arrayQueue ? stackQueue ? how are they different in C++? [closed]什么是栈?数组堆栈?数组队列?堆栈队列?它们在 C++ 中有何不同? [关闭]
【发布时间】:2013-09-30 19:29:15
【问题描述】:

我只是在我的数据结构 C++ 课程中学习有关 Stack 的所有基本知识。但是,它们之间变得有些混乱。谁能告诉我def是什么? arrayStack 和arrayQueue、stackQueue 和它们的区别?而且教科书一点用也没有。

【问题讨论】:

标签: c++ arrays stack


【解决方案1】:
Firstly you need to understand the fundamentals, lets take a ride thorough the basics again.

We begin with stack empty:

-----
stack
Now, let's perform Push(stack, A), giving:

-----
| A |  <-- top
-----
stack
Again, another push operation, Push(stack, B), giving:

-----
| B |  <-- top
-----
| A |
-----
stack

堆栈

堆栈的概念图是这样的:

Now let's remove an item, letter = Pop(stack), giving:

-----              -----
| A |  <-- top     | B |
-----              -----
stack              letter
And finally, one more addition, Push(stack, C), giving:

-----
| C |  <-- top
-----
| A |
-----
stack
You'll notice that the stack enforces a certain order to the use of its contents, i.e., the Last thing In is the First thing Out. Thus,

我们说堆栈强制执行 LIFO 顺序。

Now we can see one of the uses of a stack...To reverse the order of a set of objects.


Like a stack, a queue usually holds things of the same type. We usually draw queues horizontally. Here's a queue of characters with 3

元素:

queue
-------------
| a | b | c |
-------------
  ^       ^
  |       |
front    rear


Queues are useful because they produce a certain order in which the contents of the queue are used. Let's see what order that is by

查看字符队列。现在,一个特别的 对这个队列执行的输入和删除序列:

queue
-------------
| a | b | c |
-------------
  ^       ^
  |       |
front    rear
Now, Enter(queue, 'd')...

queue
-----------------
| a | b | c | d |
-----------------
  ^           ^
  |           |
front        rear
Now, ch = Delete(queue)...

queue           ch
-------------   -----
| b | c | d |   | a |
-------------   -----
  ^       ^
  |       |
front    rear

【讨论】:

  • 感谢 kotAPI。什么是数组堆栈和数组队列,以及堆栈队列数据结构?它们是数组还是堆栈还是队列? opendatastructures.org/ods-cpp.pdf
  • 数组栈——在编程中使用数组实现栈。 Array Queue——在编程中使用数组实现队列。
  • @kotAPI,看起来你已经从另一个答案中引用了这个答案。如果你能提到你从哪里复制的,那就太好了。
【解决方案2】:

堆栈和队列都是随着您添加元素而自然增长的数据结构。

  • 堆栈中,元素被添加(推送)到堆栈的一侧,然后从堆栈的同一侧检索(弹出)。换句话说,您插入的最后一个元素是您可以检索的第一个元素。这种类型的数据结构称为 LIFO(后进先出)。
  • 队列中,元素被添加到队列的后面并从前面检索。换句话说,您插入的第一个元素是您可以检索的第一个元素。这种类型称为 FIFO(先进先出)。

在各种语言中(特别是 C++ 或其库),可以通过多种方式实现自然增长的数据结构。一种方法是保留一个简单的内部数组:元素存储在数组中,添加/删除操作负责扩大或缩小该内部数组,而不会打扰您。通常,当一个结构被称为 ArraySomething 时,它应该表示类似的意思。

【讨论】:

  • 非常感谢。我想我现在明白了
【解决方案3】:

array 是一种组织数据(及其存储分配)的方式,而 stackqueue 是针对 insertremoveaccess 数据的策略。

不同的policies可以和organizations结合,根据你的需要构造不同的数据结构。

例如:

  • 使用数组堆叠
  • 使用数组排队
  • 使用链表堆叠
  • 使用数组等的树

【讨论】:

  • 我发现你的答案是最容易理解的;)
  • @user2383193 谢谢,很高兴它有帮助:)
【解决方案4】:

ArrayStack 是基于数组的实习生。

它们最重要的区别是,Stack 基于LIFO (Last In First Out) 系统,因此您将元素添加到顶部(push),如果您想从堆栈中取出元素@987654325 @,你也从上面拿它。如果添加一些元素:

stack.push(1), stack.push(2), stack.push(3)

然后弹出一个:

stack.pop() //returns 3

Queue 基于FIFO (First In First Out) 系统,因此您在队列的“后部”添加元素,并从队列的“前部”获取元素。这意味着如果您添加三个元素:

queue.add(1), queue.add(2), queue.add(3)

然后想从队列中获取一个元素:

queue.get() //returns 1

ArrayStack,ArrayQueue 的意思是,它们被实现为一个数组。 StackQueue 是两者的结合。您可以从正面和背面获取元素。

【讨论】:

  • 我以前从未听说过 ArrayStack。这是一个猜测,还是你读过同一本书?
  • 这只是意味着,堆栈或队列后面的实现是一个数组,而不是链表或类似的东西。
  • 所以如果是ArrayStack,还是要遵循LIFO的规则,如果是ArrayQueue,还是基于FIFO?
  • 是的,所以这可能是一个类,它使用数组来存储其数据,但只允许您按照 FIFO 或 LIFO 规则输入和获取数据。
【解决方案5】:

C++ 标准没有说明您提到的结构(std::stack 容器适配器除外)。因此,您必须再次阅读该章节。

很可能你也可以把这本书扔掉,改用标准容器。

【讨论】:

  • 这对理解栈和队列的基本区别很有帮助。
  • @ThorstenDittmar 这将是一个与 OP 提出的完全不同的问题的答案。
猜你喜欢
  • 2020-06-12
  • 1970-01-01
  • 1970-01-01
  • 2013-05-28
  • 2021-03-17
  • 2014-04-21
  • 2013-09-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多