6.10 Lab*: Singly-Linked Lists (Park)

Optimize searchList
This commit is contained in:
Iurii Tatishchev 2024-05-02 16:14:13 -07:00
parent 4ae56c5a42
commit 5b5e2b6d67
Signed by: CaZzzer
GPG Key ID: 28BE602058C08557
2 changed files with 9 additions and 2 deletions

6
01-stacks/.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

View File

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