Remember the first-come-first-out sequence? When we execute the dequeue operation, it will first remove 5 from the queue. So the back arrow followed the items in the order they were added all the way to the last. We will go on and fill up the array so we can see what happens when we dequeue. But now that we have enqueued more items, the back will keep following the last item. Since that was the first and only item then, the front and back sat at that position. The front and back moved together in the last example so that the front could assume the position of the first item. This will continue as we enqueue more items. Next, we will see what happens as we enqueue more itemsĪ second item has been added but only the back moved. The position of the front and back have moved as well. We have inserted (enqueued) our first item – 5. Let us add some items into our array and see what happens. When both positions are at -1, it means the array is empty. The arrows show the position of the front and back of our array. This will enable us move the front and back position properly as values are added. While implementing our code, we are going to set the index of the front and back values of our array to -1. The indices of arrays in most programming languages start from 0.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |