5

I'm trying to become more comfortable with these 4 concepts.

So if we had an array of {15, 34, 23, 32, 15, 5}

and we had operations such as

pop();
push(30);
enqueue(40);
dequeue(100);

pop() would just remove the first number, which is 15, right ? what if it was pop(20) ?

I'm assuming push(30) would add 30 as the last number.

Do enqueue and dequeue work the same as pop and push?

(Then would enqueue(40) add 40 at the back of the line? What would dequeue(100) do ?)

Zoe
  • 27,060
  • 21
  • 118
  • 148
vexomrs
  • 61
  • 1
  • 1
  • 4
  • do you really want to perform these operations on an array? Maybe you should consider to be more specific and use queues/stacks? – miracle_the_V Dec 10 '15 at 10:43
  • Well I'm just trying to understand the concepts. The array is just something that I could see the demonstration on more clearly – vexomrs Dec 10 '15 at 10:45
  • C++ array as in std::array does not have these operations. push and pop are available in std::stack, and std::stack::pop() returns an element and does not take any arguments (pop(20) is not possible), push puts elements at the top of the stack, not at the end. There is no enqueue and dequeue in std library, but std::vector and std::deque have specific operations which are explicit about the placement of elements: push_back, pop_front, etc.. Just look up the manual of these structures. – mariusm Dec 10 '15 at 10:49
  • @vexomrs: Please don't forget to come back to consider up-voting and accepting a response if you found it useful. It benefits you, those trying to answer your questions, and the community at large. That's how we say "thank you" here. – code_dredd Dec 11 '15 at 03:37

1 Answers1

10

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 pushing and poping, 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
code_dredd
  • 5,915
  • 1
  • 25
  • 53