FIFO dequeueing in python?

Posted by Aaron Ramsey on Stack Overflow See other posts from Stack Overflow or by Aaron Ramsey
Published on 2013-11-05T21:50:04Z Indexed on 2013/11/05 21:53 UTC
Read the original article Hit count: 278

Filed under:
|

hello again everybody—

I'm looking to make a functional (not necessarily optimally efficient, as I'm very new to programming) FIFO queue, and am having trouble with my dequeueing.

My code looks like this:

class QueueNode:
    def __init__(self, data):
        self.data = data
        self.next = None 

    def __str__(self):
        return str(self.data) 

class Queue:
    def__init__(self):
        self.front = None
        self.rear = None
        self.size = 0

    def enqueue(self, item)
        newnode = QueueNode(item)
        newnode.next = None
        if self.size == 0:
            self.front = self.rear = newnode
        else:
            self.rear = newnode
            self.rear.next = newnode.next
        self.size = self.size+1

    def dequeue(self)
        dequeued = self.front.data
        del self.front
        self.size = self.size-1
        if self.size == 0:
            self.rear = None
        print self.front #for testing

if I do this, and dequeue an item, I get the error "AttributeError: Queue instance has no attribute 'front'." I guess my function doesn't properly assign the new front of the queue? I'm not sure how to fix it though.

I don't really want to start from scratch, so if there's a tweak to my code that would work, I'd prefer that—I'm not trying to minimize runtime so much as just get a feel for classes and things of that nature.

Thanks in advance for any help, you guys are the best.

© Stack Overflow or respective owner

Related posts about python

Related posts about fifo