From ba978063064e32a6e76fd6eb295876e470b30313 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 10 Feb 2021 22:37:49 +0400 Subject: =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20=D1=80?= =?UTF-8?q?=D0=B5=D1=88=D0=B5=D0=BD=D0=B8=D0=B5=208=20=D0=B7=D0=B0=D0=B4?= =?UTF-8?q?=D0=B0=D1=87=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- structures/task8.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 structures/task8.cpp (limited to 'structures/task8.cpp') 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 +#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; + +} -- cgit v1.2.3