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

Hey guys, I was hoping somebody could help me with creating a function to be abl

ID: 3701090 • Letter: H

Question

Hey guys, I was hoping somebody could help me with creating a function to be able to modify a node in a linked list in C++. Below is my code. Please help! (And i know the code isnt formatted correctly I'm sorry)

#include

#include

#include

#include

struct node {

char name[20]; // Name of up to 20 letters

int age; // D.O.B. would be better

float height; // In metres

node *nxt; // Pointer to next node

};

using namespace std;

class LinkedLists {

public:

node *add_node_at_start (node *start_ptr);

node *add_node_at_end (node *start_ptr);

node *delete_start_node(node *start_ptr);

node *delete_end_node(node *start_ptr);

node *delete_all_node(node *start_ptr);

node * search(node *start_ptr);

void print_list(node *ptr);

};

node * LinkedLists::add_node_at_start (node *start_ptr)

{

node *temp; // Temporary pointers

// Reserve space for new node and fill it with data

temp = new node;

cout << "Please enter the name of the person: ";

cin >> temp->name;

cout << "Please enter the age of the person : ";

cin >> temp->age;

cout << "Please enter the height of the person : ";

cin >> temp->height;

temp->nxt = start_ptr;

start_ptr = temp;

return start_ptr;

}

node * LinkedLists::add_node_at_end (node *start_ptr)

{

node *temp; // Temporary pointers

// Reserve space for new node and fill it with data

temp = new node;

cout << "Please enter the name of the person: ";

cin >> temp->name;

cout << "Please enter the age of the person : ";

cin >> temp->age;

cout << "Please enter the height of the person : ";

cin >> temp->height;

temp->nxt = NULL;

// Set up link to this node

if (start_ptr == NULL)

start_ptr = temp;

else

{

node *temp2 = start_ptr; // We know this is not NULL - list not empty!

while (temp2->nxt != NULL)

{

temp2 = temp2->nxt; // Move to next link in chain

}

temp2->nxt = temp; // makes the last node point to the new node which points to Null

}

return start_ptr;

}

node * LinkedLists::delete_start_node(node *start_ptr)

{

if (start_ptr == NULL) {

cout << "List is empty -- cannot delete from the empty list!" << endl;

return NULL;

}

else

{

node *temp;

temp = start_ptr;

start_ptr = start_ptr->nxt;

delete temp;

return start_ptr;

}

}

node * LinkedLists::delete_end_node(node *start_ptr)

{

node *temp1, *temp2;

if (start_ptr == NULL) {

cout << "Cannot delete! The list is empty!" << endl;

return NULL;

}

else if (start_ptr->nxt == NULL) {

delete start_ptr;

return NULL;

}

else

{

temp1 = start_ptr;

while (temp1->nxt != NULL)

{

temp2 = temp1;

temp1 = temp1->nxt;

}

delete temp1;

temp2->nxt = NULL;

return start_ptr;

}

}

node * LinkedLists::search(node *start_ptr)

{

int choice;

cout << "Please select search criteria ";

cout << "1. By name ";

cout << "2. By age ";

cout << "3. By height ";

cin >> choice;

switch (choice)

{

case 1:

char nameSearch[20];

cout << "Please enter the name ";

cin >> nameSearch;

break;

case 2:

int ageSearch;

cout << "Please enter the age ";

cin >> ageSearch;

break;

case 3:

float heightSearch;

cout << "Please enter the height ";

cin >> heightSearch;

break;

default:

break;

}

}

void LinkedLists::print_list(node *ptr)

{

/*The temporary pointer moves along the list, displaying the details of the nodes it comes across. At each stage, it can get hold of the next node in the list by using the nxt pointer of the node it is currently pointing to */

cout << "The linked list currently : " << endl;

node *temp = ptr;

do {

if (temp == NULL)

cout << "The list is empty" << endl;

else

{

// Display details for what temp points to

cout << "Name : " << temp->name;

cout << ", Age : " << temp->age;

cout << ", Height : " << temp->height << " -->" << endl;

// Move to next node (if present)

temp = temp ->nxt;

}

} while (temp != NULL);

}

node * LinkedLists::delete_all_node(node *start_ptr)

{

node *temp1, *temp2;

if (start_ptr == NULL) {

cout << "Cannot delete! The list is empty!" << endl;

}

else if (start_ptr->nxt == NULL) {

delete start_ptr;

}

else

{

temp1 = start_ptr;

while (temp1->nxt != NULL)

{

temp2 = temp1;

temp1 = temp1->nxt;

delete temp2;

}

}

return NULL;

}

int main() {

LinkedLists LL;

node *start_ptr = NULL;

// LL.print_list(start_ptr);

int choice;

// cout << "Adding 4 people to the empty list..." << endl;

// for (int i=0; i<4; i++)

// start_ptr = LL.add_node_at_end(start_ptr);

// LL.print_list(start_ptr);

// Menu out

while(choice != 9){

cout << "Menu" << endl

<< "1 - Add first person." << endl

<< "2 - Add more people" << endl

<< "3 - Delete first guy." << endl

<< "4 - Delete last guy." << endl

<< "5 - Search for person(Only works if list is not empty, please add atleast one person before doing this)." << endl

<< "6 - Modify Person. (Nothing yet)" << endl

<< "7 - Display Whole List." << endl

<< "8 - Destroy List." << endl

<< "9 - Exit please..." << endl; // exit program

//User input

cin >> choice;

switch(choice){

case 1:

cout << "Adding 1st person... ";

start_ptr = LL.add_node_at_start (start_ptr);

LL.print_list(start_ptr);

break;

case 2:

cout << "Adding another person... ";

start_ptr = LL.add_node_at_end (start_ptr);

LL.print_list(start_ptr);

break;

case 3:

cout << "Deleting first person in the list..." << endl;

start_ptr = LL.delete_start_node(start_ptr);

LL.print_list(start_ptr);

break;

case 4:

cout << "Deleting last person in the list..." << endl;

start_ptr = LL.delete_end_node(start_ptr);

LL.print_list(start_ptr);

break;

case 5:

cout << "Searching for person... ";

start_ptr = LL.search(start_ptr);

LL.print_list(start_ptr);

break;

case 6:

cout << "Modify Person... ";

break;

case 7:

cout << "Displaying list..." << endl;

LL.print_list(start_ptr);

break;

case 8:

cout << "Deleting whole list..." << endl;

LL.delete_all_node(start_ptr);

LL.print_list(start_ptr);

break;

case 9: // exit command

cout << "ok bai";

return 0;

default: // ErR0r

cout << "ok, try that again." << endl;

}

}

// Remove end

cout << "Deleting last person in the list..." << endl;

start_ptr = LL.delete_end_node(start_ptr);

LL.print_list(start_ptr);

// Add node start

start_ptr = LL.add_node_at_start(start_ptr);

LL.print_list(start_ptr);

// Delete all

start_ptr = LL.delete_all_node(start_ptr);

LL.print_list(start_ptr);

}

Explanation / Answer

#include<iostream>

#include<cstdlib>

#include<cstring>

using namespace std;

struct node {

char name[20]; // Name of up to 20 letters

int age; // D.O.B. would be better

float height; // In metres

node *nxt; // Pointer to next node

};

using namespace std;

class LinkedLists {

public:

node *add_node_at_start (node *start_ptr);

node *add_node_at_end (node *start_ptr);

node *delete_start_node(node *start_ptr);

node *delete_end_node(node *start_ptr);

node *delete_all_node(node *start_ptr);

node * search(node *start_ptr);

void print_list(node *ptr);

// modify the node with the given name of the person

node* modify( node *start_ptr, char person[20] );

};

node * LinkedLists::add_node_at_start (node *start_ptr)

{

node *temp; // Temporary pointers

// Reserve space for new node and fill it with data

temp = new node;

cout << "Please enter the name of the person: ";

cin >> temp->name;

cout << "Please enter the age of the person : ";

cin >> temp->age;

cout << "Please enter the height of the person : ";

cin >> temp->height;

temp->nxt = start_ptr;

start_ptr = temp;

return start_ptr;

}

node * LinkedLists::add_node_at_end (node *start_ptr)

{

node *temp; // Temporary pointers

// Reserve space for new node and fill it with data

temp = new node;

cout << "Please enter the name of the person: ";

cin >> temp->name;

cout << "Please enter the age of the person : ";

cin >> temp->age;

cout << "Please enter the height of the person : ";

cin >> temp->height;

temp->nxt = NULL;

// Set up link to this node

if (start_ptr == NULL)

start_ptr = temp;

else

{

node *temp2 = start_ptr; // We know this is not NULL - list not empty!

while (temp2->nxt != NULL)

{

temp2 = temp2->nxt; // Move to next link in chain

}

temp2->nxt = temp; // makes the last node point to the new node which points to Null

}

return start_ptr;

}

node * LinkedLists::delete_start_node(node *start_ptr)

{

if (start_ptr == NULL) {

cout << "List is empty -- cannot delete from the empty list!" << endl;

return NULL;

}

else

{

node *temp;

temp = start_ptr;

start_ptr = start_ptr->nxt;

delete temp;

return start_ptr;

}

}

node * LinkedLists::delete_end_node(node *start_ptr)

{

node *temp1, *temp2;

if (start_ptr == NULL) {

cout << "Cannot delete! The list is empty!" << endl;

return NULL;

}

else if (start_ptr->nxt == NULL) {

delete start_ptr;

return NULL;

}

else

{

temp1 = start_ptr;

while (temp1->nxt != NULL)

{

temp2 = temp1;

temp1 = temp1->nxt;

}

delete temp1;

temp2->nxt = NULL;

return start_ptr;

}

}

node * LinkedLists::search(node *start_ptr)

{

int choice;

cout << "Please select search criteria ";

cout << "1. By name ";

cout << "2. By age ";

cout << "3. By height ";

cin >> choice;

switch (choice)

{

case 1:

char nameSearch[20];

cout << "Please enter the name ";

cin >> nameSearch;

break;

case 2:

int ageSearch;

cout << "Please enter the age ";

cin >> ageSearch;

break;

case 3:

float heightSearch;

cout << "Please enter the height ";

cin >> heightSearch;

break;

default:

break;

}

}

void LinkedLists::print_list(node *ptr)

{

/*The temporary pointer moves along the list, displaying the details of the nodes it comes across. At each stage, it can get hold of the next node in the list by using the nxt pointer of the node it is currently pointing to */

cout << "The linked list currently : " << endl;

node *temp = ptr;

do {

if (temp == NULL)

cout << "The list is empty" << endl;

else

{

// Display details for what temp points to

cout << "Name : " << temp->name;

cout << ", Age : " << temp->age;

cout << ", Height : " << temp->height << " -->" << endl;

// Move to next node (if present)

temp = temp ->nxt;

}

} while (temp != NULL);

}

node * LinkedLists::delete_all_node(node *start_ptr)

{

node *temp1, *temp2;

if (start_ptr == NULL) {

cout << "Cannot delete! The list is empty!" << endl;

}

else if (start_ptr->nxt == NULL) {

delete start_ptr;

}

else

{

temp1 = start_ptr;

while (temp1->nxt != NULL)

{

temp2 = temp1;

temp1 = temp1->nxt;

delete temp2;

}

}

return NULL;

}

// modify the list with name same as passed argument

// person[] is the name of the node which is to be modified

node* LinkedLists::modify(node *start_ptr, char person[20] )

{

    node *trav = start_ptr;

    //cout<<"Hi";

    // iterate through the list

  while( trav )

    {

        //cout<<trav->name<<" ";

        // if the current node is the required node to be changed

        if( !strcmp( trav->name , person ) )

        {

            cout << "Please enter the new name of the person: ";

            cin >> trav->name;

           

            cout << "Please enter the new age of the person : ";

            cin >> trav->age;

           

            cout << "Please enter the new height of the person : ";

            cin >> trav->height;

           

            break;

        }

       

        // move to next node

        trav = trav->nxt;

    }

   

    if( !trav )

        cout<<"No such person in the list. ";

   

    return start_ptr;

}

int main() {

LinkedLists LL;

node *start_ptr = NULL;

// LL.print_list(start_ptr);

char temp[20];

int choice;

// cout << "Adding 4 people to the empty list..." << endl;

// for (int i=0; i<4; i++)

// start_ptr = LL.add_node_at_end(start_ptr);

// LL.print_list(start_ptr);

// Menu out

while(choice != 9){

cout << "Menu" << endl

<< "1 - Add first person." << endl

<< "2 - Add more people" << endl

<< "3 - Delete first guy." << endl

<< "4 - Delete last guy." << endl

<< "5 - Search for person(Only works if list is not empty, please add atleast one person before doing this)." << endl

<< "6 - Modify Person. (Nothing yet)" << endl

<< "7 - Display Whole List." << endl

<< "8 - Destroy List." << endl

<< "9 - Exit please..." << endl // exit program

<< "10 - Modify node..." << endl; // exit program

//User input

cin >> choice;

switch(choice){

case 1:

cout << "Adding 1st person... ";

start_ptr = LL.add_node_at_start (start_ptr);

LL.print_list(start_ptr);

break;

case 2:

cout << "Adding another person... ";

start_ptr = LL.add_node_at_end (start_ptr);

LL.print_list(start_ptr);

break;

case 3:

cout << "Deleting first person in the list..." << endl;

start_ptr = LL.delete_start_node(start_ptr);

LL.print_list(start_ptr);

break;

case 4:

cout << "Deleting last person in the list..." << endl;

start_ptr = LL.delete_end_node(start_ptr);

LL.print_list(start_ptr);

break;

case 5:

cout << "Searching for person... ";

start_ptr = LL.search(start_ptr);

LL.print_list(start_ptr);

break;

case 6:

cout << "Modify Person... ";

break;

case 7:

cout << "Displaying list..." << endl;

LL.print_list(start_ptr);

break;

case 8:

cout << "Deleting whole list..." << endl;

LL.delete_all_node(start_ptr);

LL.print_list(start_ptr);

break;

case 9: // exit command

cout << "ok bai";

return 0;

case 10 : cout<<"Enter the name of the person to be modified : ";

          cin>>temp;

           start_ptr = LL.modify(start_ptr, temp);

default: // ErR0r

cout << "ok, try that again." << endl;

}

}

// Remove end

cout << "Deleting last person in the list..." << endl;

start_ptr = LL.delete_end_node(start_ptr);

LL.print_list(start_ptr);

// Add node start

start_ptr = LL.add_node_at_start(start_ptr);

LL.print_list(start_ptr);

// Delete all

start_ptr = LL.delete_all_node(start_ptr);

LL.print_list(start_ptr);

}