def test_inf_empty_queue(self): queue = LinkedListQueue() assert queue.is_empty() == True assert queue.is_full() == False assert queue.capacity == float('inf') assert queue.__repr__() == 'LinkedListQueue: front [] tail' assert len(queue) == 0 with pytest.raises(EmptyError): queue.front() with pytest.raises(EmptyError): queue.dequeue()
def breadthfirst(self): if not self.is_empty(): q = LinkedListQueue() q.enqueue(self.root()) while not q.is_empty(): p = q.dequeue() yield p for c in self.children(p): q.enqueue(c)
class EarliestUsageCache(object): def __init__(self, capacity=1000): self.capacity = capacity self.size = 0 self.keyQueue = LinkedListQueue() self.cache = dict() def add(self, key, value): if (not self.contains(key)): if (self.isFull()): self.__evict() self.keyQueue.enqueue(key) self.size += 1 self.cache[key] = value def get(self, key): if (not self.contains(key)): raise ValueError("%s is not a key in this cache" % key) return self.cache[key] def contains(self, key): return key in self.cache def isFull(self): return self.size == self.capacity def clear(self): self.size = 0 self.keyQueue = LinkedListQueue() self.cache = dict() def __evict(self): key = self.keyQueue.dequeue() self.cache.pop(key) self.size -= 1
#back of queue at tail '''ll_queue = LinkedList() #enqueue ll_queue.append(40) ll_queue.append(49) ll_queue.append(50) #ll_queue.print_list() #front #print(ll_queue.head.data) #dequeue ll_queue.delete_from_head() print(ll_queue.head.data)''' ll_queue = LinkedListQueue() ll_queue.enqueue(6) ll_queue.enqueue(20) ll_queue.enqueue(16) ll_queue.enqueue(5) ll_queue.dequeue() print(ll_queue.front())
for i in range(TEST_NUM // 2): deque.pop_front() end = time.time() return end - start if __name__ == '__main__': print('TEST_NUM: {}'.format(TEST_NUM)) queue = ArrayQueue(TEST_NUM) print('ArrayQueue, time: {:.3f} s'.format(queue_exec_time(queue))) queue = LinkedListQueue(TEST_NUM) print('LinkedListQueue, time: {:.3f} s'.format(queue_exec_time(queue))) queue = ArrayLoopQueue(TEST_NUM) print('ArrayLoopQueue, time: {:.3f} s'.format(queue_exec_time(queue))) queue = LinkedListLoopQueue(TEST_NUM) print('LinkedListLoopQueue, time: {:.3f} s'.format(queue_exec_time(queue))) deque = ArrayDeque(TEST_NUM) print('ArrayDeque, time: {:.3f} s'.format(deque_exec_time(deque))) deque = LinkedListDeque(TEST_NUM) print('LinkedListDeque, time: {:.3f} s'.format(deque_exec_time(deque))) deque = ArrayLoopDeque(TEST_NUM)
def clear(self): self.size = 0 self.keyQueue = LinkedListQueue() self.cache = dict()
def __init__(self, capacity=1000): self.capacity = capacity self.size = 0 self.keyQueue = LinkedListQueue() self.cache = dict()
def test_fix_len_queue(self): queue = LinkedListQueue(3) queue.enqueue(1) queue.enqueue(2) assert queue.is_empty() == False assert queue.is_full() == False assert queue.capacity == 3 assert queue.__repr__() == 'LinkedListQueue: front [1,2] tail' assert len(queue) == 2 assert queue.front() == 1 queue.enqueue(3) assert queue.is_empty() == False assert queue.is_full() == True assert queue.capacity == 3 assert queue.__repr__() == 'LinkedListQueue: front [1,2,3] tail' assert len(queue) == 3 assert queue.front() == 1 with pytest.raises(FullError): queue.enqueue(4) assert queue.dequeue() == 1 assert queue.is_empty() == False assert queue.is_full() == False assert queue.capacity == 3 assert queue.__repr__() == 'LinkedListQueue: front [2,3] tail' assert len(queue) == 2 assert queue.front() == 2
def test_fix_len_empty_queue(self): queue = LinkedListQueue(0) assert queue.is_empty() == True assert queue.is_full() == True assert queue.capacity == 0 assert queue.__repr__() == 'LinkedListQueue: front [] tail' assert len(queue) == 0 with pytest.raises(EmptyError): queue.front() with pytest.raises(EmptyError): queue.dequeue() with pytest.raises(FullError): queue.enqueue(1) queue = LinkedListQueue(3) assert queue.is_empty() == True assert queue.is_full() == False assert queue.capacity == 3 assert queue.__repr__() == 'LinkedListQueue: front [] tail' assert len(queue) == 0 with pytest.raises(EmptyError): queue.front() with pytest.raises(EmptyError): queue.dequeue()
def main(): print("Creating a new queue...") q = LinkedListQueue() print("length of the queue:", len(q)) print("The queue is empty:", q.is_empty()) print() print("Adding 10 to the queue...") q.enqueue(10) print("The front element is", q.first()) print("length of the queue:", len(q)) print("The queue is empty:", q.is_empty()) print() print("Adding 20 30 40 50 in this order...") q.enqueue(20) q.enqueue(30) q.enqueue(40) q.enqueue(50) print("The front element is", q.first()) print("length of the queue:", len(q)) print("The queue is empty:", q.is_empty()) print() print("Dequeue 3 times...") for i in range(3): q.dequeue() print("The front element is", q.first()) print("length of the queue:", len(q)) print("The queue is empty:", q.is_empty()) print() print("Adding 10 elements...") for i in range(10): q.enqueue(i) print("The front element is", q.first()) print("length of the queue:", len(q)) print("The queue is empty:", q.is_empty()) print()