6.10 Lab*: Singly-Linked Lists (Park)
Optimize searchList
This commit is contained in:
@@ -118,12 +118,13 @@ bool LinkedList::searchList(string target, Park &dataOut) const {
|
||||
pCur = head->next;
|
||||
|
||||
// While pCur points to a node, traverse the list.
|
||||
while (pCur && pCur->park.getCode() != target) {
|
||||
// Since the list is sorted, we can stop when the target is less than the current node.
|
||||
while (pCur && pCur->park.getCode() < target) {
|
||||
pCur = pCur->next;
|
||||
}
|
||||
|
||||
// If the target was found, copy the data
|
||||
if (pCur) {
|
||||
if (pCur && pCur->park.getCode() == target) {
|
||||
dataOut = pCur->park;
|
||||
found = true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user