2

I have been trying to make my circular queue work for some time now and just thought I'd ask a question and see if I can get any hints. The problem with my circular queue is that the insert method always returns the value False even when the queue is not full. Here is what I have so far.

class CircularQueue:

    def __init__(self, size):
        """
        -------------------------------------------------------
        Initializes an empty queue. Data is stored in a list.
        -------------------------------------------------------
        Postconditions:
          Initializes an empty queue.
        -------------------------------------------------------
        """
        assert size>=0, "size must be >=0"
        self._values = [None] * size
        self._front = 0
        self._rear = 0
        self._size = size
        self.size = 0

        return 

    def __len__(self):
        """
        -------------------------------------------------------
        Returns the size of the queue.
        Use: n = len( q )
        -------------------------------------------------------
        Postconditions:
          Returns the number of values in the queue.
        -------------------------------------------------------
        """
        return len(self._values())


  def is_full(self):
          value = self._rear - self._size
          if self._values[value]==None:
                 full = False
    else:
        full = True
    return full
    def is_empty(self):
        """
        -------------------------------------------------------
        Determines if the queue is empty.
        Use: b = q.is_empty()
        -------------------------------------------------------
        Postconditions:
          Returns True if the queue is empty, False otherwise.
        -------------------------------------------------------
        """
        if len(self._values) == 0:
            empty = True
        else:
            empty = False
        return empty

    def insert(self, value):
        """
        -------------------------------------------------------
        Inserts a copy of value into the queue.
        Use: q.insert( value )
        -------------------------------------------------------
        Preconditions:
          value - a data element (?)
        Postconditions:
          value is added to the rear of the queue.
        -------------------------------------------------------
        """
        if self.is_full() == True:
            inserted = False
        else:
            inserted = True
            self._values[self._rear] = value
            self._rear = (self._rear + 1)% self._size

        return inserted

    def remove( self ):
        """
        -------------------------------------------------------
        Removes and returns value from the queue.
        Use: v = q.remove()
        -------------------------------------------------------
        Postconditions:
          Returns the value at the front of queue - the value is
          removed from queue. Returns None if queue is empty.
        -------------------------------------------------------
        """
        if self.is_empty() == False:
            value = copy.deepcopy(self._values[self._front])
            self._front = (self._front + 1) % self._size
        else:
            value = None

        return value

    def peek(self):
        """
        -------------------------------------------------------
        Peeks at the front of queue.
        Use: v = q.peek()
        -------------------------------------------------------
        Postconditions:
          Returns a copy of the value at the front of queue -
          the value is not removed from queue. Returns None
          if queue is empty.
        -------------------------------------------------------
        """

        if self.is_empty() == True:
            value = None
        else:
            value = copy.deepcopy(self._values[self._front])

        return value

    def print_i(self):
        """
        -------------------------------------------------------
        Prints the contents of queue from front to rear.
        Use: q.print_i()
        -------------------------------------------------------
        Postconditions:
          Prints each value in queue from front to rear.
          Each value starts on a new line.
        -------------------------------------------------------
        """
        for i in range(len(self._values)):
            print(self._values[i])
        return

I just started to try and test it so this is all I have for my testing module.

from circularqueue import CircularQueue

q = CircularQueue(10)

value = q.insert(1)

print(value)

All I'm trying to do here is see if my insert method will return True because I know for a fact that the queue is not full because that is the very first value that I have tried to enter. Any hints?

3
  • 1
    If you don't have to implement this yourself, you can use the standard library deque . Commented Jun 4, 2015 at 4:47
  • There's no way insert returns None. Commented Jun 4, 2015 at 4:50
  • sorry i meant insert returns False...my fault sorry. Commented Jun 4, 2015 at 4:52

1 Answer 1

1

You initialize _front and _rear to 0, so your is_full check returns True from the start.

Sign up to request clarification or add additional context in comments.

8 Comments

I feel like that is the problem, now to figure out how to change my is_full method.
Draw out a quick example with a queue of size 3. Insert 3 elements. Remove 1. Insert 1. It should be easy to figure out the is_full condition from there.
insert 1, 2, 3: q = [1,2,3] remove1: q= [none, 2, 3] insert1: q=[2,3,1] so i'm guessing that if self._front=0 and self._rear=self._size: q is full else: q is not full?
Wouldn't your last insert give you q = [1,2,3], where _front = 1, _rear = 0?
So have you come up with a is_full test condition?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.