Circular Queue Python Implementation -
i have been trying make circular queue work time , thought i'd ask question , see if can hints. problem circular queue insert method returns value false when queue not full. here have far.
class circularqueue: def __init__(self, size): """ ------------------------------------------------------- initializes empty queue. data stored in list. ------------------------------------------------------- postconditions: initializes empty queue. ------------------------------------------------------- """ assert size>=0, "size must >=0" self._values = [none] * size self._front = 0 self._rear = 0 self._size = size self.size = 0 return def __len__(self): """ ------------------------------------------------------- returns size of queue. use: n = len( q ) ------------------------------------------------------- postconditions: returns number of values in 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 queue empty. use: b = q.is_empty() ------------------------------------------------------- postconditions: returns true if queue empty, false otherwise. ------------------------------------------------------- """ if len(self._values) == 0: empty = true else: empty = false return empty def insert(self, value): """ ------------------------------------------------------- inserts copy of value queue. use: q.insert( value ) ------------------------------------------------------- preconditions: value - data element (?) postconditions: value added rear of 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 , returns value queue. use: v = q.remove() ------------------------------------------------------- postconditions: returns value @ front of queue - value removed queue. returns none if queue 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 @ front of queue. use: v = q.peek() ------------------------------------------------------- postconditions: returns copy of value @ front of queue - value not removed queue. returns none if queue empty. ------------------------------------------------------- """ if self.is_empty() == true: value = none else: value = copy.deepcopy(self._values[self._front]) return value def print_i(self): """ ------------------------------------------------------- prints contents of queue front rear. use: q.print_i() ------------------------------------------------------- postconditions: prints each value in queue front rear. each value starts on new line. ------------------------------------------------------- """ in range(len(self._values)): print(self._values[i]) return
i started try , test have testing module.
from circularqueue import circularqueue q = circularqueue(10) value = q.insert(1) print(value)
all i'm trying here see if insert method return true because know fact queue not full because first value have tried enter. hints?
you initialize _front
, _rear
0, is_full
check returns true start.
Comments
Post a Comment