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

Need to write the following functions in dlist.cpp , add function prototypes for

ID: 3869403 • Letter: N

Question

Need to write the following functions in dlist.cpp, add function prototypes for them to dlist.h and invoke the functions in main.cpp. You should label the output of your test, such as “the list after removal: “ etc.

·         int countEven(node * head)       

recursively compute and return the number of nodes that  contains even number in the doubly linked list.

·         int removeEven(node *& head)   

recursively remove all the nodes that contain even number in the doubly linked list and return the number of nodes removed

main.cpp

#include "dlist.h"
using namespace std;

int main()
{
node * head = NULL;
build(head);
display(head);

//PLEASE PUT YOUR CODE HERE to call the function assigned


display(head);
destroy(head);
return 0;
}

dlist.h

#ifndef DLIST_H
#define DLIST_H
//doubly linked list
#include <iostream>
#include <cstring>
#include <cctype>
#include <cstdlib>


struct node
{
int data;
node * previous;
node * next;
};

/* These functions are already written and can be called to test out your code */
void build(node * & head); //supplied
void display(node * head); //supplied
void destroy(node * &head); //supplied

/* *****************YOUR TURN! ******************************** */
/* place your prototype here */
#endif

dlist.cpp

#include "dlist.h"

//put the implmenetation of the required functions here

Please try to use recursion for the two functions.

Explanation / Answer

main.cpp

#include "dlist.h"
using namespace std;

int main()
{
    node * head = NULL;
    build(head);
    display(head);

    //PLEASE PUT YOUR CODE HERE to call the function assigned
    cout << endl;
    cout << "Number of nodes that contains even number in doubly linked list: " << countEven(head) << endl;
    cout << "After removing the nodes that contain even numbers: " << removeEven(head) << endl;

    display(head);
    destroy(head);
    return 0;
}

dlist.cpp


//dlist.cpp

#include "dlist.h"

int countEven(node * head){
    //recursively count even numbers in doubly
    //linked list
    int count = 0;
    if(head){
        int test;
        test = head->data % 2;
        if(test == 0){
            count = 1 + countEven(head->next);
        }else{
            count = countEven(head->next);
        }
    }
    return count;
}


int removeEven(node *& head){
    //recursively remove even numbers in doubly
    //linked list
    int count = 0;
    if(head){
        int test;
        test = head->data % 2;
        if(test == 0){
            count = 1 + removeEven(head->next);

            //if head is root of list
            if(head->previous == nullptr){
                node * temp = head;
                head = head->next;
                head->previous = nullptr;
                delete temp;
            }

            else{
                node * temp;
                temp = head;

                //tail of list
                if(head->next == nullptr){
                    head = head->previous;
                    head->next = nullptr;
                }

                else{
                    head->next->previous = head->previous;
                    head = head->next;
                }
                delete temp;
                temp = nullptr;
            }
        }else{
            count = removeEven(head->next);
        }
    }
    return count;

}

dlist.h

//doubly linked list
#include <iostream>
#include <cstring>
#include <cctype>
#include <cstdlib>


struct node
{
    int data;
    node * previous;
    node * next;
};

/* These functions are already written and can be called to test out your code */
void build(node * & head); //supplied
void display(node * head); //supplied
void destroy(node * &head); //supplied


int countEven(node * head); //recursively count even numbers in doubly linked list
int removeEven(node *& head); //reecursively remove all the nodes that contain an even number in doubly linked list

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote