3. Define a function named stackToQueue. This function expects a stack as an argument. The function builds and returns an instance of LinkedQueue that contains the items in the stack. The function assumes that the stack has the interface described in Chapter 7, "Stacks." The function's postconditions are that the stack is left in the same state as it was before the function was called, and that the queue's front item is the one at the top of the stack.   Use this Python template: class Queue: ''' TODO: Remove the "pass" statements and implement each method Add any methods if necesssary DON'T use any builtin queue class to store your items ''' def __init__(self): # Constructor function pass def isEmpty(self): # Returns True if the queue is empty or False otherwise pass def len(self): # Returns the number of items in the queue pass def peek(self): # Returns the item at the front of the queue pass def add(self, item): # Adds item to the rear of the queue pass def pop(self): # Removes and returns the item at the front of the queue pass def remove(self, index): # Removes and returns the item at index of the queue pass def stackToQueue(stack): '''input stack will be in the form of a list. Implement your LinkedQueue. We will pop() the elements and compare with the LIFO order of the stack ''' #TODO (optional): Your testing code here queue = Queue() return queue if __name__ == "__main__": stack = [1,2,3] res=stackToQueue(stack) print(res.pop()) #the autograder will use your Queue's pop() function, append to a list and compare with initial stack. ''' Correct output of res would be a LinkedQueue in the order 3, 2, 1 '''

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter17: Linked Lists
Section: Chapter Questions
Problem 18SA
icon
Related questions
icon
Concept explainers
Question

3. Define a function named stackToQueue. This function expects a stack as an argument. The function builds and returns an instance of LinkedQueue that contains the items in the stack. The function assumes that the stack has the interface described in Chapter 7, "Stacks." The function's postconditions are that the stack is left in the same state as it was before the function was called, and that the queue's front item is the one at the top of the stack.

 

Use this Python template:

class Queue:
'''
TODO: Remove the "pass" statements and implement each method
Add any methods if necesssary
DON'T use any builtin queue class to store your items
'''
def __init__(self): # Constructor function
pass
def isEmpty(self): # Returns True if the queue is empty or False otherwise
pass
def len(self): # Returns the number of items in the queue
pass
def peek(self): # Returns the item at the front of the queue
pass
def add(self, item): # Adds item to the rear of the queue
pass
def pop(self): # Removes and returns the item at the front of the queue
pass
def remove(self, index): # Removes and returns the item at index of the queue
pass
def stackToQueue(stack):
'''input stack will be in the form of a list. Implement your LinkedQueue.
We will pop() the elements and compare with the LIFO order of the stack
'''
#TODO (optional): Your testing code here
queue = Queue()
return queue
if __name__ == "__main__":
stack = [1,2,3]
res=stackToQueue(stack)
print(res.pop()) #the autograder will use your Queue's pop() function, append
to a list and compare with initial stack.
'''
Correct output of res would be a LinkedQueue in the order 3, 2, 1
'''

 

Expert Solution
Algorithm

stackToQueue(stack):
    1. Create an empty Queue instance.
    2. Make a copy of the input stack to preserve its state.
    3. While the copy of the stack is not empty:
        a. Pop an item from the copy of the stack.
        b. Add the popped item to the rear of the queue.
    4. Return the resulting queue.

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

It keep saying that 'Queue' object has no attribute 'len' and I can't change the filename

Solution
Bartleby Expert
SEE SOLUTION
Follow-up Question

It keep saying that 'Queue' object has no attribute 'len'

Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Types of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning