Question: Header file using namespace std; template // Forward declaration of the List class class List; template class ListNode // Facilitator class for the List class
Header file using namespace std;
template // Forward declaration of the List class class List;
template class ListNode // Facilitator class for the List class { private:
// Constructor ListNode(const DT& initData, ListNode* nextPtr);
// Data members DT dataItem; // List data item ListNode* next; // Pointer to the next list node
friend class List
//--------------------------------------------------------------------
template class List { public:
// Constructor List();
// Destructor ~List();
// List manipulation functions void insert(const DT& newData); // Insert after cursor void clear(); // Clear list
// Output the list structure -- used in testing/debugging void showStructure() const;
// functions for getting recursive things going void write() const; // Output list data items void insertEnd(const DT& newData); // Insert at end void aBeforeb(); // In-lab Exercise 3
private:
// Recursive partners of the Prelab functions void writeSub(ListNode
// Recursive partners of the In-lab functions void aBeforebSub(ListNode
private:
// Data members ListNode
//-------------------------------------------------------------------- // // Laboratory 8 listrec.cpp // // SOLUTION: Partial linked list implementation of the List ADT // with additional recursive linked list functions // //--------------------------------------------------------------------
//-------------------------------------------------------------------- // // Uses parts of the singly linked list implementation // (from Laboratory 6): // // - The ListNode class constructor // // - The List class constructor, destructor, insert(), clear(), and // showstructure() functions. // //--------------------------------------------------------------------
template ListNode
// Creates a list node containing item elem and next pointer // nextPtr.
: dataItem(nodeDataItem), next(nextPtr) {}
//--------------------------------------------------------------------
template List
: head(0), cursor(0) {}
//--------------------------------------------------------------------
template List
// Frees the memory used by a list.
{ clear(); }
//--------------------------------------------------------------------
template void List
// Inserts newDataItem after the cursor. If the list is empty, then // newDataItem is inserted as the first (and only) data item in the // list. In either case, moves the cursor to newDataItem.
{ if (head == 0) // Empty list { head = new ListNode
//--------------------------------------------------------------------
template void List
// Removes all the data items from a list.
{ ListNode
head = 0; cursor = 0; }
//--------------------------------------------------------------------
template void List
// Outputs the data items in a list. If the list is empty, outputs // "Empty list". This operation is intended for testing and // debugging purposes only.
{ ListNode
if (head == 0) cout next) if (p == cursor) cout dataItem dataItem
//-------------------------------------------------------------------- // // Recursively implemented linked list functions used in the Prelab // Exercise // //--------------------------------------------------------------------
template void List
// Outputs the data items in a list from beginning to end. Assumes that // objects of type DT can be output to the cout stream.
{ cout
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
template void List
// Recursive partner of the write() function. Processes the sublist // that begins with the node pointed to by p.
{ if (p != 0) { cout dataItem; // Output data item writeSub(p->next); // Continue with next node
} }
//--------------------------------------------------------------------
template void List
// Inserts newDataItem at the end of a list. Moves the cursor to // newDataItem.
{ insertEndSub(head, newDataItem); }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
template void List
// Recursive partner of the insertEnd() function. Processes the // sublist that begins with the node pointed to by p.
{ if (p != 0) insertEndSub(p->next, newDataItem); // Continue searching for else // end of list { p = new ListNode
//--------------------------------------------------------------------
template void List
// Inserts an data item containing 'a' before each data item // containing 'b'. //The sole purpose of this function is to call aBeforebSub and //send the head as an argument. //ie. only one line of code is necessary here.
{ // add code here to call aBeforebSub()
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
template void List
// Recursive partner of the aBeforeb() function. Processes the sublist // that begins with the node pointed to by p.
{ //add code here to complete the function
}

-
Your primary tasks for this exercise are:
- Alter the writeSub() function so that it prints the list in reverse.
Steps include:
- Try to run this program. You should get the prompt: Enter a list of characters :
Now, type "abc" and enter. You will get the following output:
Enter a list of characters : abc a b [c] List : abc List : abc! a b c [!] Press any key to continue
- Now, modify the writeSub() function (in the listrec.h file) so that it prints in reverse. Build and Run your program, you should get the following output:
Enter a list of characters : abc a b [c] List : cba List : !cba a b c [!] Press any key to continue
Hint: If you don't get this output, try Rebuild All from the Build Menu. - Next, create a new recursive function that will insert the character 'a' immediately before each occurence of the character 'b'. The cursor will not change.
- add code in the listrec.h file for aBeforeb() and aBeforebSub(). The function prototypes exist, you have to add code to get them running.
- Activate the call to the aBeforeb() function (in the recursion.cpp file) by uncommenting the lines: // testList.aBeforeb(); // testList.showStructure();
- Build and run the executable. Your output will look like this:
Enter a list of characters : abc a b [c] List : cba List : !cba a a b c [!] Press any key to continue
- Prepare a test plan for this function that includes list containing the character 'b' at the beginning, middle, and end. A test plan form follows.
- Execute your test plan. If you discover mistakes in your implementation of the aBeforeb() function, correct them and execute your test plan again.
Test Plan for the aBeforeb Operation
| Test Case | List | Expected Result | Checked |
|---|---|---|---|
| b in beginning | |||
| b in middle | |||
| b at end | |||
| multiple b's |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
