#๐Ÿ”’ Circular queues

11 messages ยท Page 1 of 1 (latest)

sullen lichen
#

I get the concept of circular queues but it partially works when I tried to make it in Python.

ancient pewterBOT
#

@sullen lichen

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.

sullen lichen
#

not sure how to make it colour coded

solemn cipher
#

!code

ancient pewterBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

sullen lichen
#
#############################################################################
# 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())

sullen lichen
#

by using the self.print_queue() method i can have a look at my queue and it works fine, however i do not get the expected output

ancient pewterBOT
#
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.