Question
IMPLEMENT THE CODE USING C++ OR PYTHON ONLY: In this exercise, you will implement a queue-like data structure that supports insertion and deletion at both
IMPLEMENT THE CODE USING C++ OR PYTHON ONLY:
In this exercise, you will implement a queue-like data structure that supports insertion and deletion at both the front and the back of the queue. Such a structure is called a double-ended queue, or deque, which is usually pronounced deck to avoid confusion with the DEQUEUE method of the regular queue.
For example, consider a restaurant using a queue to maintain a waitlist. Occasionally, the first person might be removed from the queue only to find that a table was not available; typically, the restaurant will re-insert the person at the first position in the queue. It may also be that a customer at the end of the queue may grow impatient and leave the restaurant.
The deque data structure is defined so that deque supports the following operations:
The following table shows a series of operations and their effects on an initially empty deque of integers:
Implement all the eight operations stated above to work on any sequence of integers provided by the user.
\begin{tabular}{|l|c|l|} \hline No. & Operation & \multicolumn{1}{|c|}{ Description } \\ \hline 1. & D.add_first (x) & Add element x to the front of deque D. \\ \hline 2. & D.add_last (x) & Add element x to the back of deque D. \\ \hline 3. & D.delete_first( ) & RemoveandreturnthefirstelementfromdequeD;anerroroccursifthedequeisempty. \\ \hline 4. & D. delete_last( ) & RemoveandreturnthelastelementfromdequeD;anerroroccursifthedequeisempty. \\ \hline 5. & D.first ( ) & Return(butdonotremove)thefirstelementofdequeD;anerroroccursifthedequeisempty. \\ \hline 6. & D.last () & Return(butdonotremove)thelastelementofdequeD;anerroroccursifthedequeisempty. \\ \hline 7. & D.is_empty( ) & Return TRuE if deque D does not contain any elements. \\ \hline 8. & len(D) & Return the number of elements in deque D. \\ \hline \end{tabular} \begin{tabular}{|c|c|c|} \hline Operation & Return Value & Description \\ \hline D.add_last(5) & - & {[5]} \\ \hline D.add_first(3) & - & {[3,5]} \\ \hline D.add_first(7) & - & {[7,3,5]} \\ \hline D.first( ) & 7 & {[7,3,5]} \\ \hline D.delete_last( ) & 5 & {[7,3]} \\ \hline len(D) & 2 & {[7,3]} \\ \hline D.delete_last( ) & 3 & {[7]} \\ \hline D.delete_last( ) & 7 & []] \\ \hline D.add_first(6) & - & {[6]} \\ \hline D.last() & 6 & {[6]} \\ \hline D.add_first(8) & - & {[8,6]} \\ \hline D.is_empty( ) & FALSE & {[8,6]} \\ \hline D.last( ) & 6 & {[8,6]} \\ \hline \end{tabular}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