Step 1: #include using namespace std; class LIST_NODE { public: int data; // dat
ID: 3559265 • Letter: S
Question
Step 1: #include using namespace std; class LIST_NODE { public: int data; // data element of node LIST_NODE *next; // pointer element of node }; class LINKED_LIST_CLASS { public: LINKED_LIST_CLASS(); // default constructor ~LINKED_LIST_CLASS(); // destructor void Add(int); // mutator void Print(); // accessor private: LIST_NODE *front; // pointer to front of list (header node) }; LINKED_LIST_CLASS::LINKED_LIST_CLASS() { cout << endl << "Default constructor has been called. "; front = new LIST_NODE; front->next = 0; // initialize next field to null front->data = -10000; } LINKED_LIST_CLASS::~LINKED_LIST_CLASS() { cout << endl << "The destructor has been called. "; while (front->next != 0) { LIST_NODE *p = front->next; front->next = front->next->next; delete p; } delete front; front = 0; } void LINKED_LIST_CLASS::Add(int item) { LIST_NODE *p = new LIST_NODE; p->data = item; if (front->next == 0) // empty list { front->next = p; p->next = 0; } else // list has information and is not empty { p->next = front->next; front->next = p; } } void LINKED_LIST_CLASS::Print() { cout << endl; for(LIST_NODE *p = front->next; p != 0; p = p->next) { cout << p->data; if (p->next != 0) { cout << "-->"; } } cout << endl << endl; } int main () { LINKED_LIST_CLASS sample1; sample1.Add(4); sample1.Add(5); sample1.Add(6); sample1.Add(4); sample1.Add(7); sample1.Print(); return 0; } Question 1: What is the output of the program in Step 1? Please show the values. Question 2: Please draw an empty list. Question 3: Please draw the list that is printed by the program in Step 1. Question 4: Is the front of the list ever equal to 0 or null? Please explain your answer. Question 5: What are the contents of the header node? Question 6: Please list the order in which nodes are removed from the list by the destructor?
Explanation / Answer
#include<iostream>
using namespace std;
class LIST_NODE
{
public:
int data; // data element of node
LIST_NODE *next; // pointer element of node
};
class LINKED_LIST_CLASS
{
public:
LINKED_LIST_CLASS(); // default constructor
~LINKED_LIST_CLASS(); // destructor
void Add(int); // mutator
void Print(); // accessor
private:
LIST_NODE *front; // pointer to front of list (header node)
};
LINKED_LIST_CLASS::LINKED_LIST_CLASS()
{
cout << endl << "Default constructor has been called. ";
front = new LIST_NODE;
front->next = 0; // initialize next field to null
front->data = -10000;
}
LINKED_LIST_CLASS::~LINKED_LIST_CLASS()
{
cout << endl << "The destructor has been called. ";
while (front->next != 0)
{
LIST_NODE *p = front->next;
front->next = front->next->next;
delete p;
}
delete front;
front = 0;
}
void LINKED_LIST_CLASS::Add(int item)
{
LIST_NODE *p = new LIST_NODE;
p->data = item;
if (front->next == 0) // empty list
{
front->next = p;
p->next = 0;
}
else
// list has information and is not empty
{
p->next = front->next;
front->next = p;
}
}
void LINKED_LIST_CLASS::Print()
{
cout << endl;
for(LIST_NODE *p = front->next; p != 0; p = p->next)
{
cout << p->data;
if (p->next != 0)
{
cout << "-->";
}
}
cout << endl << endl;
}
int main ()
{
LINKED_LIST_CLASS sample1;
sample1.Add(4);
sample1.Add(5);
sample1.Add(6);
sample1.Add(4);
sample1.Add(7);
sample1.Print();
return 0;
}
Question 1: What is the output of the program in Step 1? Please show the values.
OUTPUT:
Default constructor has been called.
7-->4-->6-->5-->4
The destructor has been called.
Question 2: Please draw an empty list.
front -> next = 0;
-1000 -> NULL
Question 3: Please draw the list that is printed by the program in Step 1.
7-->4-->6-->5-->4 -> 0.
Question 4: Is the front of the list ever equal to 0 or null? Please explain your answer.
front of list even equal to 0 or null because in constructor we are assigning value -1000.
Question 5: What are the contents of the header node?
header node contains -1000 and next points to null.
Question 6: Please list the order in which nodes are removed from the list by the destructor?
7 -> 4 -> 6 -> 5 -> 4.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.