Question

/* Write a function that looks for a particular person in their respective linked list The...

/*

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;
}

Homework Answers

Answer #1

////////////////// 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;
}
};
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in...
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 565 Error 2 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 761 I need this code to COMPILE and RUN, but I cannot get rid of this error. Please Help!! #include #include #include #include using namespace std; enum contactGroupType {// used in extPersonType FAMILY, FRIEND, BUSINESS, UNFILLED }; class addressType { private:...
8.19 LAB: Grocery shopping list (linked list: inserting at the end of a list) PLEASE ANSWER...
8.19 LAB: Grocery shopping list (linked list: inserting at the end of a list) PLEASE ANSWER IN C++ Given main(), define an InsertAtEnd() member function in the ItemNode class that adds an element to the end of a linked list. DO NOT print the dummy head node. Ex. if the input is: 4 Kale Lettuce Carrots Peanuts where 4 is the number of items to be inserted; Kale, Lettuce, Carrots, Peanuts are the names of the items to be added...
Complete the missing code for the constructors as indicated in the comments in all three header...
Complete the missing code for the constructors as indicated in the comments in all three header files. C++ mainDriver.cpp #include <string> #include "Address.h" #include "Date.h" #include "Person.h" using namespace std; int main() {    Person p1;    Person p2("Smith", "Bobby", "[email protected]", 111, "Main St", "Clemson",            "SC", 29630, 1, 31, 1998);    cout << endl << endl;    p1.printInfo();    p2.printInfo();    return 0; } Person.h #ifndef PERSON_H #define PERSON_H #include <iostream> #include <string> using namespace std; class...
c++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete....
c++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete. Return true or false to indicate success or failure. Delete the memory the node was using The algorithm for deleting a Node to a Linked List (general case): ● Begin at the head. ● Compare the id to the current node. ● If search id > current id, stop. ● Detach the current Node ○ current->previous->next = current->next ○ current->next->previous = current->previous ● Deallocate...
C++ See the provided specification files. Complete the implementation for each as a separate file. void...
C++ See the provided specification files. Complete the implementation for each as a separate file. void seen(std::string); If there is already a Word object in the Words list, then the number of occurrences for this word is incremented. If there is no Word object for this word already, create a new word object with occurrence =1, and insert this object into the list of Word objects. std::string getNextWord(); Returns the next word of the list and sets the currentItem pointer...
Given the following specifications for an array-based unsorted list, implement all of the functions (declared below)...
Given the following specifications for an array-based unsorted list, implement all of the functions (declared below) and a write a driver code to test all of your implementations. // Define a structure to use as the list item struct ListItem { int key; int Data; }; #define MAX_SIZE 50 // Define maximum length of the list class UnsortedArray { private: int head; // Index to head of the list ListItem theList[MAX_SIZE]; // The list public: UnsortedArray(); // Class constructor ~...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h"...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h" template StackLinked::StackLinked (int maxNumber) { } template StackLinked::StackLinked(const StackLinked& other) { } template StackLinked& StackLinked::operator=(const StackLinked& other) { } template StackLinked::~StackLinked() {    clear(); } template void StackLinked::push(const DataType& newDataItem) throw (logic_error) {    } template DataType StackLinked::pop() throw (logic_error) { } template void StackLinked::clear() {    StackNode* t;    while ( top != NULL)    {        t = top;       ...
Data Structures using C++ Searching a Linked List Here are the declarations for a simple unsorted...
Data Structures using C++ Searching a Linked List Here are the declarations for a simple unsorted linked list of ints that ends in a null pointer. //=============================================================== class Cell { friend class UList; private: int data; Cell* next; Cell( int dt, Cell* nx=nullptr ) : data(dt), next(nx) {} }; //=============================================================== class UList { private: Cell* head = nullptr;    // stationary head pointer. Cell* scan = nullptr;          // for walking down the List. Cell* follow = nullptr; public: void find( int...
In main.cpp, write a templated function more which takes in two variables of the same type...
In main.cpp, write a templated function more which takes in two variables of the same type and returns whichever variable is greater than (>) the other. You can and may need to add something to the food class in order for more to be able to be called properly. //food.h #ifndef _FOOD_H #define _FOOD_H #include <iostream> #include <string> using namespace std; class Food { private: string name; int quantity; public: Food(); void setName(string newName); void setQuantity(int newQuantity); string getName(); int...
The following program creates a linked list which contains 5 links. Add a method called doubleValue()...
The following program creates a linked list which contains 5 links. Add a method called doubleValue() to the LinkedList class. The doubleValue() method must double the value of the number data field in each link. Add the required code to the main() method to call the doubleValue() method and display the revised list. public class Link { private int number; private Link next;      public Link(int x) { number = x; }    public void displayLink() { System.out.println("The number is:...