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(void) {;
- 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++) {
- List ptr = curr;
- if (curr->value > 100) {
- list_remove(ptr);
- curr = ptr->next;
- list_push(ptr, list);
- }
- else
- if (curr->value < 100 && (abs(curr->value) % 2 == 1)) {
- list_remove(ptr);
- curr = ptr->next;
- free(ptr);
- }
- else
- curr = curr->next;
- }
- }
- 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->prev) && (list->value == list->next->value));
- }
- 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_insert(List t, List p) {
- Node* q = p->next; //следущий узел, после чего вставляем
- t->prev = p;
- t->next = q;
- p->next = t;
- q->prev = t;
- }
- 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;
- 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 push_list(List list, int data) {
- List new = calloc(1, sizeof(Node));
- new->value = data;
- new->next = NULL;
- new->prev = NULL;
- list_push(new, list);
- }
- 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