什么是堆栈?arrayStack ?arrayQueue ?堆栈队列 ?它们在C++上有何不同



我只是在我的数据结构C++课上学习有关 Stack 的所有基本知识。但是,它们之间有点混乱。谁能告诉我 arrayStack 和 arrayQueue、stackQueue 的定义以及它们的区别是什么?而且教科书根本没有真正的帮助。

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,

我们说堆栈强制执行后进先出订单。

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

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

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

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

数组

是一种组织数据(及其存储分配(的方法,而堆栈和队列是insertremoveaccess数据的策略。

不同的policies可以与organizations相结合,根据您的需求构建不同的数据结构。

例如:

  • 使用数组进行堆栈
  • 使用数组排队
  • 使用链表进行堆叠
  • 使用数组等的树。

ArrayStack是基于数组的实习生。

其中最重要的区别在于,Stack是基于LIFO (Last In First Out)系统,因此您将元素添加到顶部(push),如果您想从堆栈(pop)中获取元素,您也可以从顶部获取它。如果添加一些元素:

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是两者的结合。你可以从前面和后面获取你的元素。

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

很可能你也可以把书扔掉,开始使用标准容器。

最新更新