C++ DATA STRUCTURES 2. The society has decided that the animals should be stored
ID: 3734738 • Letter: C
Question
C++ DATA STRUCTURES
2. The society has decided that the animals should be stored in a linked list instead of an iray Define a class (or classes) suitable for holding a linked list of circus animal information. Define any appropriate constructors Define a method that allows a circus animal to be added to the linked list. Define a method that is given a name and species as parameters, and searches the linked list for an exact match. It should return true if a match is found, and false otherwise. Because of government regulation, it is necessary to produce a linked list of all known gorillas. Write a method or function that takes a linked list as its parameter, and returns as its result a new linked list containing only the gorilla records. The gorillas should not be removed firom the original list, just collected together in a new one.Explanation / Answer
here is your program : ----------->>>>>>>>>>>
#include<iostream>
using namespace std;
class Node{
string name;
string species;
Node *next;
public:
Node(){
next = nullptr;
species = "";
name = "";
}
Node(string n,string s,Node *nex){
name = n;
species = s;
next = nex;
}
void setName(string n){
name = n;
}
void setSpecies(string s){
species = s;
}
string getName()const{
return name;
}
string getSpecies()const{
return species;
}
Node* getNext()const{
return next;
}
void setNext(Node *n){
next = n;
}
};
class LinkList{
Node *head;
int size;
public:
LinkList(){
head = nullptr;
size = 0;
}
void add(string n,string s){
Node *temp = new Node;
temp->setName(n);
temp->setSpecies(s);
temp->setNext(head);
head = temp;
size++;
}
int getSize()const{
return size;
}
Node* get(int i){
if(isEmpty()){
return nullptr;
}
if(i < 0 || i >= size){
return nullptr;
}
Node *temp = head;
for(int j = 0;j<i;j++){
temp = temp->getNext();
}
return temp;
}
bool search(string name,string species){
if(isEmpty()){
return false;
}
Node *temp = head;
while(temp != nullptr){
if(name == temp->getName() && species == temp->getSpecies()){
return true;
}
temp = temp->getNext();
}
return false;
}
bool isEmpty(){
if(size == 0){
return true;
}
return false;
}
void print(){
if(isEmpty()){
return;
}
Node *temp = head;
while(temp != nullptr){
cout<<" "<<temp->getName()<<" "<<temp->getSpecies();
temp = temp->getNext();
}
}
};
LinkList GorillaRecord(LinkList l){
LinkList tmp;
string sp = "Gorilla";
for(int i = 0;i<l.getSize();i++){
Node *temp = l.get(i);
if(temp->getSpecies() == sp){
tmp.add(temp->getName(),sp);
}
}
return tmp;
}
int main(){
LinkList ll;
ll.add("dkp","Gorilla");
ll.add("dkp1","Gorilla");
ll.add("dkp2","Monkey");
ll.add("dkp3","Gorilla");
ll.add("dkp4","Gorilla");
ll.add("dkp5","Lion");
ll.print();
cout<<" Gorilla Records ";
LinkList lg = GorillaRecord(ll);
lg.print();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.