diff options
| author | Andrew <saintruler@gmail.com> | 2021-03-02 22:45:12 +0400 |
|---|---|---|
| committer | Andrew <saintruler@gmail.com> | 2021-03-02 22:45:12 +0400 |
| commit | e413f3fc478da395fab295671b1ccd9f5bdb5b7e (patch) | |
| tree | 9e82529349fc7830388e7bf97ea9338c46975ad9 | |
| parent | b3f28bca3da03a81fb7a72f85f563360d4f13b0f (diff) | |
Добавил решение четвёртой задачи по связным спискам
| -rw-r--r-- | list/Makefile | 24 | ||||
| -rw-r--r-- | list/list.h | 100 | ||||
| -rw-r--r-- | list/task4.cpp | 42 | ||||
| -rw-r--r-- | list/zadachilist.pdf | bin | 0 -> 43739 bytes | |||
| -rw-r--r-- | theory/teorylist3.pdf (renamed from structures/teorylist3.pdf) | bin | 192869 -> 192869 bytes |
5 files changed, 166 insertions, 0 deletions
diff --git a/list/Makefile b/list/Makefile new file mode 100644 index 0000000..4a9376f --- /dev/null +++ b/list/Makefile @@ -0,0 +1,24 @@ +CXX=g++ +CFLAGS=-g -Wall +COMPILE=$(CXX) $(CFLAGS) + +task4: + $(COMPILE) -o task.out task4.cpp +test4: task4 + @printf "9\n5 2 9 1 3 7 9 2 9" | ./task.out + @printf "Answer: 5 9 1 3 7 9 9\n" + +task7: + $(COMPILE) -o task.out task7.cpp +test7: task7 + @printf "9\n6\n5 2 9 1 3 7 9 2 9" | ./task.out + @printf "Answer: 5 2 1 3 2 9 7 9 9\n" + +task15: + $(COMPILE) -o task.out task15.cpp +test15: task15 + @printf "9\n3\n1 2 3 4 5 6 7 8 9" | ./task.out + @printf "Answer: 1\n" + +clean: + rm task.out diff --git a/list/list.h b/list/list.h new file mode 100644 index 0000000..fe2c967 --- /dev/null +++ b/list/list.h @@ -0,0 +1,100 @@ +#include <iostream> + +struct list +{ + int inf; + list *next; + list *prev; +}; + + +void push(list *&h, list *&t, int x) +{ + list *r = new list; + r->inf = x; + r->next = NULL; + if (!h && !t) + { + r->prev = NULL; + h = r; + } + else + { + t->next = r; + r->prev = t; + } + t = r; +} + +void print(list *h, list *t) +{ + list *p = h; + while (p) + { + std::cout << p->inf << " "; + p = p->next; + } +} + +list *find(list *h, list *t, int x) +{ + list *p = h; + while (p) + { + if (p->inf == x) break; + p = p->next; + } + return p; +} + +void insert_after(list *&h, list *&t, list *r, int y) +{ + list *p = new list; + p->inf = y; + if (r == t) + { + p->next = NULL; + p->prev = r; + r->next = p; + t = p; + } + else { + r->next->prev = p; + p->next = r->next; + p->prev = r; + r->next = p; + } +} + +void del_node(list *&h, list *&t, list *r) +{ + if (r == h && r == t) + h = t = NULL; + else if (r == h) + { + h = h->next; + h->prev = NULL; + } + else if (r == t) + { + t = t->prev; + t->next = NULL; + } + else + { + r->next->prev = r->prev; + r->prev->next = r->next; + } + delete r; +} + +void del_list(list *&h, list *&t) +{ + while (h) + { + list *p = h; + h = h->next; + h->prev = NULL; + delete p; + } +} diff --git a/list/task4.cpp b/list/task4.cpp new file mode 100644 index 0000000..c217664 --- /dev/null +++ b/list/task4.cpp @@ -0,0 +1,42 @@ +#include <iostream> +#include "list.h" + +using namespace std; + +void solve(list *&h, list *&t) +{ + list *cur = h; + while (cur) + { + if (cur->inf % 2 == 0) + { + list *tmp = cur; + cur = cur->next; + del_node(h, t, tmp); + } + else cur = cur->next; + } +} + +int main() +{ + int n; + cout << "n = "; + cin >> n; + + list *head = NULL; + list *tail = NULL; + int x; + for (int i = 0; i < n; i++) + { + cin >> x; + push(head, tail, x); + } + + solve(head, tail); + print(head, tail); + cout << endl; + + return 0; + +} diff --git a/list/zadachilist.pdf b/list/zadachilist.pdf Binary files differnew file mode 100644 index 0000000..5f6f037 --- /dev/null +++ b/list/zadachilist.pdf diff --git a/structures/teorylist3.pdf b/theory/teorylist3.pdf Binary files differindex 7cc2f16..7cc2f16 100644 --- a/structures/teorylist3.pdf +++ b/theory/teorylist3.pdf |