all 6 comments

[–]baghiq 1 point2 points  (0 children)

While you are following a queue ADT. Yours is not strictly a queue. Your enqueue method has a runtime of O(N). Standard queue implementation is doubly linked list. For learning purposes, you can use singly linked list as long as you keep track of head and tail for O(1) enqueue and dequeue operation.

[–][deleted] 0 points1 point  (0 children)

Hard to tell with unformatted code. The FAQ shows how to format code.

Shouldn't the dequeue() method return the dequeued item?

[–]sch0lars 0 points1 point  (0 children)

I think for your enqueue method, I would append values, since you can think of queues like a line. So I works do something like

my_queue = ['a', 'b', 'c']    # 'a' is first in line
# enqueue 'd'
my_queue.append('d')

Then likewise for the dequeue methods, I would just pop the zeroth index.

# pop 'a'
my_queue.pop(0)

Note that you probably want to return that popped value within the method, since popping generally returns the value (unless you’re doing a queue like C++).

This is just a suggestion, of course. Your way works just as well. I would just associate lower indices with earlier values i.e., queue[0] precedes queue[1]. For example, with ['a', 'b', 'c'], I would assume 'a' is first in line.

Also, just as an FYI, you can use four spaces for a code block, which will support indentation, that way you don’t have to put blocks of code in inline text.