Question
-----PYTHON----- 1. A Queue is an abstract data type for storing a collection of items, where items are added to one end and
-----PYTHON-----
1. A "Queue" is an abstract data type for storing a collection of items, where items are added to one end and removed from the opposite end of the collection. The methods for adding and removing items from a queue are traditionally called "enqueue()" and "dequeue()" respectively.
Complete and test the Queue implementations. Exceptions are raised when preconditions are violated. For example, an IndexError will be raised if a client program attempt to dequeue from an empty queue. You can modify the dequeue() method of the Queue class to raise an IndexError if an attempt is made to dequeue from an empty queue.
You should include the entire Queue class definition in your answer to this question.
For example:
Test | Result |
---|---|
try: q = Queue() q.enqueue(2) print(q.dequeue()) except IndexError as err: print (err) | 2 |
2.
Extend the Queue class by adding the method str(self) that print all the elements from the queue. Note, this method does not remove any elements from the queue.
You should include the entire Queue class definition in your answer to this question.
For example:
Test | Result |
---|---|
q = Queue() q.enqueue(2) print(q) print(q.size()) | Queue: [2] 1 |
q = Queue() print(q) print(q.size()) | Queue: [] 0 |
3.
Extend the Queue class by adding the method enqueue_list(self) that enqueues all the elements from a list into the queue.
You should include the entire Queue class definition in your answer to this question.
For example:
Test | Result |
---|---|
carBrand = Queue() carlist = ['Audi', 'Honda', 'Toyota', 'Mercedes'] carBrand.enqueue_list(carlist) print(carBrand) | Queue: ['Audi', 'Honda', 'Toyota', 'Mercedes'] |
student = Queue() studentlist = ['Aaron', 'Julia', 'Wendy', 'Paul'] student.enqueue_list(studentlist) print(student) | Queue: ['Aaron', 'Julia', 'Wendy', 'Paul'] |
4.
Extend the Queue class by adding the method splice(self, second_queue). The splice method combines two queues by adding all elements of a second queue to the end of a first queue. The second_queue should not be altered when the method completes. For full credit, you must take advantage of being able to directly access the elements of second_queue.
You should include the entire Queue class definition in your answer to this question.
For example:
Test | Result |
---|---|
my_queue = Queue() my_queue.enqueue('1') my_queue.enqueue('2') my_queue.enqueue('3') another_queue = Queue() another_queue.enqueue('4') another_queue.enqueue('5') my_queue.splice(another_queue) print(my_queue) | Queue: ['1', '2', '3', '4', '5'] |
my_queue = Queue() my_queue.enqueue('4') my_queue.enqueue('5') another_queue = Queue() my_queue.splice(another_queue) print(my_queue) | Queue: ['4', '5'] |
5.
Write a function, mirror_queue(q), which takes a Queue, q, as a parameter and modifies the queue so that the queue items appear in their original order followed by a copy of the queue items in reverse order. You must make use of a stack to help you to solve this problem.
The implementations of both the Stack and Queue ADTs are provided to you. You can simply use: push(), pop(), is_empty() as well as enqueue() and dequeue() as necessary in your function definition.
However, you will need to use the following statement to create a Stack (in this question)
s = my_stack_module.Stack()
Similarly, you will need to use the following statement to create a Queue (in this question)
q = my_queue_module.Queue()
For example:
Test | Result |
---|---|
q1 = my_queue_module.Queue() q1.enqueue(1) q1.enqueue(2) q1.enqueue(3) mirror_queue(q1) print(q1) | Queue: [1, 2, 3, 3, 2, 1] |
q1 = my_queue_module.Queue() mirror_queue(q1) print(q1) | Queue: []
|
6.
Write a function, reverse_queue(q), which takes a Queue, q, as a parameter and modifies the queue so that the elements in the queue are rearranged into reverse order. You must make use of a stack to help you in reversing the elements in the queue.
The implementations of both the Stack and Queue ADTs are provided to you. You can simply use: push(), pop(), is_empty() as well as enqueue() and dequeue() as necessary in your function definition.
However, you will need to use the following statement to create a Stack (in this question)
s = my_stack_module.Stack()
Similarly, you will need to use the following statement to create a Queue (in this question)
q = my_queue_module.Queue()
For example:
Test | Result |
---|---|
q1 = my_queue_module.Queue() q1.enqueue(1) q1.enqueue(2) q1.enqueue(3) print(q1) reverse_queue(q1) print(q1) | Queue: [1, 2, 3] Queue: [3, 2, 1] |
q1 = my_queue_module.Queue() reverse_queue(q1) print(q1) | Queue: [] |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started