diff options
| -rw-r--r-- | structures/Makefile | 5 | ||||
| -rw-r--r-- | structures/task8.cpp | 56 |
2 files changed, 61 insertions, 0 deletions
diff --git a/structures/Makefile b/structures/Makefile index 8694df6..d2d7739 100644 --- a/structures/Makefile +++ b/structures/Makefile @@ -5,5 +5,10 @@ COMPILE=$(CXX) $(CFLAGS) task5: $(COMPILE) -o task.out task5.cpp +task8: + $(COMPILE) -o task.out task8.cpp +test8: task8 + printf "10\n1 1 2 3 2 4 3 5 4 1" | ./task.out + clean: rm task.out 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; + +} |