From 6b8435d5f66ffbc7d04e6f5f12d47b87fa0d1dd3 Mon Sep 17 00:00:00 2001 From: Shaun Reed Date: Thu, 4 Jun 2020 08:48:45 -0400 Subject: [PATCH] Check return value when peeking linked lists --- cpp/datastructs/circledoublelist/circledoublelist.cpp | 3 ++- cpp/datastructs/circlesinglelist/circlesinglelist.cpp | 3 ++- cpp/datastructs/doublelist/doublelist.cpp | 3 ++- cpp/datastructs/singlelist/singlelist.cpp | 3 ++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/cpp/datastructs/circledoublelist/circledoublelist.cpp b/cpp/datastructs/circledoublelist/circledoublelist.cpp index 767baad..a8c2c16 100644 --- a/cpp/datastructs/circledoublelist/circledoublelist.cpp +++ b/cpp/datastructs/circledoublelist/circledoublelist.cpp @@ -187,7 +187,8 @@ int CircleDoubleList::peek() const if (!isEmpty()) std::cout << "[" << tail->next->data << "] is at the top of our list\n"; else std::cout << "Nothing to peek, our list is empty...\n"; - return tail->data; + // If the list has data we return it, otherwise we return the smallest possible int (error) + return (!isEmpty()) ? tail->data : INT32_MIN; } /** print diff --git a/cpp/datastructs/circlesinglelist/circlesinglelist.cpp b/cpp/datastructs/circlesinglelist/circlesinglelist.cpp index 0f2390b..9027f3f 100644 --- a/cpp/datastructs/circlesinglelist/circlesinglelist.cpp +++ b/cpp/datastructs/circlesinglelist/circlesinglelist.cpp @@ -191,7 +191,8 @@ int CircleSingleList::peek() const if (!isEmpty()) std::cout << "[" << tail->next->data << "] is at the top of our list\n"; else std::cout << "Nothing to peek, our list is empty...\n"; - return tail->next->data; + // If the list has data we return it, otherwise we return the smallest possible int (error) + return (!isEmpty()) ? tail->next->data : INT32_MIN; } /** print diff --git a/cpp/datastructs/doublelist/doublelist.cpp b/cpp/datastructs/doublelist/doublelist.cpp index 06df49e..4fd8182 100644 --- a/cpp/datastructs/doublelist/doublelist.cpp +++ b/cpp/datastructs/doublelist/doublelist.cpp @@ -168,7 +168,8 @@ int DoubleList::peek() const if (!isEmpty()) std::cout << "[" << head->data << "] is at the top of our list\n"; else std::cout << "Nothing to peek, our list is empty...\n"; - return head->data; + // If the list has data we return it, otherwise we return the smallest possible int (error) + return (!isEmpty()) ? head->data : INT32_MIN; } /** print diff --git a/cpp/datastructs/singlelist/singlelist.cpp b/cpp/datastructs/singlelist/singlelist.cpp index 52d54bd..e469f53 100644 --- a/cpp/datastructs/singlelist/singlelist.cpp +++ b/cpp/datastructs/singlelist/singlelist.cpp @@ -168,7 +168,8 @@ int SingleList::peek() const if (!isEmpty()) std::cout << "[" << head->data << "] is at the top of our list\n"; else std::cout << "Nothing to peek, our list is empty...\n"; - return head->data; + // If the list has data we return it, otherwise we return the smallest possible int (error) + return (!isEmpty()) ? head->data : INT32_MIN; } /** print