/*
Write a function that looks for a particular person in their respective linked list
The only place you need to write code is the "find" method in the linked list class
*/
#include <bits/stdc++.h>
using namespace std;
string ltrim(const string &);
string rtrim(const string &);
#define BUFFLEN 10
/*
Each "person" is defined by their name, zipcode, and their pet's
name.
Persons are hashed by their zipcode.
*/
//----------------------------------------------------------------------------
/* function declarations
------------------------*/
int computeKey(int);
void add_to_buffer(string,int,string);
void find_in_buffer(int);
//----------------------------------------------------------------------------
/* define a "person"
--------------------*/
class person{
public:
// variables that define a person
string name;
int zipcode;
string petsname;
person *next;
// constructor
person(string nm, int zc, string pn, person *n=NULL){
name = nm;
zipcode = zc;
petsname = pn;
next = n;
}
// print a person
void print(){
cout << name << " lives in " << zipcode <<
" with pet " << petsname << endl;
}w
};
//----------------------------------------------------------------------------
/* define a Linked List
-------------------------*/
class LL{
public:
// just the one variable
person *head;
// constructor
LL(){head=NULL;}
// create a new person and push them onto the List
void push(string nm, int zc, string pn){
person *p = new person(nm,zc,pn,head);
head = p;
}
// find a person in the list
// inputs provided: the person's zipcode
// return: a pointer to either the person or to NULL (if the person
isn't found)
person *find(int zc){
// --------------------
// YOUR CODE GOES HERE
// --------------------
}
};
//--t--------------------------------------------------------------------------
/* Define the buffer, which is an array of Linked Lists
-------------------------------------------------------*/
LL buffer[BUFFLEN];
//----------------------------------------------------------------------------
/* Function definitions
-------------------------------------------------------*/
void add_to_buffer(string nm, int zc, string pn){
int key = computeKey(zc);
buffer[key].push(nm,zc,pn);
}
int computeKey(int val){
return val%BUFFLEN;
}
void find_in_buffer(int zc){
int key = computeKey(zc);
person *p = buffer[key].find(zc);
if (p!=NULL)
p->print();
else
cout << zc << " not found" << endl;
}
void test_buffer(int zip) {
add_to_buffer("alice",19122,"hoot");
add_to_buffer("bob",12193,"rover");
add_to_buffer("charlie",27707,"lady");
add_to_buffer("dan",90210,"bubbles");
add_to_buffer("elise",20904,"marbles");
add_to_buffer("fran",86753,"moose");
find_in_buffer(zip);
}
int main()
{
string zip_temp;
getline(cin, zip_temp);
int zip = stoi(ltrim(rtrim(zip_temp)));
test_buffer(zip);
return 0;
}
string ltrim(const string &str) {
string s(str);
s.erase(
s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun<int,
int>(isspace)))
);
return s;
}
string rtrim(const string &str) {
string s(str);
s.erase(
find_if(s.rbegin(), s.rend(), not1(ptr_fun<int,
int>(isspace))).base(),
s.end()
);
return s;
}
////////////////// find method //////////////////
person *find(int zc){
/*
* the process goes as follow
* initially the head is pointed to first node of that index
* now we are creating a temp pointer and pointing to head node i.e. first node
* then we traverse through all nodes by setting temp node to be its next node until the temp node becomes null
* at any iteration if temp node's zipcode equals the given zc then it returns that pointer
* if no node is found with the required zc then it exists while loop and returns null
* */
person *temp =head; // create a temp pointer that points to head node
while(temp!=NULL){
if(temp->zipcode==zc){
return temp;
}
temp=temp->next;
}
return NULL;
}
};
Get Answers For Free
Most questions answered within 1 hours.