Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Structures and data algorithms / Exam 1 / ex3.cpp
diff options
context:
space:
mode:
1 files changed, 55 insertions, 0 deletions
-rw-r--r--Year 2/Structures and data algorithms/Exam 1/ex3.cpp55
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 <iostream>
+#include <queue>
+#include <stack>
+#include <string>
+
+bool readAndDelete(std::stack<char>& st, std::string& word) {
+ std::queue<char> 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<char>& st) {
+ // Знам че принтирането е в обратен ред, бързам!
+ while (!st.empty()) {
+ std::cout << st.top() << std::endl;
+ st.pop();
+ }
+ std::cout << std::endl;
+}
+
+int main() {
+ std::string word1 = "mom";
+ std::stack<char> 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<char> 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);
+}