I need help being able to make a function that adds an item to the back of a doubly linked list to add an animal to the end of the list in C++ Here's what I got so far with the code #include #include using namespace std;   class node { public:     string data;// Data item     node* next; // Point to next node };   class linkedList { public:          // Variables? (Attributes)     node* headptr;          // Functions (methods)     linkedList()     {         headptr = nullptr;     }          // Add an item to front of list     void addFront(string x)     {         // Declare a pointer variable         node* p;                  // Create a new node, and store its address in p         p = new node;                  // Put x into data field of the new node         // Can also put p -> data = x, same thing.         (*p).data = x;                  // Set next pointer of node to previous front item         (*p).next = headptr;                  //Update the head pointer         headptr = p;     }          // Add an item to the back of the list     void addBack(string x)     {        // Help required here     }                                     // Remove and return front item from list     string popFront()     {         // Step 1: Write down value to return         string output;         output = (*headptr).data;                  // Now, let's remove the front item                  // Write down node location for deletion         node* doomedNode;         doomedNode = headptr;                  // Move headpoint to remove node         headptr = (*headptr).next;                  // Prevent memory lea         delete doomedNode;                  //Step last: Return the value         return output;              }          void printList()     {         // Create pointer initialized to point at first node         node* current;         current = headptr;                  while (current != nullptr)         {             // Print data of current node             cout << (*current).data << endl;                          // Move current to point ot next node             current = (*current).next;         }     }      };     int main()   {     linkedList mylist;          mylist.addFront("Cat");     mylist.addFront("Dog");     mylist.addFront("Lion");     mylist.addFront("Turtle");     mylist.addFront("Tiger");     mylist.addFront("Platypus");          mylist.printList();          cout << "Popping: " << mylist.popFront() << endl;     cout << "Popping: " << mylist.popFront() << endl;     cout << "Popping: " << mylist.popFront() << endl;     cout << "Popping: " << mylist.popFront() << endl;          mylist.addFront("Duck");     mylist.addFront("Goose");          mylist.printList();          // Need help here     mylist.addBack("Deer");     mylist.addBack("Bear");          mylist.printList();       return 0; }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

I need help being able to make a function that adds an item to the back of a doubly linked list to add an animal to the end of the list in C++

Here's what I got so far with the code

#include <iostream>

#include <string>

using namespace std;

 

class node

{

public:

    string data;// Data item

    node* next; // Point to next node

};

 

class linkedList

{

public:

    

    // Variables? (Attributes)

    node* headptr;

    

    // Functions (methods)

    linkedList()

    {

        headptr = nullptr;

    }

    

    // Add an item to front of list

    void addFront(string x)

    {

        // Declare a pointer variable

        node* p;

        

        // Create a new node, and store its address in p

        p = new node;

        

        // Put x into data field of the new node

        // Can also put p -> data = x, same thing.

        (*p).data = x;

        

        // Set next pointer of node to previous front item

        (*p).next = headptr;

        

        //Update the head pointer

        headptr = p;

    }

    

    // Add an item to the back of the list

    void addBack(string x)

    {

       // Help required here

    }

        

        

        

    

    // Remove and return front item from list

    string popFront()

    {

        // Step 1: Write down value to return

        string output;

        output = (*headptr).data;

        

        // Now, let's remove the front item

        

        // Write down node location for deletion

        node* doomedNode;

        doomedNode = headptr;

        

        // Move headpoint to remove node

        headptr = (*headptr).next;

        

        // Prevent memory lea

        delete doomedNode;

        

        //Step last: Return the value

        return output;

        

    }

    

    void printList()

    {

        // Create pointer initialized to point at first node

        node* current;

        current = headptr;

        

        while (current != nullptr)

        {

            // Print data of current node

            cout << (*current).data << endl;

            

            // Move current to point ot next node

            current = (*current).next;

        }

    }

    

};

 

 

int main()

 

{

    linkedList mylist;

    

    mylist.addFront("Cat");

    mylist.addFront("Dog");

    mylist.addFront("Lion");

    mylist.addFront("Turtle");

    mylist.addFront("Tiger");

    mylist.addFront("Platypus");

    

    mylist.printList();

    

    cout << "Popping: " << mylist.popFront() << endl;

    cout << "Popping: " << mylist.popFront() << endl;

    cout << "Popping: " << mylist.popFront() << endl;

    cout << "Popping: " << mylist.popFront() << endl;

    

    mylist.addFront("Duck");

    mylist.addFront("Goose");

    

    mylist.printList();

    

    // Need help here

    mylist.addBack("Deer");

    mylist.addBack("Bear");

    

    mylist.printList();

 

    return 0;

}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 1 images

Blurred answer
Knowledge Booster
Linked List Representation
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education