Looking to test my library and implementing it so that insert function can assign the index of the rear in th e ring after the items are inserted in ring

class CircularQueue:

    def __init__(self, size):
        """
        -------------------------------------------------------
        Initializes an empty queue. Data is stored in a list.
        -------------------------------------------------------
        Postconditions:
          Initializes an empty queue.
        -------------------------------------------------------
        """
        self._values = [None] * size

        return

    def is_empty(self):
        """
        -------------------------------------------------------
        Determines if the list is empty.
        Use: b = l.is_empty()
        -------------------------------------------------------
        Postconditions:
          Returns True if the list is empty, False otherwise.
        -------------------------------------------------------
        """
        return len(self._values) == 0

    def insert(self, value):
        """
        -------------------------------------------------------
        Inserts a copy of value into list.
        Use: l.insert( value )
        -------------------------------------------------------
        Preconditions:
          value - a data element (?)
        Postconditions:
          value is added to the list.
        -------------------------------------------------------
        """
        length_circ = len(self._values)

        rear = 0



        self._values

        return

    def remove(self, key):
        """
        -------------------------------------------------------
        Finds, removes, and returns the value in list that matches key.
        Use: value = l.remove( key )
        -------------------------------------------------------
        Preconditions:
          key - a data element (?)
        Postconditions:
          Returns and removes the full value matching key, otherwise
          returns None.
        -------------------------------------------------------
        """

        index_found = self._linear_search(key)

        if index_found == -1:
            value = None

        else:

            value = self._values.remove(index_found)

        return value

    def peek(self):
        """
        -------------------------------------------------------
        Finds, removes, and returns the value in list that matches key.
        Use: value = l.remove( key )
        -------------------------------------------------------
        Preconditions:
          key - a data element (?)
        Postconditions:
          Returns and removes the full value matching key, otherwise
          returns None.
        -------------------------------------------------------
        """

        index_found = self._linear_search(key)

        if index_found == -1:
            value = None

        else:

            value = self._values.remove(index_found)

        return value

    def is_full(self):
        """
        -------------------------------------------------------
        Determines if the list is empty.
        Use: b = l.is_empty()
        -------------------------------------------------------
        Postconditions:
          Returns True if the list is empty, False otherwise.
        -------------------------------------------------------
        """
        return len(self._values) == 0

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

Edited 2 Years Ago by pyTony: Edited wrron formatting

Please read a basic python tutorial, write down in normal words what you expect form this class, and repost your code.

Issues:

  • The some method's naming contradict the methods documentation (is_empty, is_full)
  • Different methods with the same code.
  • Semantically problematic code (insert)
  • I do not understand the text (what is e ring?)

Circular queues confuse a lot of programmers since they are not generally supported directly by computer languages. Key points: head of queue, and tail of queue. Circular queues are generally organized as an array, and the biggest issue is usually the one of "wrapping around" from head to tail, or tail to head (depending upon direction of navigation). In any case, python not-withstanding, write out the pseudo code (algorithms) on how to deal with adding/removing elements, and navigating the queue.

Edited 2 Years Ago by rubberman

Yes, considering how you initialize the circular queue, it does not agree with your definition of is_empty, for example.

Nothing in your implementation looks like circular queue for me, I do not see head pointer, for example.

Actually circular queue does not make much sense for me, when Python has lists and also Queue etc classes in modules.

Edited 2 Years Ago by pyTony

This article has been dead for over six months. Start a new discussion instead.