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
How do I get this portion of my List Iterator code to work? This is a...
How do I get this portion of my List Iterator code to work? This is a portion of the code in the AddressBookApp that I need to work. Currently it stops at Person not found and if it makes it to the else it gives me this Exception in thread "main" java.lang.NullPointerException    at AddressBookApp.main(AddressBookApp.java:36) iter.reset(); Person findPerson = iter.findLastname("Duck"); if (findPerson == null) System.out.println("Person not found."); else findPerson.displayEntry(); findPerson = iter.findLastname("Duck"); if (findPerson == null) { System.out.println("Person not found.");...
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:...
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node...
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node class Node { int value; Node prev; Node next; // Constructor to create a new node Node(int d) { value = d; } } // Inserting a node at the front of the list public void add(int newData) { // allocate node and put in the data Node newNode = new Node(newData); // Make the next of new node as head // and previous...
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...
(C++) Suppose you want to use a linked list where the items stored in the list...
(C++) Suppose you want to use a linked list where the items stored in the list are strings from the standard library string class, how would you change the node1.h header file? Header File: #ifndef MAIN_SAVITCH_NODE1_H #define MAIN_SAVITCH_NODE1_H #include <cstdlib> // Provides size_t and NULL namespace main_savitch_5 { class node { public: // TYPEDEF typedef double value_type; // CONSTRUCTOR node(const value_type& init_data=value_type( ), node* init_link=NULL) { data_field = init_data; link_field = init_link; } // MODIFICATION MEMBER FUNCTIONS node* link( )...
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...
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next...
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next reference of the tail node is set to refer back to the head of the list (rather than null) . Start from the SinglyLinkedList provided in the Lecture (not the built-in Java LinkedList class). 2. Starting from  SinglyLinkedList you will need to modify or complete methods: first(), last(), addFirst(), addLast(), removeFirst(), toString(), and also create a new rotate() method which will move the tail to...
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...
Write the implementation of a non-member function Node* deleteSecond(Node* head_ptr), where Node is a class defined...
Write the implementation of a non-member function Node* deleteSecond(Node* head_ptr), where Node is a class defined on page 257. The function takes as input a pointer to the head of a linked list consisting of numbers. The function should remove the second item in the list. If the list had only one item, the function should delete that item. If the list was empty, then let the list remain empty. In all cases return the new head of the list...
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 ~...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT