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

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.
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?
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...
2. Using the Stack ADT: Create a program that uses a stack. Your program should ask...
2. 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. In Java please.
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...
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.88 per access line per month, with a standard deviation of $2.65. Company A's operating expenses were $27.00 per access line per month. Assuming a normal distribution of operating expenses, estimate the percentage of...
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.9 per access line per month, with a standard deviation of $2.45. Company N's operating expenses were $37.03 per access line per month in the first half of 1994. Estimate the percentage of regional...
function exerciseOne(){ // Exercise One: In this exercise you will create a variable called 'aboutMe' //...
function exerciseOne(){ // Exercise One: In this exercise you will create a variable called 'aboutMe' // This variable should be assigned a new object // In this object create three key:value pairs // The keys should be: 'name', 'city', 'favoriteAnimal' // The values should be strings associated with the keys. // return the variable 'aboutMe' } function exerciseTwo(animal){ // Exercise Two: In this exercise you will be given an object called 'animal' // Create a new variable called 'animalName' //...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT