November 15th, 2004, 06:19 PM
Queue and Stack!
I have this homework to construct a queue and just wanted u guys to see and perhaps give me some tips on my way of creating a good queue.
First before I start i need to ask, to begin with, do I have to create a .py file with some "Class" and definitions functions in it. Then shall I create a queue.py and start making the queue system out of that? or? How do I start in making a queue system? oh man I'm getting so confused right now
November 16th, 2004, 02:53 AM
Assuming that you want to create the queue and stack as modules - which is probably best - then you would have two files: queue.py and stack.py layed out something like this.
You shouldn't have much problems with this since Python is so high level and it's data-types generally have everything you need built in . Check out the __builtin__ list type below.
class ImplementationName: #Queue or Stack.
def method1(): #Rename this ;).
if __name__ == '__main__':
...use the class here.
If you have any questions just ask,
November 16th, 2004, 05:45 AM
Wicked! Thanks mate! That site has good stuff in it!
I got one question, I've started out like this:
Everything is correct here when I run the program, but how do I make the program give out a return function of isEmpty(): so that it returns true if the queue is empty and false if it is not?
queue = ["Eric", "John", "Micheal"]
queue.append("Apa") # Apa Kommer
queue.append("Gooz") # Gooz kommer
queue.pop(0) # Eric Går
queue.pop(0) # John Går
November 16th, 2004, 04:30 PM
You could either write one yourself, for example:
q = [1,2,3]
q = 
or, if this is going to be object oriented, you could extend the built in list class and add an 'isEmpty' method to it, for example:
This is one of those programming examples that doesn't work very well with Python because, as you've found out, the built in lists also work like queues already. I'm going to guess though, that if you hand in just a demonstration of a list being a queue, that wont be quite what they are looking for.
>>> class Queue(list):
... # Inherit from the list class, and extend it with a new method
... def isEmpty(self):
... if self:
... return False
... return True
>>> q = Queue()
I would expect they want you to write the contents of "append" and "pop" yourself - though I might be wrong.
November 16th, 2004, 04:38 PM
Personally I would just add a method to my Queue class like this.
Here, self.queueValues is a instance variable that stores the values for the queue; of type list. If you post the program/class I would be able to give you a more exact answer .
return len(self.queueValues) > 0
November 16th, 2004, 05:47 PM
One of the Python mottos is "Batteries Included" - my implementation of a Queue would be:
this probably does far more than you need, since the Queue module in the Python library is designed for passing data between threads, and is fully thread aware.
from Queue import Queue
There is no built in Stack class, but lists have a pop method and an append method, so they can can be used as a stack with no modification if you restrict yourself to only using those methods.
This is probably not what your tutor wants as an answer, but if I was an employer I would be unhappy if a developer wasted time implementing classes that were already included in the language.
Dave - The Developers' Coach