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; }
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;
}
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 1 images