Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider a singly-linked list of integers and \"your node\" declaration below. A

ID: 3823887 • Letter: C

Question

Consider a singly-linked list of integers and "your node" declaration below. Assume the list is sorted. class DB Node public: int number; DB Node *next;}; class DB List {public: void Delete_All_Evens();//deletes all even integers from the list void Insert_Anywhere(int key);//insert key in the correct place in the sorted list}; private: DB Node *front;//points to the front of the list}; a. Using the classes define above (DB_Node and DB_List) implement the function Delete AM Evens. b. Using the classes define above (DB_Node and DB_List) implement the function Insert Anywhere.

Explanation / Answer

#include <iostream>
using namespace std;
class DB_Node
{
    public:
        int number;
        DB_Node *next;
};
class DB_List
{
    private:
        DB_Node *front;
    public:
        void Delete_All_Evens();
        void Insert_Anywhere(int key);  
};
void DB_List::Delete_All_Evens()
{
   
    while(front != NULL && front->number % 2 == 0)
        front = front->next;
    DB_Node *temp = front;
    while(temp->next != NULL)
    {
       if(temp->next->number % 2 == 0)
           temp->next = temp->next->next;
       temp = temp->next;  
    }  
}
void DB_List::Insert_Anywhere(int key)
{
    DB_Node *newNode = new DB_Node;
    newNode->number = key;
    if(front == NULL || front->number >= key)
    {
       newNode->next = front;
       front = newNode;
    }
    else
    {
       DB_Node *temp = front;
       while(temp->next != NULL && temp->next->number >= key)
           temp = temp->next;
       newNode->next = temp->next;
       temp->next = newNode;  
    }
}