diff options
| author | Andrew <saintruler@gmail.com> | 2021-02-10 22:37:49 +0400 |
|---|---|---|
| committer | Andrew <saintruler@gmail.com> | 2021-02-10 22:37:49 +0400 |
| commit | ba978063064e32a6e76fd6eb295876e470b30313 (patch) | |
| tree | 93be292aeca1ff5b2aedaa68b11bfbb0155fbb2e /structures/task8.cpp | |
| parent | 83f659d36b8c64209b72e70583cb4f0a6852e7be (diff) | |
Добавил решение 8 задачи
Diffstat (limited to 'structures/task8.cpp')
| -rw-r--r-- | structures/task8.cpp | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/structures/task8.cpp b/structures/task8.cpp new file mode 100644 index 0000000..099795d --- /dev/null +++ b/structures/task8.cpp @@ -0,0 +1,56 @@ +#include <iostream> +#include "stack.h" + +using namespace std; + +stack *result(stack *&h) +{ + stack *res = NULL; + + while (h) + { + stack *tmp = NULL; + + // Будем убирать из стека все повторения его верхнего элемента. + int el = pop(h); + // Так как все повторения будут убраны, можно положить + // значение в стек с результатом. + push(res, el); + while (h) + { + int cur = pop(h); + if (cur != el) + push(tmp, cur); + } + // После выполнения предыдущего цикла, в стеке не осталось + // значений, равных el. Обновим h и запустим на нём этот же + // самый алгоритм. + while (tmp) + push(h, pop(tmp)); + } + + reverse(res); + return res; +} + +int main() +{ + int n; + cout << "n = "; + cin >> n; + stack *head = NULL; + char x; + for (int i = 0; i < n; i++) + { + cin >> x; + push(head, int(x)); + } + reverse(head); + stack *res = result(head); + while (res) + cout << char(pop(res)) << " "; + cout << endl; + + return 0; + +} |