#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Sweden
    Posts
    20
    Rep Power
    0

    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
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.

    Code:
    #!/usr/bin/env python
    
    class ImplementationName: #Queue or Stack.
        def method1():              #Rename this ;).
            ...
        ...                                #etc.
    
    if __name__ == '__main__':
        ...use the class here.
    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.

    http://www.python.org/doc/2.3.4/lib/typesseq-mutable.html

    If you have any questions just ask,

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Sweden
    Posts
    20
    Rep Power
    0
    Wicked! Thanks mate! That site has good stuff in it!

    I got one question, I've started out like this:

    Code:
    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
    queue
    
    print queue
    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?

    Thanks,
    Micheal
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    You could either write one yourself, for example:

    Code:
    def isEmpty(queue):
        if queue:
            return False
        else:
            return True    
    
    q = [1,2,3]
    isEmpty(q)
    
    q = []
    isEmpty(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:

    Code:
    >>> class Queue(list):
    ...     # Inherit from the list class, and extend it with a new method
    ... 	def isEmpty(self):
    ... 		if self:
    ... 			return False
    ... 		else:
    ... 			return True
    ... 
    >>>		
    >>> q = Queue()
    >>> q.append(1)
    >>> q.append(3)
    >>> q
    [1, 3]
    >>> q.isEmpty()
    False
    >>> q.pop()
    3
    >>> q.pop()
    1
    >>> q.isEmpty()
    True
    >>>
    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.

    I would expect they want you to write the contents of "append" and "pop" yourself - though I might be wrong.
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Personally I would just add a method to my Queue class like this.

    Code:
    def isEmpty():
        return len(self.queueValues) > 0
    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 .

    Mark.
    programming language development: www.netytan.com Hula

  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    One of the Python mottos is "Batteries Included" - my implementation of a Queue would be:

    Code:
    from Queue import Queue
    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.

    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

IMN logo majestic logo threadwatch logo seochat tools logo