Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct Node* List;
- typedef struct Node {
- struct Node *next;
- struct Node *prev;
- int value;
- } Node;
- void list_print(List);
- void list_print_back(List);
- void list_print_dbg(List);
- void list_insert(List, List);
- void list_push(List, List);
- void list_init(List); //инициализация списка
- void list_remove(List);
- void push_list(List, int);
- void list_delete(List *);
- int is_empty(List);
- void pop(List);
- void clear(List);
- void foo(List, int);
- void delete(List);
- int main(int argc, char** argv) {
- List list = calloc(1, sizeof(Node));
- list_init(list);
- int n = 0, cntr = 0;
- while (scanf("%d", &n) == 1) {
- push_list(list, n);
- cntr++;
- }
- foo(list, cntr);
- list_print_back(list);
- clear(list);
- free(list);
- return 0;
- }
- void foo(List list, int cntr) {
- List curr = list->next;
- for (int i = 0; i < cntr; i++) {
- if (curr == list) break; // Проверка на конец списка
- List ptr = curr;
- curr = curr->next;
- if (ptr->value > 100) {
- list_remove(ptr);
- list_push(ptr, list);
- } else if (ptr->value < 100 && (abs(ptr->value) % 2 == 1)) {
- list_remove(ptr);
- free(ptr); // Освобождаем память
- }
- }
- }
- void list_print(List list) {
- for (List ptr = list->next; ptr != list; ptr = ptr->next)
- printf("%d ", ptr->value);
- putchar('\n');
- }
- int is_empty(List list) {
- return (list->next == list && list->prev == list);
- }
- void list_print_back(List list) {
- for (Node* ptr = list->prev; ptr != list; ptr = ptr->prev)
- printf("%d ", ptr->value);
- putchar('\n');
- }
- void list_print_dbg(List list) {
- Node* p = list;
- printf("--------\n");
- printf("list: prev=%p %p next=%p\n", p->prev, p, p->next);
- for (Node* ptr = list->next; ptr != list; ptr = ptr->next)
- printf("%d prev=%p %p next=%p\n", ptr->value, ptr->prev, ptr, ptr->next);
- putchar('\n');
- }
- void list_push(List add, List list) {
- list_insert(add, list->prev);
- }
- void list_init(List list) {
- list->value = 0;
- list->next = list;
- list->prev = list;
- }
- void pop(List list) {
- List ptr = list->prev;
- if (ptr != list) { // Проверка на пустоту
- list_remove(ptr);
- free(ptr);
- }
- }
- void list_remove(List del) {
- Node* p = del->next;
- Node* q = del->prev;
- p->prev = q;
- q->next = p;
- }
- void list_insert(List t, List p) {
- Node* q = p->next; // следующий узел, после чего вставляем
- t->prev = p;
- t->next = q;
- p->next = t;
- q->prev = t;
- }
- void push_list(List list, int data) {
- List new = calloc(1, sizeof(Node));
- new->value = data;
- list_insert(new, list->prev);
- }
- void delete(List t) {
- list_remove(t);
- free(t);
- }
- void clear(List list) {
- while (!is_empty(list)) {
- pop(list);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement