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