0
  • Accepts items from the user and create a queue till the user enters -1
  • Print the queue
  • Reverse the given queue
  • Print the modified queue

for example the input may be

  • 1
  • 2
  • 3
  • 4
  • -1

the only thing I cannot get working is the logic around reversing the queue only using enqueue and dequeue and obviously my attempt logic is completely wrong, I'm stuck as every online page just uses a stack however I cannot use a stack.

from Queue import Queue

def reverseQueueFirstKElements(k, queue):
    for i in range(k):
        if queue is None:
            return
        temp =  queue.dequeue()
        queue.enqueue(temp)
        node = queue.list.head
        print(node.data)
        

if __name__ == '__main__':
    queue = Queue()
    nums = 0
    k = 0
    while nums != -1:
        nums = int(input())
        if nums == -1:
            break
        else:   
            queue.enqueue(nums)
        k += 1
    node = queue.list.head
    while node is not None:
        print(node.data)
        node = node.next
    reverseQueueFirstKElements(k, queue)

here is the Queue file

from Node import Node
from LinkedList import LinkedList

class Queue:
    def __init__(self):
        self.list = LinkedList()
        
    def enqueue(self, new_item):
        # Create a new node to hold the item
        new_node = Node(new_item)
        
        # Insert as list tail (end of queue)
        self.list.append(new_node)
    
    def dequeue(self):
        # Copy data from list's head node (queue's front node)
        dequeued_item = self.list.head.data
        
        # Remove list head
        self.list.remove_after(None)
        
        # Return the dequeued item
        return dequeued_item
  • Added the `Queue` file was an improvement, but how about `Node` and `LinkedList`? Do you see where this is going? The ideas is to provide a [mre] (MRE) that others can use to answer your question. – martineau Aug 10 '21 at 12:46
  • use recursion for reversion, count k down and queue the element on the way up again. I'm no good in python, you can have java or pseudocode if you wan't... – Turo Aug 10 '21 at 12:54
  • You say that every online page just uses a stack but you cannot use one. How about a list? You can use your own or Python's own built-in `list` to do it. – martineau Aug 10 '21 at 12:56

2 Answers2

0

I dont't know if this is what you where searching for, but the deque module has a .reverse() function wich automatically reverses the order of the entire list. more inforamtion can be found here: https://docs.python.org/3/library/collections.html#collections.deque.reverse

0

You cannot do this in a loop as you tried, because this just does not change the order... it just rotates the queue only to end up (almost) the same as you started.

The way to do this, is using a stack: flush the queue unto a stack, and then flush the stack back into the queue.

Now, you'll say you are not supposed to use a stack, but you can use the call stack for this purpose, and use recursion:

def reverse(queue):
    try:
        data = queue.dequeue()
    except AttributeError:
        return queue
    reverse(queue)
    queue.enqueue(data)

Note that I did away with k, since you want anyway to reverse the whole queue.

The base case of this algorithm is when the queue is empty. In that case calling your dequeue implementation will trigger an exception on accessing head.data, since the head member will be None at that time. This function traps that error, and returns the empty queue. Then the recursion unwinds, and all values get enqueued again in opposite order.

trincot
  • 317,000
  • 35
  • 244
  • 286
  • okay this seems on the right track, I am new to python so how would you then print this? I used while node is not None: print(node.data) node = node.next inside the reverse function and I get the output 1 2 3 4 4 4 3 4 3 2 4 3 2 1 – Mitch Callahan Aug 11 '21 at 00:15
  • edit: I printed using revers = reverse(queue).list.head while revers is not None: print(revers.data) revers = revers.next. inside the if __name__ == '__main__': – Mitch Callahan Aug 11 '21 at 00:28
  • You should just print `queue` like you would do before reversing. If you have trouble with printing a queue / linkedlist, then ask a separate question about that, showing what the problem is you encounter. – trincot Aug 11 '21 at 07:59