#๐Ÿ”’ help with circular queues

10 messages ยท Page 1 of 1 (latest)

turbid lily
#

i can have a look at my queue and it works fine, however i do not get the expected output

fallow scrollBOT
#

@turbid lily

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

turbid lily
#
#############################################################################
# Queue class implemented with an array
class Queue:
    # Constructor
    def __init__(self):
        self.FrontPointer = 0
        self.BackPointer = -1
        self.Max = 5
        self.Count = 0
        self.Contents = ["" for Elements in range(self.Max)]

    # Add an item to the queue
    def Enqueue(self, Item):

        # ENTER YOUR CODE HERE
        if self.Count < self.Max - 1:
          self.BackPointer = (self.BackPointer + 1) % self.Max
          self.Contents[self.BackPointer] = Item
          self.Count = self.Count + 1
          self.print_queue()
          return True
        else:
          return False

    # Remove an item from the queue
    def Dequeue(self):

        # ENTER YOUR CODE HERE
        if self.Count > 0:
          Item = self.Contents[self.FrontPointer]
          self.FrontPointer = (self.FrontPointer + 1) % self.Max
          self.Count = self.Count - 1
          self.print_queue()
          return Item
        else:
          return None


    # Look at the next item in the queue without removing it
    def Peek(self):

        # ENTER YOUR CODE HERE
        if self.FrontPointer <= self.BackPointer:
          Item = self.Contents[self.FrontPointer]
          return Item
        else:
          return None
    def print_queue(self):
        print("Queue:", self.Contents[self.FrontPointer:self.FrontPointer + self.Count])


#############################################################################
# Main program starts here

# Subroutine to output the contents of a queue
def ClearQueue(InputQueue):
    Item = InputQueue.Peek()
    while Item:
        Item = InputQueue.Dequeue()
        if Item:
            print(Item)

#
# How to create a new queue (you can have as many queue objects as you want using lists)
MyQueue = Queue()

#How to add to the queue (returns True on success or False on queue overflow)
Action = MyQueue.Enqueue("Craig")
Action = MyQueue.Enqueue("Dave")
Action = MyQueue.Enqueue("Mark")
Action = MyQueue.Enqueue("Sam")
print(MyQueue.Dequeue())
print(MyQueue.Dequeue())
Action = MyQueue.Enqueue("Andy")
Action = MyQueue.Enqueue("Carol")
print(MyQueue.Dequeue())
print(MyQueue.Dequeue())
print(MyQueue.Dequeue())

# How to remove from the queue (queue underflow returns None)
#print(MyQueue.Dequeue())

# Output the entire stack using the ClearQueue subroutine
#ClearQueue(MyQueue)
print(MyQueue.Peek())

#

i know how they work just not how to solve this small issue

#

afk (no one will reply by the time im back probably)

subtle edge
#

@turbid lily your other thread, #1250116141729185813, is still open

#

!close

fallow scrollBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.