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

Do the following: 1. Complete the function s insertName , removeFirst . The inse

ID: 3770072 • Letter: D

Question

Do the following:

1.Complete the functions insertName, removeFirst. The insertName function generates a

node dynamically, adds the node to the front of the list, and returns a pointer to the new list.

The removeFirst function deletes the first element and returns the updated list.

2.Using the given functions in the main function complete the following in order:

a.Add Three Different Names to the Linked List (nameList)

b.Print the List to the Console

c.Removethe Names Using the removeFirstfunction

d.Again, print the List to the Console

e.Clear the list freeing up the memory

Example printed to console: Adding three names Jim, James, John

After removing the first name the elements in the list are James, John

Here are parts of the code which you will build upon:

#include<iostream>
#include<string>
#include<iomanip>
using namespace std;

struct Node
{
   string name;
   Node *next;
};

Node* insertName(string name, Node* list);
void printNames(Node* list);
Node* removeFirst(Node *list);
Node* removeName(string name, Node *list);
void clearList(Node* list);

int main()
{
   Node *nameList = 0;

   // Add Three Different Names to the Linked List

   // Print the List to the Console

   // Remove the Name Using the removeFirst function

   // Print the List to the Console

   // Clear The List, freeing up the memory


   return 0;
}

Node* insertName(string name, Node* list)
{
   // Complete The Function

}

void printNames(Node* list)
{
   Node *curNode = list;
  
   // The list is iterated through, curNode is a pointer
   while(curNode != 0)
   {
       cout << curNode->name;
       curNode = curNode->next;
       if(curNode != 0)
           cout << ", ";
   }
   cout << endl;
}


// Removes the first node in the list
Node* removeFirst(Node *list)
{
   // Complete the function
}

// Removes a node which contains the same name
Node* removeName(string name, Node *list)
{
   // Find Element
   Node *curNode = list, *prevNode = 0;
   while(curNode != 0)
   {
       if(curNode->name == name)
           break;

       prevNode = curNode;
       curNode = curNode->next;
   }

   // Element May Not be Found, return list with no action
   if(curNode == 0)
       return list;

   // Otherwise previous Node must Point To Next
   // Check to see if it was the first element in the list
   if(prevNode == 0)
   {
       prevNode = curNode->next;
       delete curNode;
       return prevNode;
   }

   prevNode->next = curNode->next;
   delete curNode;
   return list;
}

// Deletes Entire List Freeing the Memory
void clearList(Node* list)
{
   while(list != 0)
       list = removeFirst(list);
}

Explanation / Answer

Code:

#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
struct Node
{
string name;
Node *next;
};
Node* insertName(string name, Node* list);
void printNames(Node* list);
Node* removeFirst(Node *list);
Node* removeName(string name, Node *list);
void clearList(Node* list);
int main()
{
Node *nameList = 0;
cout<<"Adding three names Jim, James, John"<<endl;
nameList = insertName("John",nameList);
nameList = insertName("James",nameList);
nameList = insertName("Jim",nameList);
cout<<"before removing the elements in the list are ";
printNames(nameList);
nameList = removeFirst(nameList);
cout<<"After removing the first name the elements in the list are ";
printNames(nameList);
clearList(nameList);
return 0;
}
Node* insertName(string name, Node* list)
{
Node *temp = new Node;
temp->name = name;
temp->next = list;
return temp;

}
void printNames(Node* list)
{
Node *curNode = list;
  
// The list is iterated through, curNode is a pointer
while(curNode != 0)
{
cout << curNode->name;
curNode = curNode->next;
if(curNode != 0)
cout << ", ";
}
cout << endl;
}

// Removes the first node in the list
Node* removeFirst(Node *list)
{
// Complete the function
if(list==NULL)
{
cout<<"no elements in the list ";
return NULL;
}
Node *temp = list;
list = list->next;
delete temp;
return list;
}
// Removes a node which contains the same name
Node* removeName(string name, Node *list)
{
// Find Element
Node *curNode = list, *prevNode = 0;
while(curNode != 0)
{
if(curNode->name == name)
break;
prevNode = curNode;
curNode = curNode->next;
}
// Element May Not be Found, return list with no action
if(curNode == 0)
return list;
// Otherwise previous Node must Point To Next
// Check to see if it was the first element in the list
if(prevNode == 0)
{
prevNode = curNode->next;
delete curNode;
return prevNode;
}
prevNode->next = curNode->next;
delete curNode;
return list;
}
// Deletes Entire List Freeing the Memory
void clearList(Node* list)
{
while(list != 0)
list = removeFirst(list);
}

output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote