Website Summary Files Commits Branches @commit @diff @stats
path: root / Seminar02 / bonusTask01.cpp
diff options
context:
space:
mode:
Commitea11dc4a7563647a551e15095792e3f7127be9ea (patch)
Author
 Ekaterina <73318371+keti200125@users.noreply.github.com>
2023-10-25 21:05:18 +0300
Committer
 GitHub <noreply@github.com>
2023-10-25 21:05:18 +0300
Files5a6bcf97bb75e24e251b058ed04733a3816874d1 /Seminar02/bonusTask01.cpp
Data-structures-and-algorithms-ea11dc4a7563647a551e15095792e3f7127be9ea.tar
Data-structures-and-algorithms-ea11dc4a7563647a551e15095792e3f7127be9ea.tar.gz
Data-structures-and-algorithms-ea11dc4a7563647a551e15095792e3f7127be9ea.zip
Parentcd0ab73e854c0cd1932c6ec9faeaae0cb843a5e4 (diff)
bonus task seminar02
1 files changed, 51 insertions, 0 deletions
-rw-r--r--Seminar02/bonusTask01.cpp51
diff --git a/Seminar02/bonusTask01.cpp b/Seminar02/bonusTask01.cpp
new file mode 100644
index 0000000..6e41016
--- /dev/null
+++ b/Seminar02/bonusTask01.cpp
@@ -0,0 +1,51 @@
+#include <iostream>
+#include <stack>
+
+void sortStack(std::stack<int> &first)
+{
+ std::stack<int> second;
+ int currentElement = 0;
+ while (!first.empty())
+ {
+ currentElement = first.top();
+ first.pop();
+
+ while (!second.empty() && second.top() > currentElement)
+ {
+ first.push(second.top());
+ second.pop();
+ }
+ second.push(currentElement);
+ }
+
+ while (!second.empty())
+ {
+ first.push(second.top());
+ second.pop();
+ }
+}
+
+void stackPrint(std::stack<int> st)
+{
+ while (!st.empty())
+ {
+ std::cout << st.top() << " ";
+ st.pop();
+ }
+ std::cout << std::endl;
+}
+
+int main()
+{
+ std::stack<int> st;
+ st.push(8);
+ st.push(2);
+ st.push(4);
+ st.push(1);
+ st.push(5);
+ st.push(3);
+
+ stackPrint(st);
+ sortStack(st);
+ stackPrint(st);
+} \ No newline at end of file