Question

In: Computer Science

For this exercise, you will be defining a function which USES both the Stack and the...

For this exercise, you will be defining a function which USES both the Stack and the Queue ADTs. Your code can make use of any of the Queue ADT methods: Queue(), enqueue(), dequeue(), peek(), size() and is_empty() and any of the Stack ADT methods: Stack(), push(), pop(), peek(), size() and is_empty().

Write a function called mirror_queue(a_queue) which takes a Queue as a parameter. The function must modify the parameter Queue object so that the original queue items appear in their original order followed by a copy of the queue items in reverse order.

Note: you can assume that the parameter is not empty.

HINT: It will be useful to make use of a Stack and another Queue to help you mirror the elements in the queue.

For example:

Test Result
q1 = Queue()
q1.enqueue(1)
q1.enqueue(2)
q1.enqueue(3)
print(q1)
mirror_queue(q1)
print(q1)
Queue: [1, 2, 3]
Queue: [1, 2, 3, 3, 2, 1]
q1 = Queue()
q1.enqueue(1)
q1.enqueue(2)
q1.enqueue(3)
q1.enqueue(4)
q1.enqueue(5)
print(q1)
mirror_queue(q1)
print(q1)
Queue: [1, 2, 3, 4, 5]
Queue: [1, 2, 3, 4, 5, 5, 4, 3, 2, 1]

Solutions

Expert Solution

please please thumbs up!!!!

hope it will help uh out!!!

Code::

(IN PYTHON PROGRAMMING LANGUAGE)

---------------------------------------------------------------

import copy
class Queue:
def __init__(self):
self.__items = []
def is_empty(self):
return self.__items == []
def enqueue(self, item):
self.__items.insert(0,item)
def dequeue(self):
if self.__items == []:
raise IndexError ("The queue is empty!")
return self.__items.pop()
def size(self):
return len(self.__items)
def peek(self):
if self.__items ==[]:
raise IndexError ("The queue is empty!")
return self.__items[len(self.__items)-1]
def __str__(self):
return 'Queue: ' + str(self.__items[::-1])
#Implementation of mirror_queue() method
def mirror_queue(a_queue):
#Declare a stack
stack = []
#Make a deep copy of the orginal queue object
#Deep copy doesnot affect original object if changes made to copy
temp = copy.deepcopy(a_queue)
#Insert values of queue to stack
for i in range(temp.size()):
stack.append(temp.dequeue())
#Append values from top of stack to original queue object
for i in range(len(stack)):
a_queue.enqueue(stack.pop())
# Test 1
q1 = Queue()
q1.enqueue(1)
q1.enqueue(2)
q1.enqueue(3)
print(q1)
mirror_queue(q1)
print(q1)
#Test 2
q1 = Queue()
q1.enqueue(1)
q1.enqueue(2)
q1.enqueue(3)
q1.enqueue(4)
q1.enqueue(5)
print(q1)
mirror_queue(q1)
print(q1)
print()

---------------------------------------------------------------

output::

NOTE :: PLEASE CHECK THE SCREENSHOT OF THE PROGRAM FOR THE INDENTATION OF ABOVE CODE


Related Solutions

python IMPORTANT : For this exercise, you will be defining a function that USES the Stack...
python IMPORTANT : For this exercise, you will be defining a function that USES the Stack ADT. A stack implementation is provided to you as part of this exercise - you should not use your own Stack class. Instead, simply use the functions: Stack(), push(), pop() and is_empty() where necessary inside your function definition. For this exercise, you must write a function called balanced_brackets(). This function will be passed a string as an input, and you must check that any...
Write a function that uses a local char queue and a local char stack to determine...
Write a function that uses a local char queue and a local char stack to determine if its string parameter is a palindrome. Your solution will look like: #include <stack> #include <queue> ... bool isPalindrome(const string& candidate) { stack<char> s; queue<char> q; //add only upper case letters and digits to the stack and the queue //if a letter is not upper case then convert it to upper case   } Note: I need help to write out this problem in C++...
Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack...
Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack List 2. Display the list 3. Create the function isEmply 4. Count the number of nodes 5. Insert a new node in the Stack List. 6. Delete the node in the Stack List. 7. Call all methods above in main method with the following data: Test Data : Input the number of nodes : 4 Input data for node 1 : 5 Input data...
write an implementation of the ADT stack that uses a resizeable array to represent the stack...
write an implementation of the ADT stack that uses a resizeable array to represent the stack items. Anytime the stack becomes full, double the size of the array. Maintain the stack's top entry at the end of the array. Please use c++ for this question.
Write an implementation of the ADT stack that uses a resizeable array to represent the stack...
Write an implementation of the ADT stack that uses a resizeable array to represent the stack items. Anytime the stack becomes full, double the size of the array. Maintain the stack's top entry at the beginning of the array. Use c++ to write this code.
Write an implementation of the ADT stack that uses a resizeable array to represent the stack...
Write an implementation of the ADT stack that uses a resizeable array to represent the stack items. Anytime the stack becomes full, double the size of the array. Maintain the stack's top entry at the beginning of the array. Use c++ to write this code.
What is the difference between primary and secondary deviance? Answer this by defining both concepts. Which...
What is the difference between primary and secondary deviance? Answer this by defining both concepts. Which one is caused by labeling and how is it caused by labeling?
Write a Python function that receives a stack object s, where the items in the stack...
Write a Python function that receives a stack object s, where the items in the stack are only numbers in the set {0,1} and returns the number of 1's in the stack s. The function must satisfy the following restrictions: the state of the stack must be preserved; ie., after calling this function the state of the stack s must be the same it was before the function was called. The function cannot use any additional variables of any of...
Using the Stack ADT: Create a program that uses a stack. Your program should ask the...
Using the Stack ADT: Create a program that uses a stack. Your program should ask the user to input a few lines of text and then outputs strings in reverse order of entry. (Optional) Create a similar program that uses a stack. Your new program should ask the user to input a line of text and then it should print out the line of text in reverse. To do this your application should use a stack of Character. In Java...
This exercise uses the normal probability density function and requires the use of either technology or...
This exercise uses the normal probability density function and requires the use of either technology or a table of values of the standard normal distribution. The cash operating expenses of the regional phone companies during the first half of 1994 were distributed about a mean of $29.77 per access line per month, with a standard deviation of $2.55. Company A's operating expenses were $29.00 per access line per month. Assuming a normal distribution of operating expenses, estimate the percentage of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT