University of Arizona, Department of Computer Science

CSc 120: Queue

Expected Behavior

Implement the Queue ADT by using the built-in Python string type.

Note: Items added to this Queue ADT will consist only of strings of length one.

Attributes

The class has only one attribute, self.items, which will be a string representing the items currently in the queue.

Methods

Your class should implement the following methods:

__init__(self)
Initializes a new Queue object

enqueue(self,item)
Adds item to the back of the queue and returns None.

dequeue(self)
Removes the character at the front of the queue and returns it. Returns None if the queue is empty.

is_empty(self)
Returns True if the queue is empty and False otherwise.

__str__(self)
Returns a string representation of the queue.

Examples

Here are examples of making a queue, using it various ways, and printing the resulting queue:
  1. Test code 1:

        q = Queue()
        for c in "abcd":
            q.enqueue(c)
        print(q)
    
    Output: abcd

  2. Test code 2:

        q = Queue()
        for c in "abcd":
            q.enqueue(c)
        q.dequeue()
        print(q.dequeue())
    
    Output: b

  3. Test code 3:

        q = Queue()
        for c in "abcd":
            q.enqueue(c)
        q.dequeue()
        q.dequeue()
        print(q.is_empty())
    
    Output: False

  4. Test code 4:

    
        q = Queue()
        for c in "hide":
            q.enqueue(c)
        q.dequeue()
        q.dequeue()
        q.enqueue("e")
        q.enqueue("p")
        print(str(q))
    
    
    Output: deep