Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

this should be an easy python answer: my assignment is this: here's the work i have: class Queue: def __init__(self): # Constructor function self.items =

this should be an easy python answer: my assignment is this: image text in transcribed here's the work i have:  class Queue: def __init__(self): # Constructor function self.items = [] def isEmpty(self): # Returns True if the queue is empty or False otherwise return len(self.items) == 0 def __len__(self): # Returns the number of items in the queue return len(self.items) def peek(self): # Returns the item at the front of the queue if self.isEmpty(): return None return self.items[0] def add(self, item): # Adds item to the rear of the queue self.items.append(item) def pop(self): # Removes and returns the item at the front of the queue if self.isEmpty(): return None return self.items.pop(0) def stackToQueue(stack): """ Builds a Queue instance containing the items in the given stack. The stack is left unchanged. """ # Create a new empty queue queue = Queue() # Create a temporary stack to store the items temp_stack = [] # Transfer items from stack to temp_stack, preserving order while not stack.isEmpty(): temp_stack.append(stack.pop()) # Transfer items from temp_stack to queue, preserving order for item in reversed(temp_stack): queue.add(item) # Restore the original stack by transferring items back from temp_stack for item in temp_stack: stack.add(item) # Set the front item of the queue to be the top item of the original stack queue.items[0] = stack.peek() # Return the resulting queue return queue

but i need to run it with the following main at the end (as per the assignment):

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

I'm not able to figure out how to make the program work with that main function included.

please help, thanks.

2. 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning Microsoft SQL Server 2012 Programming

Authors: Paul Atkinson, Robert Vieira

1st Edition

1118102282, 9781118102282

More Books

Students also viewed these Databases questions

Question

Were the right people involved in the decision-making process?

Answered: 1 week ago

Question

Were they made on a timely basis?

Answered: 1 week ago