I ASK THIS QUESTION HERE AND YOUR EXPERT SOLVE IT THIS WAY  BUT CAN YOU WRITE THE main ( ) FOR THIS CODE     Consider the following implementation of the node and doubly linked-list: template class node { public: type info; node * next;// next node * prev;//back }; template class doubly_linked_list { //data members private: node *head, *tail; int length; public: doubly_linked_list() { head = tail = NULL; length = 0; } bool isEmpty() { // return (head==NULL); if (head == NULL) return true; else return false; } void Append(type e) { node *newnode = new node; newnode->info = e; if (isEmpty()) { newnode->next = NULL; newnode->prev = NULL; head = newnode; tail = newnode; } else { tail->next = newnode; newnode->prev = tail; newnode->next = NULL; tail = newnode; } ++length; } void Display() { if (isEmpty()) { cout << "The linked list is empty !!!!"; return; } cout << "list elements: "; node * current = head; while (current != NULL) { cout << current->info << " "; current = current->next; } cout << endl; } void ReverseDisplay() { if (isEmpty()) { cout << "The linked list is empty !!!!"; return; } cout << "Reverse list elements: "; node * current = tail; while (current != NULL) { cout<< current->info<<" "; current = current->prev; } cout << endl; } void insert(type e, int index) { if (index< 1 || index>length + 1) { cout << "Invalid index !!!!"; return; } else { node * newnode = new node ; newnode->info = e; if (index == 1) { newnode->prev = NULL; if (isEmpty()) { newnode->next = NULL; head = tail = newnode; } else { newnode->next = head; head->prev = newnode; head = newnode; }   } else { node * current = head; int i = 1; while (i != index - 1) { current = current->next; ++i; } if(current !=tail) { current->next->prev = newnode; newnode->next = current->next; current->next = newnode; newnode->prev = current; } else { current->next = newnode; newnode->prev = current; newnode->next = NULL; tail = newnode; } }     } } }; Extend the class doubly_linked_list by adding the following methods: Largest method .This method should return the largest element in a doubly linked-list.  Delete method. This method should delete the first occurrence of an element (value)  from a doubly linked-list.   check_circle Expert Answer Step 1 Here are the required functions for the above code: int getMaxNode(node *head){     /* Input Validation */     if(head == NULL){         printf("Error : Invalid Input !!!!\n");         return INT_MIN;     }     int max = head->data;     while(head != NULL){         if(head->data > max){             max = head->data;         }         head = head->next;     }     return max; } /* Function to delete a node in a Doubly Linked List.    head_ref --> pointer to head node pointer.    del --> pointer to node to be deleted. */ void deleteNode(node** head_ref, node* del) {     /* base case */     if (*head_ref == NULL || del == NULL)         return;     /* If node to be deleted is head node */     if (*head_ref == del)         *head_ref = del->next;     /* Change next only if node to be deleted        is NOT the last node */     if (del->next != NULL)         del->next->prev = del->prev;     /* Change prev only if node to be deleted        is NOT the first node */     if (del->prev != NULL)         del->prev->next = del->next;     /* Finally, free the memory occupied by del*/     free(del); } /* function to delete all occurrences of the given     key 'x' */ void deleteFirstOccurOfX(struct Node** head_ref, int x) {     /* if list is empty */     if ((*head_ref) == NULL)         return; node* current = *head_ref; node* next;     /* traverse the list up to the end */     while (current != NULL) {         /* if node found with the value 'x' */         if (current->data == x) {             /* save current's next node in the                pointer 'next' */             next = current->next;             /* delete the node pointed to by               'current' */             deleteNode(head_ref, current); break;         }         /* else simply move to the next node */         else             current = current->next;     } }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

I ASK THIS QUESTION HERE AND YOUR EXPERT SOLVE IT THIS WAY 

BUT CAN YOU WRITE THE main ( ) FOR THIS CODE  

 

Consider the following implementation of the node and doubly linked-list:


template <class type>
class node
{
public:
type info;

node<type> * next;// next
node<type> * prev;//back

};


template <class type>
class doubly_linked_list
{

//data members
private:
node<type> *head, *tail;

int length;
public:
doubly_linked_list()
{

head = tail = NULL;
length = 0;
}
bool isEmpty()
{ // return (head==NULL);
if (head == NULL)
return true;
else

return false;
}


void Append(type e)
{

node<type> *newnode = new node<type>;
newnode->info = e;
if (isEmpty())
{

newnode->next = NULL;
newnode->prev = NULL;

head = newnode;
tail = newnode;

}
else

{
tail->next = newnode;

newnode->prev = tail;
newnode->next = NULL;

tail = newnode;
}

++length;

}


void Display()
{
if (isEmpty()) { cout << "The linked list is empty !!!!"; return; }

cout << "list elements: ";
node<type> * current = head;
while (current != NULL)
{
cout << current->info << " ";

current = current->next;
}
cout << endl;

}

void ReverseDisplay()

{
if (isEmpty()) { cout << "The linked list is empty !!!!"; return; }
cout << "Reverse list elements: ";

node<type> * current = tail;
while (current != NULL)
{

cout<< current->info<<" ";
current = current->prev;
}

cout << endl;
}

void insert(type e, int index)
{

if (index< 1 || index>length + 1)

{
cout << "Invalid index !!!!"; return;

}
else
{

node<type> * newnode = new node <type>;
newnode->info = e;

if (index == 1)
{
newnode->prev = NULL;

if (isEmpty())
{

newnode->next = NULL;

head = tail = newnode;
}

else {
newnode->next = head;
head->prev = newnode;
head = newnode;

}

 


}
else

{
node<type> * current = head;

int i = 1;
while (i != index - 1)

{
current = current->next;
++i;
}


if(current !=tail)

{
current->next->prev = newnode;

newnode->next = current->next;
current->next = newnode;

newnode->prev = current;

}

else
{

current->next = newnode;

newnode->prev = current;

newnode->next = NULL;
tail = newnode;

}

}

 

 


}

}

};

Extend the class doubly_linked_list by adding the following methods:

  • Largest method .This method should return the largest element in a doubly linked-list. 
  • Delete method. This method should delete the first occurrence of an element (value)  from a doubly linked-list.
 
check_circle

Expert Answer

Step 1

Here are the required functions for the above code:

int getMaxNode(node *head){

    /* Input Validation */

    if(head == NULL){

        printf("Error : Invalid Input !!!!\n");

        return INT_MIN;

    }

    int max = head->data;

    while(head != NULL){

        if(head->data > max){

            max = head->data;

        }

        head = head->next;

    }

    return max;

}

/* Function to delete a node in a Doubly Linked List.

   head_ref --> pointer to head node pointer.

   del --> pointer to node to be deleted. */

void deleteNode(node** head_ref, node* del)

{

    /* base case */

    if (*head_ref == NULL || del == NULL)

        return;

    /* If node to be deleted is head node */

    if (*head_ref == del)

        *head_ref = del->next;

    /* Change next only if node to be deleted

       is NOT the last node */

    if (del->next != NULL)

        del->next->prev = del->prev;

    /* Change prev only if node to be deleted

       is NOT the first node */

    if (del->prev != NULL)

        del->prev->next = del->next;

    /* Finally, free the memory occupied by del*/

    free(del);

}

/* function to delete all occurrences of the given

    key 'x' */

void deleteFirstOccurOfX(struct Node** head_ref, int x)

{

    /* if list is empty */

    if ((*head_ref) == NULL)

        return;

node* current = *head_ref;

node* next;

    /* traverse the list up to the end */

    while (current != NULL) {

        /* if node found with the value 'x' */

        if (current->data == x) {

            /* save current's next node in the

               pointer 'next' */

            next = current->next;

            /* delete the node pointed to by

              'current' */

            deleteNode(head_ref, current);

break;

        }

        /* else simply move to the next node */

        else

            current = current->next;

    }

}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY