nikitast

lists_1

May 18th, 2021
672
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. };
  8.  
  9. Node* toCur(Node* first, Node* cur) {
  10.     while (first->next != cur) {
  11.         first = first->next;
  12.     }
  13.     return first;
  14. }
  15.  
  16. Node* addNode(Node* first, double data) {
  17.     if (first == NULL) {
  18.         first = new Node;
  19.         first->data = data;
  20.         first->next = NULL;
  21.     }
  22.     else {
  23.         Node* tmp = new Node;
  24.         tmp->data = data;
  25.         tmp->next = NULL;
  26.         toCur(first, NULL)->next = tmp;
  27.     }
  28.     return first;
  29. }
  30.  
  31. Node* delNode(Node* first, Node* cur) {
  32.     if (cur == first) {
  33.         first = cur->next;
  34.         delete cur;
  35.     }
  36.     else if (cur == toCur(first, NULL)) {
  37.         toCur(first, cur)->next = NULL;
  38.         delete cur;
  39.     }
  40.     else {
  41.         toCur(first, cur)->next = cur->next;
  42.         delete cur;
  43.     }
  44.     return first;
  45. }
  46.  
  47. Node* sortInsert(Node* first, double data) {
  48.     if (first == NULL) {
  49.         first = addNode(first, data);
  50.     }
  51.     else {
  52.         Node* cur = first;
  53.         while ((cur->data > data) && (cur->next != NULL)) {
  54.             cur = cur->next;
  55.         }
  56.         if (cur->data > data) {
  57.             first = addNode(first, data);
  58.         }
  59.         else if (cur == first) {
  60.             Node *tmp = new Node;
  61.             tmp->data = data;
  62.             tmp->next = cur;
  63.             first = tmp;
  64.         }
  65.         else {
  66.             Node *tmp = new Node;
  67.             tmp->data = data;
  68.             tmp->next = cur;
  69.             toCur(first, cur)->next = tmp;
  70.         }
  71.     }
  72.     return first;
  73. }
  74.  
  75. Node* listFromFile(FILE* f, Node* first) {
  76.     char str[127];
  77.     fgets(str, 127, f);
  78.     puts(str);
  79.     char* ptr = strtok(str, " ");
  80.     while (ptr != NULL) {
  81.         first = sortInsert(first, atof(ptr));
  82.         ptr = strtok(NULL, " ");
  83.     }
  84.     return first;
  85. }
  86.  
  87. Node* delLess(Node* first, int num) {
  88.     Node* cur = first;
  89.     while (cur->data < num) {
  90.         cur = delNode(first, cur);
  91.         first = cur;
  92.     }
  93.     while (cur != NULL) {
  94.         if (cur->data < num) {
  95.             cur = delNode(first, cur);
  96.             first = cur;
  97.         }
  98.         cur = cur->next;
  99.     }
  100.     return first;
  101. }
  102.  
  103. void printList(Node* first) {
  104.     while (first != NULL) {
  105.         printf("%lf ", first->data);
  106.         first = first->next;
  107.     }
  108. }
  109.  
  110. void delList(Node*& first) {
  111.     while (first != NULL) {
  112.         Node* tmp = first;
  113.         first = first->next;
  114.         delete tmp;
  115.     }
  116. }
  117.  
  118. int main() {
  119.     FILE* f;
  120.     f = fopen(R"(C:\Clion Projects\lists\test.txt)", "r");
  121.     Node* first = NULL;
  122.     first = listFromFile(f, first);
  123.     fclose(f);
  124.     first = delLess(first, 2);
  125.     printList(first);
  126.     delList(first);
  127.     return 0;
  128. }
  129.  
RAW Paste Data