Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Structures and data algorithms / Exam 1 / ex3.cpp
diff options
context:
space:
mode:
Commit884a3cc6d873053b72855107f9f5d95d7ad1706d (patch)
Author
 Syndamia <kamen@syndamia.com>
2022-11-24 20:07:46 +0200
Committer
 Syndamia <kamen@syndamia.com>
2022-11-24 20:07:46 +0200
Files4dddd0d1fa72fcf514f649cbe8054e8ed3709bb4 /Year 2/Structures and data algorithms/Exam 1/ex3.cpp
uni-projects-884a3cc6d873053b72855107f9f5d95d7ad1706d.tar
uni-projects-884a3cc6d873053b72855107f9f5d95d7ad1706d.tar.gz
uni-projects-884a3cc6d873053b72855107f9f5d95d7ad1706d.zip
Parent81f851c6ea7988314812cf7cdb3cc94a6aa5cbf3 (diff)
[SDP/E1] Added work from today's exam
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);
+}