Implementing an efficient queue in Python -
i have been trying implement queue in python, , i've been running problem.
i attempting use lists implement queue data structure, can't quite figure out how make enqueue
, dequeue
o(1) operations.
every example have seen online, seems append enqueue
operation , remove first element list dequeue
operation. make dequeue
operation o(n) (where n size of list) correct?
is there basic have missed? or have use linkedlists implement queue efficiently?
import unittest class queue(): def __init__(self): self._queue = [] self.size = 0 self.maxsize = 10 def enqueue(self, item): if self.size < self.maxsize: self._queue.append(item) def dequeue(self): ''' removes item front of list. remove first element of array ''' first = self._queue[0] del self._queue[0] return first
Comments
Post a Comment