From 5b5e2b6d671064ddcb5c584f0533eee225471881 Mon Sep 17 00:00:00 2001 From: Iurii Tatishchev Date: Thu, 2 May 2024 16:14:13 -0700 Subject: [PATCH] 6.10 Lab*: Singly-Linked Lists (Park) Optimize searchList --- 01-stacks/.idea/vcs.xml | 6 ++++++ 03-lists/LinkedList.cpp | 5 +++-- 2 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 01-stacks/.idea/vcs.xml diff --git a/01-stacks/.idea/vcs.xml b/01-stacks/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/01-stacks/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/03-lists/LinkedList.cpp b/03-lists/LinkedList.cpp index 1e3dead..8168b90 100644 --- a/03-lists/LinkedList.cpp +++ b/03-lists/LinkedList.cpp @@ -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; }