Answered step by step
Verified Expert Solution
Question
1 Approved Answer
C++ Question: Write an implementation of the ADT queue that uses linked nodes to represent the queue items. Hint: you can take a look at
C++
Question: Write an implementation of the ADT queue that uses linked nodes to represent the queue items. Hint: you can take a look at chapter 14.1.2 of your textbook.
14.1.2 A Link-Based Implementation A link-based implementation of a queue uses a chain of linked nodes, much like the other link-based implementations that you have seen. However, the queue presents a challenge, since we must be able to not only remove entries from its front but also add them to its back. Removing a node from the beginning of a linked chain is easy, but to add a new node to the chain's end, we need a pointer to the chain's last node. One way to accomplish this is to begin at the first node and traverse the chain until we reach the last one. A much more efficient approach uses a tail pointer to reference the end of the chain just as the head pointer references the beginning of the chain. Figure 14-2 illustrates a chain of linked nodes that has both head and tail pointers. Like the head pointer frontPtr, backPtr is exter- nal to the chain. FIGURE 14-2 A chain of linked nodes with head and tail pointers 110 HOOD frontPtr backPtr Figure 14-3 shows that you can actually get by with one external pointer to the back if you make the last node point to the first one. This data structure is a circular chain of linked nodes. Notice that the nodes in a circular chain have next pointers that never contain nullptr. We will refer to a chain like the one in Figure 14-2 as a linear chain, regardless of how many external pointers it has. Such a chain does have a node whose next pointer is nullptr. Programming Problem 1 at the end of the chapter asks you to consider the details of the circular chain implementation. Here we will develop an implementation of the ADT queue using a chain that has both head and tail pointers, as illustrated in Figure 14-2. pienellaLIULIS UILE ADI Queue 401 FIGURE 14-3 A circular chain of linked nodes with one external pointer backPtr Note: If you use a linear chain with only a head pointer to implement a queue, the enqueue operation will be inefficient. Each addition to the queue requires a traversal to the end of the chain. As the queue increases in length, the traversal time and hence enqueue's time requirementwill increase. Listing 14-3 shows the header file for our class definition. LISTING 14-3 The header file for the class LinkedQueue /** ADT queue: Link-based implementation. @file LinkedQueue. */ #ifndef LINKED_QUEUE #define _LINKED_QUEUE #include "Queue Interface.h" #include "Node.h" #include "PrecondViolatedExcep.h" templateStep 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