Queue/Definition: Difference between revisions

From Rosetta Code
Content added Content deleted
(Start new task)
 
(Python)
Line 13: Line 13:


Define the data structure for a FIFO element. Said element should contain a data member capable of holding a numeric value, and the link to the next element should be mutable.
Define the data structure for a FIFO element. Said element should contain a data member capable of holding a numeric value, and the link to the next element should be mutable.

==[[Python]]==
[[Category:Python]]

Python 2.4 and later includes a [http://docs.python.org/lib/deque-objects.html deque class], supporting thread-safe, memory efficient appends and pops from either side of the deque with approximately the same O(1) performance in either direction.

from collections import deque
fifo = deque()
fifo.append(value) # push
value = fifo.pop()
not fifo # empty

Revision as of 20:36, 4 November 2007

Task
Queue/Definition
You are encouraged to solve this task according to the task description, using any language you may know.

Implement a FIFO queue. Elements are added at one side and popped from the other in the order of insertion.

Operations:

  • push - add element
  • pop - pop first element
  • empty - return truth value when empty

Errors:

  • handle the error of trying to pop from an empty queue (behavior depends on the language and platform)

Define the data structure for a FIFO element. Said element should contain a data member capable of holding a numeric value, and the link to the next element should be mutable.

Python

Python 2.4 and later includes a deque class, supporting thread-safe, memory efficient appends and pops from either side of the deque with approximately the same O(1) performance in either direction.

from collections import deque
fifo = deque()
fifo.append(value) # push
value = fifo.pop()
not fifo # empty