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

HW 4b -Write a program that creates a linked list of Dogs Create a new project a

ID: 3738468 • Letter: H

Question

HW 4b -Write a program that creates a linked list of Dogs Create a new project and name it: Linked List Create 2 Files in the project: . Dog.h . main.cpp Description: Create a linked list of Dogs. . Each node is an object of struct Dog o Each Dog object has a name (string). . Place your struct Dog in a header file named: Dog.h In driver.cpp, use a while loop to create a linked list of Dog objects. o Insert at least 3 dogs into a linked list. to quit the loop OUTPUT Enter a dog name ( to quit): ?with each iteration do the following: .Ask the user for a Dog's name. . Read in the name. . Call a function named: insertNode0 Bud Big Boy Spot White Lightning User enters to quit Display the list (Y/N)? y The function is a void-returning function.// acreen clears LIST OF DOGS . Pass the head pointer to the function. . The function created a new Dog object . Assign the name to the new object.2.)pot NAME Assign the name to the new object.: pste Lightning Connect the new object to the list. Connect the new objt the itBig y Bud . After creating a list of at least 3 Dogs, clear the screen. Prompt the user if he or she wishes to see the list o If yes, then call displayAllNodes0 to display the list. . Then clear the screen. Finally, call a searchList0 function. o The function prompts the user to enter a dog name. o Then the function uses while loop to go through the list to find the name. o If the name is in the list, output that the name has been found. o If not in the list, output that the name was not found.

Explanation / Answer

//Dog.h
struct Dog
{

string name;
struct Dog *link;
};
#include<Dog>
#include<iostream>
#include<conio>
using namespace std;
struct Dog h[3],*head=NULL,*temp;
void insertNode()
{
temp=new Dog();
string n;
getline(cin,n);
if(head==NULL)
{
temp->name=n;
temp->link=NULL;
}
else
{
temp->name=n;
temp->link=head;
head=temp;
}
}
void searchList()
{
string nam;
std::cout<<"enter a dog name:";
getline(cin,nam);
bool flag=false;
while(head!=NULL)
{
if(strcmp(nam,head->name)==0)
flag=true;
head=head->link;
}
if(flag)
std::cout<<nam;
else
std::cout<<"the name was not found";
}

void displayAllNodes()
{
struct Dog *p;
for(p=head;p!=NULL;p=p->link)
std::cout<<p->name<<"->";
}
int main()
{
string name;
int i=0;
while(i<3){
insertNode();
i++;
clrscr();
char c;
std::cout<<"are you wishing to see the list";
std::cin>>c;
if(c=='y'||c=='Y')
displayAllNodes();
searchList();
}
return 0;
}