Question: C + + Programming Assignment: Implementing a Simple Singly Linked List Objective: Create a singly linked list that supports adding nodes, printing the list, and

C++ Programming Assignment: Implementing a Simple Singly Linked List
Objective:
Create a singly linked list that supports adding nodes, printing the list, and deleting nodes with a
given string.
Assignment Details:
1. Create a Node Structure:
o Define a Node class with a std::string data field and a pointer to the next node.
o Include a default constructor, a parameterized constructor, and a destructor.
2. Implement an add_node Function:
o This function should take a pointer to the head of the linked list and a string to
add as a new node at the end of the list.
3. Implement a print_list Function:
o This function should take a pointer to the head of the linked list and print all the
nodes in the list.
4. Implement a delete_node Function:
o This function should take a pointer to the head of the linked list and a string to
delete the corresponding node if it exists.
Requirements:
Node Class Definition:
#include
#include
class Node {
public:
std::string data;
Node* next;
// Default constructor
Node() : data(""), next(nullptr){}
// Parameterized constructor
Node(std::string d) : data(d), next(nullptr){}
// Destructor
~Node(){
// Clean up resources if necessary
// In this simple case, there's nothing specific to clean up
}
};
Add Node Function:
void add_node(Node*& head, const std::string& new_data){//insert code here
}
Print List Function:
void print_list(Node* head){
//insert code here
}
Delete Node Function:
void delete_node(Node*& head, const std::string& target){
//insert code here
}
Example Usage:
int main(){
Node* head = nullptr;
// Adding nodes
add_node(head, "first");
add_node(head, "second");
add_node(head, "third");
// Printing list
std::cout << "List after adding nodes: ";
print_list(head);
// Deleting a node
delete_node(head, "second");
// Printing list after deletion
std::cout << "List after deleting 'second': ";
print_list(head);
// Clean up memory
while (head != nullptr){
Node* temp = head;
head = head->next;
delete temp;
}
return 0;
}
Additional Instructions:
1. Testing: Test the functions with various inputs to ensure they work correctly. Test edge
cases like deleting from an empty list or deleting a non-existent node.
2. Memory Management: Ensure proper memory management to avoid memory leaks.
Delete all nodes before the program terminates.
3. Documentation: Comment the code to explain the logic behind each function.Submission:
Submit the C++ source code file containing the implementation of the linked list, including the
main function that demonstrates adding, printing, and deleting nodes.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!