![]() ![]() For instance, if there are three elements in the queue then Head = 0, Tail = 4. Use a Reset method - this method is called to reset the queue, Tail and Head.This means that the position 0 will always contain an element, which is not how an actual queue works. This is because once an element is removed, the list automatically shifts all the other elements by one position to the left. No element will be actually removed from the queue. After one dequeue operation, the Head pointer will point to the second element in the queue. For instance, if there are three elements in a queue, the Head pointer will be pointing to the first element. Use a Head pointer to keep a tab on the elements removed from the Queue - the Head pointer will point to the element to be dequeued next.When the queue is full, the tail pointer will be greater than the declared size. ![]() For instance when there are three elements in the queue, Tail will point to the fourth place. Use a Tail pointer to keep a tab of the elements added to the Queue - the Tail pointer will always point to the next available space.This is done by ensuring that the list doesn't grow beyond a fixed limit or size. Treat lists like arrays (fixed in size) - we can achieve it by virtually restricting the size of the list.Elements are added from the end and removed at the beginning of the Queue.However, if you want to implement Queues language agnostically, you have to bear in mind the following points: Python Lists have made it so easy to implement Queue. We will discuss the efficiency of operations in detail in later tutorials. ![]() NOTE: Implementation 2 is more efficient as the insert operation in the list is costly! This is because whenever an element is inserted at position 0, all of the other elements have to be shifted by one position (it is similar to when people sitting on a bench push down to accommodate another person). Post the ways in which you can handle this issue. Try using the popleft() command after the queue is empty and see what you get. The reason for this is covered in the 'implementation using arrays' section below.ĭeque is a library which, when imported, provides ready-made commands such as the append() command for enqueuing and the popleft() command for dequeuing. Note: You will notice that we are not removing elements from the beginning and adding elements at the end. Print(queue()) #prints Queue Empty!Ĭall the method printQueue() wherever necessary to ensure that it's working. #Removing the last element from the queue #Checking to avoid duplicate entry (not mandatory)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |