0 Replies - 2595 Views - Last Post: 07 May 2009 - 07:56 PM

#1 Ed_Bighead   User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 178
  • Joined: 26-April 09

Stack and Queue

Posted 07 May 2009 - 07:56 PM

Description: If you obtain the apihelper module that can be found from Mark Pilgrim's 'Dive into Python' (http://diveintopython.org/toc/index.html), you can view the documentation nicely by uncommenting the statements at the end of the snippet as well as the import statement at the beginning. If anyone is interested, I've got a nice program that allows you to test each of the methods; send me a PM and I can get that to you.Basic stack and queue classes. I know there are other versions of these that have been submitted, but I felt the below snippet is well-documented and could be helpful to beginners learning to create classes.
"""This module contains two types of lists: Stacks
   and Queues. Stacks are LIFO and Queues are
   FIFO. Each class contains expected methods as
   well as additional methods."""
   
__author__ = "Chris Z"
__date__ = "12/01/2008"

#import the function info from module apihelper.py
#from apihelper import info

class Stack:
    "List of objects; Last In First Out basis"
    
    def __init__(self,sList = []):
        "Initialize the stack"
        self.sList = sList
        
    def push(self,obj):
        "Add obj to top of stack"
        self.sList.append(obj)
        return True
    
    def top(self):
        "Return topmost element"
        if self.isEmpty():
            return "Stack is empty"
        else:
            return self.sList[-1]
    
    def pop(self):
        "Return and remove topmost element"
        if self.isEmpty():
            return "Unable to pop: Stack is empty"
        else:
            return self.sList.pop()
    
    def isEmpty(self):
        "Return truthvalue of Stack's emptiness"
        return len(self.sList) == 0
    
    def isFull(self):
        "Return truthvalue of Stack's fullness"
        return False
    
    def clear(self):
        "Remove all elements from Stack"
        self.sList = []
        return True
    
    def length(self):
        "Returns the length of the Stack"
        return len(self.sList)
    
    def display(self):
        "Display the Stack"
        print self.sList
        return True

class Queue:
    "List of Objects; First In First Out basis"
    def __init__(self,qList = []):
        "Initialize the queue"
        self.qList = qList
    
    def enqueue(self,obj):
        "Add element to Queue"
        self.qList.append(obj)
        return True
    
    def dequeue(self):
        "Return and remove first element from Queue"
        if(self.isEmpty()):
            return "Unable to dequeue: Queue is empty"
        else:
            obj = self.qList[0]
            self.qList = self.qList[1:]
            return obj
    
    def isEmpty(self):
        "Return truthvalue of Queue's emptiness"
        return len(self.qList) == 0
    
    def isFull(self):
        "Return truthvalue of Queue's fullness"
        return False
    
    def clear(self):
        "Remove all elements from Queue"
        self.qList = []
        return True
    
    def putFront(self,obj):
        "Adds element to front of Queue"
        self.qList.insert(0,obj)
        return True
    
    def getRear(self):
        "Returns and removes the last element in the Queue"
        if self.isEmpty():
            return "Unable to getRear: Stack is empty"
        else:
            return self.qList.pop()
    
    def length(self):
        "Returns the length of the Queue"
        return len(self.qList)
    
    def display(self):
        "Display the Queue"
        print self.qList
        return True

#If this module is being run as a stand-alone program
if __name__ == "__main__":
    print __doc__
    print "nSTACK info:"
    print Stack.__doc__
    #info(Stack())
    print "nQUEUE info:"
    print Queue.__doc__
    #info(Queue())


Is This A Good Question/Topic? 0
  • +

Page 1 of 1