python - Addition of data in queue -


i think of queue line person came @ first served first. other people join him @ back. wrote following implementation in python achieve that:

class queue:     def __init__(self):         self.item = []     def enqueue(self,value):         self.item.append(value)     def dequeue(self):         return self.item.pop(0)     def size(self):         return len(self.item)     def isempty(self):         return self.item == [] 

but when checked online tutorials, found following implementation queue:

class queue:     def __init__(self):         self.items = []      def isempty(self):         return self.items == []      def enqueue(self, item):         self.items.insert(0,item)      def dequeue(self):         return self.items.pop()      def size(self):         return len(self.items) 

according online implementation, item added first. intuition says item should added of list (appended) , removed front (remove first item). want know form correct, , if incorrect thing not understanding ?

either way works. agree intuition of adding on right , popping on left the underlying list in order of oldest-to-newest.

for worth, both implementations inefficient -- either inserting or popping @ position 0 o(n) operation.

see collections.deque() o(1) implementation.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -