From 28e049db32fb48a7605f908d2a8227097fde29e7 Mon Sep 17 00:00:00 2001 From: GKuzzinshtern <72338984+GKuzzinshtern@users.noreply.github.com> Date: Sun, 15 Nov 2020 14:47:18 +0200 Subject: [PATCH 1/4] YuriyKuznetsov_HomeWork5 --- list_impl.cpp | 183 +++++++++++++++++++++++++++++++++++++++++++++++ list_interface.h | 45 ++++++++++++ main.cpp | 33 +++++++++ 3 files changed, 261 insertions(+) create mode 100644 list_impl.cpp create mode 100644 list_interface.h create mode 100644 main.cpp diff --git a/list_impl.cpp b/list_impl.cpp new file mode 100644 index 0000000..dcca153 --- /dev/null +++ b/list_impl.cpp @@ -0,0 +1,183 @@ +#include "list_interface.h" + +structNode *GetLastEl(const structList &in_oList) { + constexpr size_t csizeOneElem{1}; + if (bIsEmpty(in_oList)) { + return nullptr; + } + if (Size(in_oList) == csizeOneElem) { + return in_oList.oFirstNode; + } + structNode *oNode{in_oList.oFirstNode}; + while (oNode->oNext != nullptr) { + oNode = oNode->oNext; + } + return oNode; +} + +void vPushFront(structList &in_oList, int in_iData) { + structNode *oNode = + new (structNode){}; // make "try" for catching allocation troubles + oNode->iData = in_iData; + if (bIsEmpty(in_oList)) { + in_oList.oFirstNode = oNode; + ++in_oList.sizeSize; + return; + } + oNode->oNext = in_oList.oFirstNode; + in_oList.oFirstNode->oPrev = oNode; + in_oList.oFirstNode = oNode; + ++in_oList.sizeSize; +} + +void vPushBack(structList &in_oList, int in_iData) { + structNode *oNode = + new (structNode){}; // make "try" for catching allocation troubles + oNode->iData = in_iData; + if (bIsEmpty(in_oList)) { + in_oList.oFirstNode = oNode; + ++in_oList.sizeSize; + return; + } + structNode *oLastNode{GetLastEl(in_oList)}; + oLastNode->oNext = oNode; + oNode->oPrev = oLastNode; + ++in_oList.sizeSize; +} + +bool bInsert(structList &in_oList, structNode *oCurrentNode, int in_iData) { + if (!bCheckNode(in_oList, oCurrentNode)) { + return false; + } + if (oCurrentNode == in_oList.oFirstNode) { + vPushFront(in_oList, in_iData); + return true; + } + structNode *oNode = new (structNode){}; // make "try"... + oNode->iData = in_iData; + oNode->oPrev = oCurrentNode->oPrev; + oNode->oNext = oCurrentNode; + (oCurrentNode->oPrev)->oNext = oNode; + oCurrentNode->oPrev = oNode; + ++in_oList.sizeSize; + return true; +} + +// Deleting element +bool bPopFront(structList &in_oList) { + constexpr size_t csizeOneElem{1}; + + if (bIsEmpty(in_oList)) { + return false; + } + if (Size(in_oList) == csizeOneElem) { + delete in_oList.oFirstNode; + in_oList.oFirstNode = nullptr; + --in_oList.sizeSize; + return true; + } + structNode *oFirstNode{in_oList.oFirstNode}; + structNode *oNewFirstNode{in_oList.oFirstNode->oNext}; + oNewFirstNode->oPrev = nullptr; + in_oList.oFirstNode = oNewFirstNode; + --in_oList.sizeSize; + + delete oFirstNode; + return true; +} + +bool bPopBack(structList &in_oList) { + constexpr size_t csizeOneElem{1}; + + if (bIsEmpty(in_oList)) { + return false; + } + if (Size(in_oList) == csizeOneElem) { + delete in_oList.oFirstNode; + in_oList.oFirstNode = nullptr; + --in_oList.sizeSize; + return true; + } + structNode *oLastNode{GetLastEl(in_oList)}; + structNode *oNewLastNode{oLastNode->oPrev}; + oNewLastNode->oNext = nullptr; + --in_oList.sizeSize; + + delete oLastNode; + return true; +} + +bool bErase(structList &in_oList, structNode *oCurrentNode) { + if (!bCheckNode(in_oList, oCurrentNode)) { + return false; + } + if (oCurrentNode == in_oList.oFirstNode) { + return bPopFront(in_oList); + } + if (oCurrentNode == (GetLastEl(in_oList))) { + return bPopBack(in_oList); + } + + structNode *oPrevNode{oCurrentNode->oPrev}; + structNode *oNextNode{oCurrentNode->oNext}; + oPrevNode->oNext = oNextNode; + oNextNode->oPrev = oPrevNode; + --in_oList.sizeSize; + + delete oCurrentNode; + return true; +} + +void vClear(structList &in_oList) { + if (bIsEmpty(in_oList)) { + return; + } + structNode *oNode{GetLastEl(in_oList)}; + while (oNode != in_oList.oFirstNode) { + structNode *oTempNode{oNode}; + oNode = oTempNode->oPrev; + delete oTempNode; + } + delete in_oList.oFirstNode; + in_oList.oFirstNode = nullptr; + in_oList.sizeSize = 0; +} + +structNode *FindByValue(structList &in_oList, const int in_iValue) { + structNode *oNode{in_oList.oFirstNode}; + while (oNode->oNext != nullptr) { + if (bEqual(oNode, in_iValue)) { + return oNode; + } + oNode = oNode->oNext; + } + return nullptr; +} + +bool bCheckNode(structList &in_oList, structNode *oNodeToCheck) { + if (bIsEmpty(in_oList)) { + return false; + } + structNode *oNode{in_oList.oFirstNode}; + while (oNode->oNext != nullptr) { + if (bEqual(oNodeToCheck, oNode)) { + return true; + } + oNode = oNode->oNext; + } + return false; +} + +void vPrintList(const structList &in_oList) { + if (bIsEmpty(in_oList)) { + std::cout << "List is empty\n"; + return; + } + structNode *oNode{in_oList.oFirstNode}; + std::cout << "List: "; + while (oNode->oNext != nullptr) { + std::cout << oNode->iData << ' '; + oNode = oNode->oNext; + } + std::cout << oNode->iData << " \n"; +} diff --git a/list_interface.h b/list_interface.h new file mode 100644 index 0000000..ce77ea9 --- /dev/null +++ b/list_interface.h @@ -0,0 +1,45 @@ +#pragma once + +#include + +struct structNode{ + structNode* oPrev{}; + int iData; + structNode* oNext{}; +}; + +struct structList{ + structNode* oFirstNode{}; + size_t sizeSize{}; +}; + +//Element access +inline structNode* GetFirstEl(const structList& in_oList) {return in_oList.oFirstNode;}; +structNode* GetLastEl(const structList& in_oList); + +// Inserting element +void vPushFront(structList& in_oList, int in_iData); +void vPushBack(structList& in_oList, int in_iData); +bool bInsert(structList& in_oList, structNode* oCurrentNode, int in_iData); + +// Deleting element +bool bPopFront(structList& in_oList); +bool bPopBack(structList& in_oList); +bool bErase(structList& in_oList, structNode* oCurrentNode); +void vClear(structList& in_oList); + +// Compare +inline bool bCmp(structNode* oLess, structNode* oMore) {return (oLess->iData < oMore->iData);} +inline bool bEqual(structNode* oFirst, structNode* oSecond) {return (oFirst->iData == oSecond->iData);} +inline bool bEqual(structNode* oFirst, int iSecond) {return (oFirst->iData == iSecond);} + +// Find element +structNode* FindByValue(structList& in_oList, const int in_iValue); + +// Capacity +inline size_t Size(const structList& in_oList) {return in_oList.sizeSize;}; +inline bool bIsEmpty(const structList& in_oList) {return (in_oList.sizeSize == 0);}; + +// Helpers +bool bCheckNode(structList& in_oList, structNode* oNodeToCheck); +void vPrintList(const structList& in_oList); diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..216d157 --- /dev/null +++ b/main.cpp @@ -0,0 +1,33 @@ +#include "list_interface.h" + +int main() { + structList myList{}; + int myData{5}; + vPrintList(myList); + + vPushBack(myList, myData); + vPushFront(myList, 8); + vPushBack(myList, 32); + vPrintList(myList); + + bInsert(myList, FindByValue(myList, 5), 123); + vPrintList(myList); + + std::cout.setf(std::ios::boolalpha); + std::cout << "First element < Last element?: " + << bCmp(GetFirstEl(myList), GetLastEl(myList)) << '\n'; + + bPopBack(myList); + bPopFront(myList); + vPrintList(myList); + + vPushBack(myList, 45); + vPushBack(myList, 280); + bErase(myList, FindByValue(myList, 5)); + vPrintList(myList); + + vClear(myList); + vPrintList(myList); + + return 0; +} From 281129bddc2e9a357d96f6aeb8ea886f7d38d101 Mon Sep 17 00:00:00 2001 From: GKuzzinshtern <72338984+GKuzzinshtern@users.noreply.github.com> Date: Sun, 15 Nov 2020 14:51:01 +0200 Subject: [PATCH 2/4] Changed formatting --- list_interface.h | 56 ++++++++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/list_interface.h b/list_interface.h index ce77ea9..ebd02d1 100644 --- a/list_interface.h +++ b/list_interface.h @@ -2,44 +2,54 @@ #include -struct structNode{ - structNode* oPrev{}; +struct structNode { + structNode *oPrev{}; int iData; - structNode* oNext{}; + structNode *oNext{}; }; -struct structList{ - structNode* oFirstNode{}; +struct structList { + structNode *oFirstNode{}; size_t sizeSize{}; }; -//Element access -inline structNode* GetFirstEl(const structList& in_oList) {return in_oList.oFirstNode;}; -structNode* GetLastEl(const structList& in_oList); +// Element access +inline structNode *GetFirstEl(const structList &in_oList) { + return in_oList.oFirstNode; +}; +structNode *GetLastEl(const structList &in_oList); // Inserting element -void vPushFront(structList& in_oList, int in_iData); -void vPushBack(structList& in_oList, int in_iData); -bool bInsert(structList& in_oList, structNode* oCurrentNode, int in_iData); +void vPushFront(structList &in_oList, int in_iData); +void vPushBack(structList &in_oList, int in_iData); +bool bInsert(structList &in_oList, structNode *oCurrentNode, int in_iData); // Deleting element -bool bPopFront(structList& in_oList); -bool bPopBack(structList& in_oList); -bool bErase(structList& in_oList, structNode* oCurrentNode); -void vClear(structList& in_oList); +bool bPopFront(structList &in_oList); +bool bPopBack(structList &in_oList); +bool bErase(structList &in_oList, structNode *oCurrentNode); +void vClear(structList &in_oList); // Compare -inline bool bCmp(structNode* oLess, structNode* oMore) {return (oLess->iData < oMore->iData);} -inline bool bEqual(structNode* oFirst, structNode* oSecond) {return (oFirst->iData == oSecond->iData);} -inline bool bEqual(structNode* oFirst, int iSecond) {return (oFirst->iData == iSecond);} +inline bool bCmp(structNode *oLess, structNode *oMore) { + return (oLess->iData < oMore->iData); +} +inline bool bEqual(structNode *oFirst, structNode *oSecond) { + return (oFirst->iData == oSecond->iData); +} +inline bool bEqual(structNode *oFirst, int iSecond) { + return (oFirst->iData == iSecond); +} // Find element -structNode* FindByValue(structList& in_oList, const int in_iValue); +structNode *FindByValue(structList &in_oList, const int in_iValue); // Capacity -inline size_t Size(const structList& in_oList) {return in_oList.sizeSize;}; -inline bool bIsEmpty(const structList& in_oList) {return (in_oList.sizeSize == 0);}; +inline size_t Size(const structList &in_oList) { return in_oList.sizeSize; }; +inline bool bIsEmpty(const structList &in_oList) { + return (in_oList.sizeSize == 0); +}; // Helpers -bool bCheckNode(structList& in_oList, structNode* oNodeToCheck); -void vPrintList(const structList& in_oList); +bool bCheckNode(structList &in_oList, structNode *oNodeToCheck); +void vPrintList(const structList &in_oList); From 7e12939a382eb3bab9fdea8901784eea3677cb29 Mon Sep 17 00:00:00 2001 From: GKuzzinshtern <72338984+GKuzzinshtern@users.noreply.github.com> Date: Sun, 22 Nov 2020 13:45:40 +0200 Subject: [PATCH 3/4] YuriyKuznetsov_HomeWork5_ReverseOrRotate --- YuriyKuznetsov_HomeWork5_ReverseOrRotate.cpp | 55 ++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 YuriyKuznetsov_HomeWork5_ReverseOrRotate.cpp diff --git a/YuriyKuznetsov_HomeWork5_ReverseOrRotate.cpp b/YuriyKuznetsov_HomeWork5_ReverseOrRotate.cpp new file mode 100644 index 0000000..2e9c37c --- /dev/null +++ b/YuriyKuznetsov_HomeWork5_ReverseOrRotate.cpp @@ -0,0 +1,55 @@ +// Reverse or rotate +#include +#include +#include + +void revrot(std::string &inout_sStr, const unsigned int in_cuiPieceSize); + +int main() { + std::string myString{"563000655734469485"}; + std::cout << myString << '\n'; + revrot(myString, 4); + std::cout << myString << '\n'; + + return 0; +} + +void revrot(std::string &inout_sStr, const unsigned int in_cuiPieceSize) { + if (in_cuiPieceSize == 0) { + inout_sStr.clear(); + return; + } + if (in_cuiPieceSize == 1) { + return; + } + if (inout_sStr.length() < in_cuiPieceSize) { + inout_sStr.clear(); + return; + } + size_t szNewLength{(inout_sStr.length() / in_cuiPieceSize) * in_cuiPieceSize}; + + inout_sStr.resize(szNewLength); + + for (size_t szCharIndex = 0; szCharIndex <= (szNewLength - in_cuiPieceSize); + szCharIndex += in_cuiPieceSize) { + std::string sPieceOfString{inout_sStr, szCharIndex, in_cuiPieceSize}; + unsigned int uiCriterion{}; + for (auto itPieceOfString = sPieceOfString.begin(); + itPieceOfString != sPieceOfString.end(); ++itPieceOfString) { + if (!std::isdigit(*itPieceOfString)) { + inout_sStr.clear(); + return; + } + std::string sOneChar{*itPieceOfString}; + uiCriterion += + std::stoi(sOneChar) * std::stoi(sOneChar) * std::stoi(sOneChar); + } + if ((uiCriterion % 2) == 0) { + std::reverse(sPieceOfString.begin(), sPieceOfString.end()); + } else { + std::rotate(sPieceOfString.begin(), sPieceOfString.begin() + 1, + sPieceOfString.end()); + } + inout_sStr.replace(szCharIndex, in_cuiPieceSize, sPieceOfString); + } +} From 753b1afac14add7bfbca6598d61b5aee8186e0af Mon Sep 17 00:00:00 2001 From: GKuzzinshtern <72338984+GKuzzinshtern@users.noreply.github.com> Date: Sun, 29 Nov 2020 11:53:16 +0200 Subject: [PATCH 4/4] Update list_impl.cpp --- list_impl.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/list_impl.cpp b/list_impl.cpp index dcca153..2f7fdc7 100644 --- a/list_impl.cpp +++ b/list_impl.cpp @@ -151,6 +151,9 @@ structNode *FindByValue(structList &in_oList, const int in_iValue) { } oNode = oNode->oNext; } + if (bEqual(oNode, in_iValue)) { + return oNode; + } return nullptr; } @@ -165,6 +168,9 @@ bool bCheckNode(structList &in_oList, structNode *oNodeToCheck) { } oNode = oNode->oNext; } + if (bEqual(oNodeToCheck, oNode)) { + return true; + } return false; }