Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

pr17-01.cpp #include <iostream> #include \"NumberList.cpp\" using namespace std;

ID: 3556571 • Letter: P

Question

pr17-01.cpp

#include <iostream>
#include "NumberList.cpp"

using namespace std;

int main()
{
   NumberList list;
   list.add(12);
   list.add(24);
   list.add(36);
  
   list.displayList();
   cout << endl;
   list.remove(24);
   list.displayList();
  
   return 0;
}

NumberList.cpp

#include "NumberList.h"
using namespace std;

//*****************************************************
// add adds a new element to the end of the list. *
//*****************************************************
void NumberList::add(int number)
{
if (head == NULL)
head = new ListNode(number);
else
{
// The list is not empty.
// Use nodePtr to traverse the list
ListNode *nodePtr = head;
while (nodePtr->next != NULL)
nodePtr = nodePtr->next;

// nodePtr->next is NULL so nodePtr points to the last node.
// Create a new node and put it after the last node.
nodePtr->next = new ListNode(number);
}
}

//***************************************************
// displayList outputs a sequence of all values *
// currently stored in the list. *
//***************************************************
void NumberList::displayList()
{
ListNode *nodePtr = head; // Start at head of list
while (nodePtr)
{
// Print the value in the current node
cout << nodePtr->value << " ";
// Move on to the next node
nodePtr = nodePtr->next;
}
}

//******************************************************
// Destructor deallocates the memory used by the list. *
//******************************************************
NumberList::~NumberList()
{
ListNode *nodePtr = head; // Start at head of list
while (nodePtr != NULL)
{
// garbage keeps track of node to be deleted
ListNode *garbage = nodePtr;
// Move on to the next node, if any
nodePtr = nodePtr->next;
// Delete the "garbage" node
delete garbage;
}
}

//***************************************************
// deleteList: delete the node with the matched value *
//***************************************************
void NumberList::remove(int number)
{
ListNode *nodePtr = head; // Start at head of list
ListNode *prePtr = NULL;
while ( (nodePtr != NULL) )
{
       if (nodePtr->value == number) {
           prePtr->next = nodePtr->next;
           delete nodePtr;
           break;
       }

       // move the pointer
       prePtr = nodePtr;
       nodePtr = nodePtr->next;
}
}

NumberList.h

#include <iostream>

using namespace std;
class NumberList
{
protected:
// Declare a class for the list node.
struct ListNode
{
double value;
ListNode *next;
ListNode(int value1, ListNode *next1 = NULL)
{
value = value1;
next = next1;
}
};
ListNode *head; // List head pointer
public:
NumberList() { head = NULL; } // Constructor
~NumberList(); // Destructor
void add(int number);
void remove(int number);
void displayList();
};

Adding Reverse List Function to the Linked List Application

Demonstrate the application and the class by writing three separate programs (where the NumberList class files are provided). You are to provide an application menu with a startup driver to demonstrate the functionality of an instance of this class.

This application program, lab12.cpp, extends the previous NumberList class member functions with reverse list feature:

You may choose to design your own startup driver data for the hardwire test, and the user menu shall allow the user to run the above commands.

Submit this lab in three files: Lab12.cpp, NumberList.h, and NumberList.cpp.

Starter Kit

Use the Lab11  as the starting files.

Submit please zip all three files: lab12.cpp, NumberList.h, and NumberList.cpp as one single lab12.zip file and submit to the dropbox here.

Universal Requirement all assignments for this course: All submitted files shall contain the following information on top of the files (header of the C++ file):

Explanation / Answer

NumberList.cpp

#include "NumberList.h"
using namespace std;

//*****************************************************
// add adds a new element to the end of the list. *
//*****************************************************
void NumberList::add(int number)
{
if (head == NULL)
head = new ListNode(number);
else
{
// The list is not empty.
// Use nodePtr to traverse the list
ListNode *nodePtr = head;
while (nodePtr->next != NULL)
nodePtr = nodePtr->next;

// nodePtr->next is NULL so nodePtr points to the last node.
// Create a new node and put it after the last node.
nodePtr->next = new ListNode(number);
}
}

//***************************************************
// displayList outputs a sequence of all values *
// currently stored in the list. *
//***************************************************
void NumberList::displayList()
{
ListNode *nodePtr = head; // Start at head of list
while (nodePtr)
{
// Print the value in the current node
cout << nodePtr->value << " ";
// Move on to the next node
nodePtr = nodePtr->next;
}
}

//******************************************************
// Destructor deallocates the memory used by the list. *
//******************************************************
NumberList::~NumberList()
{
ListNode *nodePtr = head; // Start at head of list
while (nodePtr != NULL)
{
// garbage keeps track of node to be deleted
ListNode *garbage = nodePtr;
// Move on to the next node, if any
nodePtr = nodePtr->next;
// Delete the "garbage" node
delete garbage;
}
}

//***************************************************
// deleteList: delete the node with the matched value *
//***************************************************
void NumberList::remove(int number)
{
ListNode *nodePtr = head; // Start at head of list
ListNode *prePtr = NULL;
while ( (nodePtr != NULL) )
{
       if (nodePtr->value == number) {
           prePtr->next = nodePtr->next;
           delete nodePtr;
           break;
       }

       // move the pointer
       prePtr = nodePtr;
       nodePtr = nodePtr->next;
}
}

NumberList.h

#include <iostream>

using namespace std;
class NumberList
{
protected:
// Declare a class for the list node.
struct ListNode
{
double value;
ListNode *next;
ListNode(int value1, ListNode *next1 = NULL)
{
value = value1;
next = next1;
}
};
ListNode *head; // List head pointer
public:
NumberList() { head = NULL; } // Constructor
~NumberList(); // Destructor
void add(int number);
void remove(int number);
void displayList();
};

void NumberList::reverse()
{
    ListNode *pTempHead = _pHead, *pRestNodes = NULL, *pNextNode = NULL;
    /* The list is empty do nothing */
    if (_pHead == NULL)
        return;
    /* The previous head node will act as the tail node after reversing */
    _pTail = _pHead;
    /* Take out the head node and make the reverse list basing on it */
    pRestNodes = _pHead->_pNext;
  
    while (pRestNodes != NULL) {
        /* Step1: Take out the next node */
        pNextNode = pRestNodes;
        /* Step2: Update the pointer of rest nodes after taking out the first */
        pRestNodes = pRestNodes->_pNext;
        /* Step3: Add the taken out node to the new list */
        pNextNode->_pNext = pTempHead;
        /* Step4: Update the new temp head node */
        pTempHead = pNextNode;
        /* Repeat Step 1-4 */
    }
    _pHead = pTempHead;
    _pTail->_pNext = NULL;
}