

The queue would now look like this: +- front

Then would enqueue(40) add 40 at the back of the line?Ĭorrect. Every time you're in a line at the store, you're in a queue. 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. dequeue) elements at the front, in First-In-First-Out (FIFO) order. enqueue) items at the back of the collection and remove (i.e. Queues are different from stacks in how they behave and the operations they support.įor example, while a stack always adds or removes elements at the top, queues always add (i.e. Single-ended Queue Wrapper Class using Deque from collections import deque class. Operations like enqueue and dequeue are meant for a different data structure called a queue. dequeue: Removing the element that has been in the queue the longest. In terms of stack operations like pushing and poping, you should always talk about the top or the bottom of the stack.ĭo enqueue and dequeue work the same as pop and push? 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. 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). I'm assuming push(30) would add 30 as the last number. 8) at the top of the stack, so it would now look like this: | 8 | <- topĭue 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. Obviously, using push(8) would add a new element (i.e. Here, pop would simply remove the element at the top and then leave top pointing to the element immediately below it (i.e. This means that your array could be viewed like this: | 15 | <- top 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. I think it'd be easier for you to understand the concepts if you associate the operations with specific data structures.įor 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. CALL FUNCTION 'ENQUEUEETABLE' EXPORTING MODERSTABLE WMODE TABNAME WTABLE VARKEY WVARKEY SCOPE '3' EXCEPTIONS FOREIGNLOCK 1 SYSTEMFAILURE 2. The performance is not second to his good solution.Pop() would just remove the first number, which is 15, right ? what if You can lock the table with 'ENQUEUEETABLE' and unlock with 'DEQUEUEETABLE'. I think this might be considered somewhat better since now you have enqueue, dequeue and peek as prototype inherited functions, you may add more to them as you wish and for some reason you may even extend this type. Perhaps we can do better with modern JS while still being in the realm of Chrome v74 or Node 12.0. This self answer is here just to show how we can use the Class abstraction to achieve a similar Queue implementation. fineProperty(this,"head", Test 500000 itemsĪfter thinkering a while, I really liked BlindMan67's answer except for the indentation part.
