Throw std ::runtime_error( "Failed to get the front element because the queue is empty.") We also have to ensure that we free the old array because it’s not used anymore, and update the head and the tail to match the new layout. Secondly, append the elements in [0, tail_index) to the new array. When the head_index tail_index, move the elements in two stages.To move the elements, we distinguish between two cases: To change the capacity of the array, we create a new array with the desired capacity, then move the elements from the current array to the new one. An alternative is to create a bigger array and move the elements there, then add the new element, which is what most libraries do. If there is no more space in the underlying array, which we check by comparing the size and the capacity, we raise an error. Tail_index_ = (tail_index_ + 1) % capacity_ Throw std ::runtime_error( "Failed to enqueue an element because the queue is full.") The head points to the first element in the queue, while the tail points to the slot after the last element in the queue. So, we need to know the queue’s beginning and end. The queue data will be stored in the array, but not every slot will have an element. the total number of slots in the array as capacity.the number of elements in the queue as size, and.To distinguish between the two, we will refer to: Note that the queue size is different from the underlying array size. We can use an array of size N to efficiently implement the queue data structure that can store up to N elements. ![]() When working with queues, the insert, delete, and read operations are usually called enqueue, dequeue, and front. We call this property first-in, first out (FIFO). The customer who has been in the line for the longest time is the first to leave.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |