Reputation: 23
so for my program, we are using singly linked list and i'm having trouble find records. we have hard-coded two records with rating 5 for example. When we first start my program and search for them, both records show up, but when we enter a new record with a different rating then it says rating not found or address not found. What is the problem in our code ?
p.s - btw this is a group assignment and my group mate did this, so i'm just trying to figure out how can i fix this...
struct tutor
{
int tutorid, rate;
string tutoraddress;
tutor* next;
};
class record
{
public:
//head as first in node
tutor* head;
tutor* tail;
//Get size of node
int getSize;
//Constructor to flush and renew the value of node to null, and size to 0
record()
{
this->head = NULL;
this->tail = NULL;
this->getSize = 0;
}
void exist()
{
tutor* n = new tutor;
//First
n->tutorid = 1;
n->tutoraddress = "Puchong";
n->rate = 5;
n->next = NULL;
if (head == NULL)
{
head = n;
}
else
{
tail = head;
while (tail->next != NULL)
{
tail = tail->next;
}
tail->next = n;
}
getSize++;
}
void exist1()
{
tutor* n = new tutor;
//Second
n->tutorid = 2;
n->tutoraddress = "Puchong";
n->rate = 5;
n->next = NULL;
if (head == NULL)
{
head = n;
}
else
{
tail = head;
while (tail->next != NULL)
{
tail = tail->next;
}
tail->next = n;
}
getSize++;
}
int find()
{
//create n node and assign to head
//then traverse to end of list to find the
//search value
int ch = 0;
int rating = 0;
int id = 0;
string address;
while (true)
{
cout << "\n1>Find ID\n";
cout << "2>Find rating\n";
cout << "3>Find address\n";
cout << "4>Quit\n";
cout << "Select menu: ";
cin >> ch;
if (ch == 1)
{
cout << "\nMenu selected: " << ch << "\n";
cout << "Enter ID: ";
cin >> id;
tutor* n = head;
while (n != NULL)
{
if (n->tutorid == id)
{
cout << "\nID: " << n->tutorid << "\n";
system("pause");
return true;
}
else
{
n = n->next;
}
}
cout << "\nID not found\n";
system("pause");
return false;
}
else if (ch == 2)
{
cout << "\nMenu selected: " << ch << "\n";
cout << "Enter rating: ";
cin >> rating;
tutor* n = head;
while (n != NULL)
{
if (n->rate == rating)
{
cout << "\nID: " << n->tutorid << "\n";
n = n->next;
}
else
{
n = n->next;
cout << "\nRate not found\n";
break;
}
}
}
else if (ch == 3)
{
cout << "\nMenu selected: " << ch << "\n";
cout << "Enter address: ";
cin >> address;
tutor* n = head;
while (n != NULL)
{
if (n->tutoraddress == address)
{
cout << "\nID: " << n->tutorid << "\n";
n = n->next;
}
else
{
n = n->next;
cout << "\nAddress not found\n";
break;
}
}
}
else
{
cout << "\nEnd\n";
system("pause");
break;
}
}
}
}
Upvotes: 0
Views: 270
Reputation: 57678
To answer your question about searching a linked list, here is an example function to search the list by tutoraddress
:
tutor * record::find_by_tutor_address(const std::string& key)
{
tutor * p_node = head;
while (p_node != nullptr)
{
if (p_node->tutoraddress == key)
{
break;
}
p_node = p_node->next;
}
return p_node;
}
Searching by the other fields can use a similar function.
The fundamental process is to start at the head node and follow the links until the node is found. The function will return nullptr
if the key is not found or it will return a pointer to the node that contains the key.
Upvotes: 2