私は、Data Structure C++ クラスで Stack に関するすべての基本的なことを学んでいます。ただし、それらの間で少し混乱しています。誰かが定義を教えてもらえますか。arrayStackとarrayQueue、stackQueueとその違いの?そして、教科書はまったく役に立ちません。
5 に答える
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
キャラクターの列を見ています。さて、Enter と Delete の特定のシーケンスは、このキューに対して何をしますか?
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
スタックもキューも、要素を追加すると自然に成長するデータ構造です。
- stackでは、要素はスタックの一方の側に追加 (プッシュ) され、スタックの同じ側から取得 (ポップ) されます。つまり、最後に挿入した要素が最初に取得できる要素になります。このタイプのデータ構造は、LIFO (後入れ先出し) として知られています。
- queueでは、要素はキューの後ろに追加され、前から取得されます。つまり、最初に挿入した要素が、最初に取得できる要素になります。このタイプは FIFO (First In, First Out) として知られています。
さまざまな言語 (具体的には C++ やそのライブラリではない) では、自然に成長するデータ構造を実装できるさまざまな方法があります。そのような方法の 1 つは、単純な内部配列を維持することです。要素は配列に格納され、追加/削除操作は、気にすることなく、その内部配列の拡大または縮小を処理します。通常、構造体がArraySomethingと呼ばれる場合、これらの線に沿った何かを意味する必要があります。
AnArrayStack
は、配列に基づくインターンです。
それらすべての最も重要な違いは、 aStack
がシステムに基づいているLIFO (Last In First Out)
ため、要素を一番上に追加し(push)
、スタックから要素を取得する場合(pop)
は、一番上からも取得することです。いくつかの要素を追加する場合:
stack.push(1), stack.push(2), stack.push(3)
そして、1つポップします:
stack.pop() //returns 3
AQueue
はシステムに基づいているFIFO (First In First Out)
ため、キューの「後ろ」に要素を追加し、キューの「前」から要素を取得します。これは、次の 3 つの要素を追加した場合を意味します。
queue.add(1), queue.add(2), queue.add(3)
次に、キューから要素を取得します。
queue.get() //returns 1
ArrayStack、ArrayQueue は、配列として実装されていることを意味します。そして、StackQueue は両方の組み合わせです。前面と背面から要素を取得できます。
std::stack
C++ 標準では、(コンテナー アダプターを除いて) あなたが言及した構造について何も述べていません。したがって、その章をもう一度読む必要があります。
ほとんどの場合、本も捨てて、代わりに標準のコンテナーを使用することもできます。