From 884a3cc6d873053b72855107f9f5d95d7ad1706d Mon Sep 17 00:00:00 2001 From: Syndamia Date: Thu, 24 Nov 2022 20:07:46 +0200 Subject: [SDP/E1] Added work from today's exam --- .../Structures and data algorithms/Exam 1/ex3.cpp | 55 ++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Year 2/Structures and data algorithms/Exam 1/ex3.cpp (limited to 'Year 2/Structures and data algorithms/Exam 1/ex3.cpp') diff --git a/Year 2/Structures and data algorithms/Exam 1/ex3.cpp b/Year 2/Structures and data algorithms/Exam 1/ex3.cpp new file mode 100644 index 0000000..d1f0a31 --- /dev/null +++ b/Year 2/Structures and data algorithms/Exam 1/ex3.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +#include + +bool readAndDelete(std::stack& st, std::string& word) { + std::queue backlog; + int index = 0; + while (!st.empty()) { + if (st.top() == word[index]) + index++; + else + backlog.push(st.top()); + st.pop(); + } + + while (!backlog.empty()) { + st.push(backlog.front()); + backlog.pop(); + } + + return index == word.length(); +} + +void printStack(std::stack& st) { + // Знам че принтирането е в обратен ред, бързам! + while (!st.empty()) { + std::cout << st.top() << std::endl; + st.pop(); + } + std::cout << std::endl; +} + +int main() { + std::string word1 = "mom"; + std::stack st1; + st1.push('m'); + st1.push('o'); + st1.push('o'); + st1.push('f'); + st1.push('m'); + std::cout << readAndDelete(st1, word1) << std::endl; + printStack(st1); + + + std::string word2 = "car"; + std::stack st2; + st2.push('y'); + st2.push('a'); + st2.push('d'); + st2.push('c'); + st2.push('c'); + std::cout << readAndDelete(st2, word2) << std::endl; + printStack(st2); +} -- cgit v1.3.1-26-gb76a