Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Class implementation

$
0
0
I have created this class
class Queue:
    # Constructor, which creates a new empty queue:
    def __init__(self):
        self.__items=[]
        
    # Adds a new item to the back of the queue, and returns nothing:
    def queue(self, item):
        self.items.append(item)
        
    # Removes and returns the front-most item in the queue.  
    # Returns nothing if the queue is empty.
    def dequeue(self):
        return self.__items.pop()
    
    # Returns the front-most item in the queue, and DOES NOT change the queue.  
    def peek(self):
        return self.__items[len(self.__items)-1]
        
    # Returns True if the queue is empty, and False otherwise:
    def is_empty(self):
        return len(self.__items) == 0
    
    # Returns the number of items in the queue:
    def size(self):
        return len(self.__items)
    
    # Removes all items from thq queue, and sets the size to 0:
    def clear(self):
        self.__items.pop()
        return len(self.__items) == 0
    
    # Returns a string representation of the queue
    def __str__(self):
        # TODO: You must implement this method!


Can someone take a look and let me know if my work makes sense??
Thank
Madeline.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>