Queue Class

Your task is to create a Queue class in Python that uses a list as its internal data structure. This class should provide the following methods:
  • enqueue(): This method takes an input element and adds it to the end of the queue.
  • dequeue(): This method removes an element from the front of the queue and returns it.
  • size(): This method returns the current number of elements in the queue.
Please adhere to the following specifications:
  • The queue should be implemented using a list. This means that the list should not be directly accessible from outside the class.
  • The enqueue method should take one parameter, the element to be added to the queue. It should not return anything.
  • The dequeue method should not take any parameters. It should return the element that was removed from the queue. If the queue is empty when dequeue is called, it should return None.
  • The size method should not take any parameters. It should return an integer representing the number of elements currently in the queue.
Input
Output
q = Queue(); q.enqueue('a'); q.enqueue('b'); print(q.size()); print(q.dequeue()); print(q.dequeue()); print(q.dequeue())
2 a b None
 

Constraints

Time limit: 2 seconds

Memory limit: 512 MB

Output limit: 1 MB

To check your solution you need to sign in
Sign in to continue