nikitast

lists_2

May 18th, 2021
702
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. struct Node {
  5.     double data;
  6.     Node* next;
  7.     Node* prev;
  8. };
  9.  
  10. Node* addNode(Node* first, Node*& last,double data) {
  11.     if (first == NULL) {
  12.         first = new Node;
  13.         first->data = data;
  14.         first->next = NULL;
  15.         first->prev = NULL;
  16.         last = first;
  17.     }
  18.     else {
  19.         Node* tmp = new Node;
  20.         last->next = tmp;
  21.         tmp->data = data;
  22.         tmp->next = NULL;
  23.         tmp->prev = last;
  24.         last = tmp;
  25.     }
  26.     return first;
  27. }
  28.  
  29. Node* delNode(Node* first, Node*& last, Node* cur) {
  30.     if (cur == first) {
  31.         first = cur->next;
  32.         first->prev = NULL;
  33.         delete cur;
  34.     }
  35.     else if (cur == last) {
  36.         last = cur->prev;
  37.         last->next = NULL;
  38.         delete cur;
  39.     }
  40.     else {
  41.         Node* tmpPrev = cur->prev;
  42.         Node* tmpNext = cur->next;
  43.         tmpPrev->next = tmpNext;
  44.         tmpNext->prev = tmpPrev;
  45.         delete cur;
  46.     }
  47.     return first;
  48. }
  49.  
  50. Node* sortInsert(Node* first, Node*& last, double data) {
  51.     if (first == NULL) {
  52.         first = addNode(first, last, data);
  53.     }
  54.     else {
  55.         Node* cur = first;
  56.         while ((cur->data > data) && (cur->next != NULL)) {
  57.             cur = cur->next;
  58.         }
  59.         if (cur->data > data) {
  60.             first = addNode(first, last, data);
  61.         }
  62.         else if (cur == first) {
  63.             Node *tmp = new Node;
  64.             tmp->data = data;
  65.             tmp->prev = NULL;
  66.             tmp->next = cur;
  67.             cur->prev = tmp;
  68.             first = tmp;
  69.         }
  70.         else {
  71.             Node *tmp = new Node;
  72.             tmp->data = data;
  73.             tmp->prev = cur->prev;
  74.             (cur->prev)->next = tmp;
  75.             tmp->next = cur;
  76.             cur->prev = tmp;
  77.         }
  78.     }
  79.     return first;
  80. }
  81.  
  82. Node* listFromFile(FILE* f, Node* first, Node*& last) {
  83.     char str[127];
  84.     fgets(str, 127, f);
  85.     puts(str);
  86.     char* ptr = strtok(str, " ");
  87.     while (ptr != NULL) {
  88.         first = sortInsert(first, last, atof(ptr));
  89.         ptr = strtok(NULL, " ");
  90.     }
  91.     return first;
  92. }
  93.  
  94. Node* delLess(Node* first, Node*& last, int num) {
  95.     Node* cur = first;
  96.     while (cur->data < num) {
  97.         cur = delNode(first, last, cur);
  98.         first = cur;
  99.     }
  100.     while (cur != NULL) {
  101.         if (cur->data < num) {
  102.             cur = delNode(first, last, cur);
  103.             first = cur;
  104.         }
  105.         cur = cur->next;
  106.     }
  107.     return first;
  108. }
  109.  
  110. void printList(Node* first, Node* last) {
  111.     while (first != NULL) {
  112.         printf("%lf ", first->data);
  113.         first = first->next;
  114.     }
  115.     puts("");
  116.     while (last != NULL) {
  117.         printf("%lf ", last->data);
  118.         last = last->prev;
  119.     }
  120. }
  121.  
  122. void delList(Node*& first, Node*& last) {
  123.     while (first != NULL) {
  124.         Node* tmp = first;
  125.         first = first->next;
  126.         delete tmp;
  127.     }
  128.     first = NULL;
  129.     last = NULL;
  130. }
  131.  
  132.  
  133.  
  134. int main() {
  135.     FILE* f;
  136.     f = fopen(R"(C:\Clion Projects\lists\test.txt)", "r");
  137.     Node* first = NULL;
  138.     Node* last = NULL;
  139.     first = listFromFile(f, first, last);
  140.     fclose(f);
  141.     first = delLess(first, last, 2);
  142.     printList(first, last);
  143.     delList(first, last);
  144.     return 0;
  145. }
  146.  
RAW Paste Data