Write these 5 functions for for class below Please write the

Write these 5 functions for for class below: Please write the complete code using C++ lanagauge.

1. deconstructor free all memmory on list List: ~List

2. delete oldest node instrted and return it\'s data value oldest node should be tail_ptr use int List:: pop

3. add a new node to end of the list, and newest node will be heald in head_ptr use void List::push(int new_data)

4. return if the node is containing the passed-in integer is in the list, true if such a node exists, false otherwise use bool List::search(int test_data)

Epragma once include kiostream include \"node h. using namespace std; class List int list size Node *head ptr //Holds newest member Node *tail ptr //Holds oldest member public: List list size head ptr NULL tail ptr NULL List int get list size const f return list size; //Output the list void print list if (head ptr NULL) cout \"EMPTY LIST n return; Node temp ptr head ptr int counter 0; while (temp ptr) //same as saying while (temp ptr NULL) cout \"Node counter temp ptr- get data endli temp ptr temp ptr->get next //Write these functions int pop void push (int new data) bool search int test data) bool remove (int old data)

Solution

Below is the code for your question :

//Assuming List to be Singly Linked List

~list()
{
   Node *temp_ptr = NULL;
   while (head_ptr)
   {
       *temp_ptr = head_ptr;
       free (temp_ptr);
       head_ptr = head_ptr->get_next();
   }
   head_ptr = NULL;
   tail_ptr = NULL;
}

int List::pop()
{
   int data;
   Node *temp_ptr = head_ptr;
   if(head_ptr == tail_ptr)
   {
       data = head_ptr->get_data();
       free(head_ptr);
       head_ptr = NULL;
       tail_ptr = NULL;
       return data;
   }
   while (temp_ptr)
   {
       if(temp_ptr->get_next() == tail_ptr)
       {
           data = tail_ptr->get_data();
           tail_ptr = temp_ptr;
           free(temp_ptr->get_next);
           return data;
       }
          
       else
           temp_ptr = temp_ptr->get_next();
   }
}

void List::push(int new_data)
{
   Node *temp_ptr = new Node(new_data,NULL); //constructer of Node class having data,next_ptr as parameter
   if(head_ptr == NULL)
   {
       head_ptr = temp_ptr;
       tail_ptr = temp_ptr;
       return;
   }
   temp_ptr->get_next() = head_ptr;
   head_ptr = temp_ptr;
}

bool List::search(int test_data)
{
   Node *temp_ptr = head_ptr;
   while (temp_ptr)
   {
       if(temp_ptr->get_data() == test_data)
           return true;
       else
           temp_ptr=temp_ptr->get_next();
   }
   return false;
}

bool List::remove(int old_data)
{
   Node *temp_ptr = head_ptr;
   int flag = 0;
   while (temp_ptr->get_next())
   {
       if(temp_ptr->get_next()->get_data() == old_data)
       {
           Node *temp_ptr1 = temp_ptr->get_next();
           temp_ptr->get_next() = temp_ptr1->get_next();
           free(temp_ptr1);
           return true;
       }
       else
           temp_ptr=temp_ptr->get_next();
   }
   return false;  
}

Could not execute because node.h file not found.

Please let me know for any concerns.

Write these 5 functions for for class below: Please write the complete code using C++ lanagauge. 1. deconstructor free all memmory on list List: ~List 2. delete
Write these 5 functions for for class below: Please write the complete code using C++ lanagauge. 1. deconstructor free all memmory on list List: ~List 2. delete

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site