Add code for the bolded area only: File name : 2170_10_5a.cc Purpose : This prog
ID: 3779378 • Letter: A
Question
Add code for the bolded area only:
File name : 2170_10_5a.cc
Purpose : This program demonstrates the use of dummy head node
linked lists and insertion of a node.
******************************************************************************/
#include
#include "2170_10_5a.h"
using namespace std;
// Default Constructor for the DummyList class
DummyList::DummyList()
{
//create the dummy node
head = new NodeType;
head->next = NULL;
head->data = -1.0;
}
// Default Destructor for the DummyList class
DummyList::~DummyList()
{
NodePtrType q = head;
while (q != NULL)
{
head = head->next;
q->next = NULL;
delete q;
q = head;
}
}
//This function inserts a node at the beginning of the list
//Add code to insert the node p at the beginning of the list.
// Remember that head is a dummy node and should never be changed.
void DummyList::InsertAtBeginning(float newData)
{
NodePtrType p; //pointer to node to contain new data
//create the node
p = new NodeType;
//store the data
p->data = newData;
(the code should be inserted here)
}
//This function traverses the list and prints it.
//If the dummy head node has been altered, it prints
//and error.
void DummyList::PrintList()
{
NodePtrType p;
p = head;
if (p->data != -1)
{
cout << "Illegal head node" << endl;
}
else
{
p = p->next;
while (p != NULL)
{
cout << p->data << endl;
p = p->next;
}
cout << endl;
}
}
Explanation / Answer
void DummyList::InsertAtBeginning(float newData)
{
NodePtrType p; //pointer to node to contain new data
p = (struct Node* ) malloc(sizeof (Node)); //create the node with same memory
p->data=newData;
p->next=head;//new node contain the address of node who was previously first node in the list
head->p//now head contain the address of new node (so this node is first node of the list)
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.