Step 2: #include using namespace std; class LIST_NODE { public: int data; // dat
ID: 3559360 • Letter: S
Question
Step 2: #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(LINKED_LIST_CLASS &); // copy 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 << "The 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(LINKED_LIST_CLASS & org) { cout << endl << "The copy constructor has been called. "; front = new LIST_NODE; front->next = 0; front->data = -10000; LIST_NODE *p = org.front->next; LIST_NODE *back = 0; while(p!=0) { if (back == 0) { front->next = new LIST_NODE; back = front->next; back->next = 0; back->data = p->data; } else { back->next = new LIST_NODE; back = back->next; back->data = p->data; back->next = 0; } p=p->next; } } 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<data; if (p->next != 0) { cout << "-->"; } } cout << endl << endl; } int main() { LINKED_LIST_CLASS L1; L1.Add(5); L1.Add(10); L1.Add(29); L1.Print(); LINKED_LIST_CLASS L2 = L1; L2.Print(); return 0; } Question 7: What is the output of the program in Step 2? Question 8: Please draw the list object, L2, for the program in Step 2. Question 9: What is the purpose of the back pointer in the copy constructor for the program in Step 2? Question 10: Is the front of L2 ever equal to 0 or null? Please explain your answer. Question 11: What are the contents of the header node for L2?
Explanation / Answer
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 != NULL; p = p->next)
{
cout<<p->data;
if (p->next != 0)
{
cout<<"-->";
}
}
cout<<endl<<endl;
}
void LINKED_LIST_CLASS::reverse() {
LIST_NODE *prev = NULL, *following = NULL, *current;
current = front;
while(current != NULL){
following = current->next;
current->next = prev;
prev = current;
current = following;
}
front = prev;
}
int main()
{
LINKED_LIST_CLASS L1;
L1.Add(5);
L1.Add(10);
L1.Add(29);
L1.Add(35);
L1.Print();
L1.reverse();
L1.Print();
return 0;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.