In: Computer Science
#include <bits/stdc++.h>
using namespace std;
string ltrim(const string &);
string rtrim(const string &);
#define BUFFLEN 10
/*
This code closely mimics the Python hash table we created in class this
week. 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;
}
};
//----------------------------------------------------------------------------
/* 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
// --------------------
}
};
//----------------------------------------------------------------------------
/* 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;
}
#include <bits/stdc++.h>
using namespace std;
string ltrim(const string &);
string rtrim(const string &);
#define BUFFLEN 10
/*
This code closely mimics the Python hash table we created in class this
week. 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;
}
};
//----------------------------------------------------------------------------
/* 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
person *p = head; // get address of first pointer
while(p!=NULL){ // run for all
list
if(p->zipcode
== zc){ // check if code matches
return p; // return pointer
}
p = p->next;
// move pointer to next
}
return NULL; // if not found return
null
//
--------------------
}
};
//----------------------------------------------------------------------------
/* 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;
}
/* OUTPUT */