As FIFO data structure you could use next (with corresponding complexity):
list:append()amortized O(1) andpop(0)O(n)collections.deque-append()O(1) andpopleft()O(1)Queue.queue-get()O(1) andput()O(1) and etc. It is suitable for multi-threaded programming and based oncollections.dequeinternally.
As FIFO data structure you could use next (with corresponding complexity):
list:append()amortized O(1) andpop(0)O(n)collections.deque-append()O(1) andpopleft()O(1)Queue.queue-get()O(1) andput()O(1) and etc. It is suitable for multi-threaded programming and based oncollections.dequeinternally.
You can use a Queue
queue = Queue()
queue.put(0)
queue.put(1)
while not queue.empty():
print(queue.get())
Videos
x = collections.deque(5*[0], 5)
See the docs for more about collections.deque; the method you call push is actually called appendleft in that type.
The second parameter (maxlen, giving the maximum lengths) was added in Python 2.6; if you're using older versions of Python, it won't be available.
you can also use list
a = [0,0,0,0,0]
a.pop(0)
a.append(1)
print a
result [0,0,0,0,1]
or for left side in right out, otherwise
a.pop(5)
a.insert(0,1)
print a
result [1,0,0,0,0]
Apart from learning purposes I would not advise using a custom data structure for making a LIFO or FIFO. The built in data-type list is just fine after all.
You can add items using the append method and remove them using pop. For a LIFO this would look like this:
stack = list()
stack.append(1)
stack.append(2)
stack.append(3)
print stack.pop() #3
print stack.pop() #2
print stack.pop() #1
If you supply an integer argument for pop you can specify which element to remove. For a FIFO use the index 0 for the first element:
stack = list()
stack.append(1)
stack.append(2)
stack.append(3)
print stack.pop(0) #1
print stack.pop(0) #2
print stack.pop(0) #3
Well, seeing as your class is probably over now and you didn't mention your class (or that it had to be a linked list) in the question itself, I'll just tell you the built-in easy way to do it, for now, which is probably more pertinent to your current situation (and will help people who find your question).
import sys;
if sys.version_info[0]>2: #Just making sure the program works with both Python 2.x and 3.x
from queue import Queue
else:
from Queue import Queue
q=Queue()
q.put("first") #Put an item on the Queue.
q.put("second")
q.put("third")
while not q.empty(): #If it's empty, the program will stall if you try to get from it (that's why we're checking)
print(q.get()) #Get an item from the Queue
This outputs
first
second
third
Really, though, I'm not sure what advantages this has over Constantinius's answer, but since it's an included module, I would think there must be an advantage somewhere. I know they are used with threads from the threading module. There are more functions associated with Queues than I've mentioned here.
To learn more, open your Python interpreter and type this:
from queue import Queue #or from Queue import Queue for 2.x
help(Queue) #Press q to exit the help
Don't ask me what blocking is, but this may use the term how it's used in the Queue class documentation: http://en.wikipedia.org/wiki/Blocking_(computing)
read() doesn't return until it reaches EOF.
You can try specifying the number of bytes you want read, like read(4). This will still block until enough bytes have been written, so the producer must write at least that many bytes and then call flush().
To avoid the need for flushing, open the file without buffering:
fifo_read = open('fifo', 'r', 0)
That will remove high-level buffering. Data go to the OS directly and, being a fifo, they never get actually written to disk but passed straight to the reader thru the fifo buffer, so you don't need to sync.
Of course, you should have created the fifo first with os.mkfifo() or mkfifo at the shell, as you pointed in a comment.
You can try:
import stat, os
stat.S_ISFIFO(os.stat(path).st_mode)
docs
There is an is_fifo test in the build-in pathlib module.
As an example, run python - <(echo hello) to pass a fifo. Then execute the following snippet in the interactive interpreter
path = sys.argv[1]
from pathlib import Path
Path(path).is_fifo()
which outputs True. The path variable (fifo) should be something like '/dev/fd/63'.
os.path is lower level; pathlib is higher level.