Qestion Please add precondition postconditions and descripti
Qestion: Please add pre-condition, post-conditions and descriptions for each member function of the LINKED_LIST_CLASS. Answer with your new version of the program.
#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(LINKED_LIST_CLASS &); // copy constructor
~LINKED_LIST_CLASS(); // destructor
void Add(int); // mutator
void Print(); // accessor
LIST_NODE * Search(int); // accessor
void Remove(int); // mutator
bool Is_Empty(); // accessor
private:
LIST_NODE *front; // pointer to front of list
};
LINKED_LIST_CLASS::LINKED_LIST_CLASS()
{
cout << endl << \"The default constructor has been called.\ \";
front = new LIST_NODE;
front->next = 0; // initialize the 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 << p->data;
if (p->next != 0)
{
cout << \"-->\";
}
}
cout<<endl<<endl;
}
LIST_NODE * LINKED_LIST_CLASS::Search(int key)
{
for(LIST_NODE *p = front->next; p!=0; p=p->next)
{
if (p->data == key)
return p;
}
return 0; // key not found in list
}
void LINKED_LIST_CLASS::Remove(int key)
{
LIST_NODE *p = Search(key);
if (Is_Empty())
{
cout << key << \" is not in the list. No removal performed!\ \";
}
else
{
LIST_NODE *q = front;
while (q->next->data != key)
{
q = q->next;
}
q->next = p->next; // CRITICAL STEP!!!!
delete p;
}
}
bool LINKED_LIST_CLASS::Is_Empty()
{
return front->next == 0;
}
int main()
{
LINKED_LIST_CLASS L1;
L1.Add(5);
L1.Add(10);
L1.Add(29);
L1.Print();
LINKED_LIST_CLASS L2 = L1;
L2.Print();
L1.Remove(10);
L1.Print();
return 0;
}
Solution
#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(LINKED_LIST_CLASS &); // copy constructor
 ~LINKED_LIST_CLASS(); // destructor
 void Add(int); // mutator
 void Print(); // accessor
 LIST_NODE * Search(int); // accessor
 void Remove(int); // mutator
 bool Is_Empty(); // accessor
 private:
 LIST_NODE *front; // pointer to front of list
 };
 
 LINKED_LIST_CLASS::LINKED_LIST_CLASS()
 {
 //Pre-condition : front should be null i.e. list should be empty
 if(front == 0)
 {
 cout << endl << \"The default constructor has been called.\ \";
 front = new LIST_NODE;
 front->next = 0; // initialize the next field to null
 front->data = -10000;
 
 //Post-condition : front should not be null
 if(Is_Empty())
 cout<<\"Post-condition failed for default constructor\";
 }
   
 }
 
 LINKED_LIST_CLASS::LINKED_LIST_CLASS(LINKED_LIST_CLASS & org)
 {
 //Pre condition : linked list should not be empty
 if(!org.Is_Empty())
 {
 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;
 }
 //Post-condition: Linked list copied to should not be empty
 if(Is_Empty())
 {
 cout<<\"Copy constructor failed\";
 }
 }
 }
 
 LINKED_LIST_CLASS::~LINKED_LIST_CLASS()
 {
 if(front != 0) //front should be initialized for it to destroy
 {
 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;
   
 if(front != 0) //front is not destroyed if it is not null
 {
 cout<< \"Destructor failed\";
 }
 }
 }
 
 void LINKED_LIST_CLASS::Add(int item)
 {
 //Pre-condition: item is already added in linked list
 if(Search(item) == 0)
 {
 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;
 }
 
 //Post-condition : item should be at the front of linked list
 if(front->next->data != item)
 cout<<\"Add operation failed\"<<endl;
 }
 }
 
 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;
 }
 
 LIST_NODE * LINKED_LIST_CLASS::Search(int key)
 {
 //Pre-condition : Search only is list is not empty
 if(front->next != 0)
 {
 for(LIST_NODE *p = front->next; p!=0; p=p->next)
 {
 if (p->data == key)
 return p;
 }
 }
 return 0; // key not found in list
 }
 void LINKED_LIST_CLASS::Remove(int key)
 {
 //Pre-condition: Item should be present in list
 if(Search(key) != 0)
 {
 LIST_NODE *p = Search(key);
 if (Is_Empty())
 {
 cout << key << \" is not in the list. No removal performed!\ \";
 }
 else
 {
 LIST_NODE *q = front;
 while (q->next->data != key)
 {
 q = q->next;
 }
 q->next = p->next; // CRITICAL STEP!!!!
 delete p;
 }
 //Post-condition: Item should not be present in array
 if(Search(key) != 0)
 cout<<\"Remove failed\";
 }
 }
 bool LINKED_LIST_CLASS::Is_Empty()
 {
 return front->next == 0;
 }
 int main()
 {
 LINKED_LIST_CLASS L1;
 L1.Add(5);
 L1.Add(10);
 L1.Add(29);
 L1.Print();
 LINKED_LIST_CLASS L2 = L1;
 L2.Print();
 L1.Remove(10);
 L1.Print();
 return 0;
 }





