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 '''

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education