pop() would just remove the first number, which is 15, right ? what if
it was pop(20) ?
I think it'd be easier for you to understand the concepts if you associate the operations with specific data structures.
For example, operations like push(item)
and pop()
are meant for a stack while operations like enqueue(item)
and dequeue()
are meant for a queue, both of which have specific and well defined behaviors.
Stacks only work with the item at the top, like a stack of pancakes, papers, or any other collection of items that get placed on top of each other.
This means that your {15, 34, 23, 32, 15, 5}
array could be viewed like this:
| 15 | <--- top
| 34 |
| 23 |
| 32 |
| 15 |
| 5 |
+----+
Here, pop
would simply remove the element at the top and then leave top
pointing to the element immediately below it (i.e. 34). Obviously, using push(8)
would add a new element (i.e. 8) at the top of the stack, so it would now look like this:
| 8 | <--- top
| 34 |
| 23 |
| 32 |
| 15 |
| 5 |
+----+
Due to the stack's defined behavior, an operation like pop(item)
would not make sense: it would no longer be limiting itself to the item at the top of the stack.
I'm assuming push(30) would add 30 as the last number.
This would be incorrect, or at least ambiguous: Where do you consider the "last" element to be? What does "last" mean to you? The problem here is that terms like "first" and "last" imply order, which is something queues are meant for (see later). However, stacks are not ordered collections in this sense, so speaking about first/last elements in the context of a stack wouldn't make much sense.
In terms of stack operations like push
ing and pop
ing, you should always talk about the top or the bottom of the stack.
Do enqueue and dequeue work the same as pop and push?
No. Operations like enqueue
and dequeue
are meant for a different data structure called a queue. Queues are different from stacks in how they behave and the operations they support.
For example, while a stack always adds or removes elements at the top, queues always add (i.e. enqueue
) items at the back of the collection and remove (i.e. dequeue
) elements at the front, in First-In-First-Out (FIFO) order.
+-- front
v
+--+----+----+----+----+---+
15 | 34 | 23 | 32 | 15 | 5 |
+--+----+----+----+----+---+
^
+-- back
If you think of a queue's behavior in terms of a line at a store, then it'd be easier to associate the correct behavior of the structure. Every time you're in a line at the store, you're in a queue.
Then would enqueue(40) add 40 at the back of the line?
Correct. The queue would now look like this:
+-- front
v
+--+----+----+----+----+---+----+
15 | 34 | 23 | 32 | 15 | 5 | 40 |
+--+----+----+----+----+---+----+
^
+-- back
What would dequeue(100) do?
This is similar to the issue I described with pop
: it wouldn't make sense because dequeue
is meant to always remove the element at the front. Therefore, a queue would only support dequeue()
and it would automatically remove whatever's at the front of the "line":
+-- front
v
+--+----+----+----+----+---+
34 | 23 | 32 | 15 | 5 | 40 |
+--+----+----+----+----+---+
^
+-- back