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 '''
Types of Linked List
A sequence of data elements connected through links is called a linked list (LL). The elements of a linked list are nodes containing data and a reference to the next node in the list. In a linked list, the elements are stored in a non-contiguous manner and the linear order in maintained by means of a pointer associated with each node in the list which is used to point to the subsequent node in the list.
Linked List
When a set of items is organized sequentially, it is termed as list. Linked list is a list whose order is given by links from one item to the next. It contains a link to the structure containing the next item so we can say that it is a completely different way to represent a list. In linked list, each structure of the list is known as node and it consists of two fields (one for containing the item and other one is for containing the next item address).
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
'''
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
This is a popular solution!
Step by step
Solved in 4 steps with 2 images
It keep saying that 'Queue' object has no attribute 'len' and I can't change the filename
It keep saying that 'Queue' object has no attribute 'len'