Advertisement
Sitisom

Писки2

May 13th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.51 KB | None | 0 0
  1. #include <iostream>
  2. #include<fstream>
  3. #include<string>
  4. using namespace std;
  5.  
  6. struct Node
  7. {
  8.     int data;
  9.     Node* next;
  10. };
  11.  
  12. bool is_Empty(Node*& top) {
  13.     return top == NULL;
  14. }
  15.  
  16. Node* previous(Node* top, Node* p) {
  17.     Node* pr = new Node, * q = top;
  18.     if (q == NULL)
  19.         exit(-1);
  20.     else {
  21.         while (q && q != p) {
  22.             pr = q;
  23.             q = q->next;
  24.         }
  25.     }
  26.     if (pr->next == p) return pr;
  27.     return NULL;
  28. }
  29.  
  30. Node* previous(Node * top, int data) {
  31.     Node* pr = NULL, * q = top;
  32.     if (!q)
  33.         exit(-1);
  34.     while (q && q->data != data) {
  35.         pr = q;
  36.         q = q->next;
  37.     }
  38.     if (pr->next == q) return pr;
  39.     return NULL;
  40. }
  41.  
  42. void show_list(Node* top) {
  43.     Node* p = top;
  44.     while (p) {
  45.         cout << p->data << ' ';
  46.         p = p->next;
  47.     }
  48. }
  49.  
  50. void show_lists(Node**& list, int n) {
  51.     for (int i = 0; i < n; i++) {
  52.         show_list(list[i]);
  53.         cout << endl;
  54.     }
  55. }
  56.  
  57. void push(Node * &top, int data) {
  58.     Node* p = new Node;
  59.     p->data = data;
  60.     p->next = top;
  61.     top = p;
  62. }
  63.  
  64. int pop(Node * &top) {
  65.     Node* p = top->next;
  66.     int data = top->data;
  67.     delete top;
  68.     top = p;
  69.     return data;
  70. }
  71.  
  72. int pop(Node * &top, Node * q)
  73. {
  74.     int data;
  75.     if (q == top)
  76.         data = pop(top);
  77.     else
  78.     {
  79.         Node* pr = previous(top, q);
  80.         pr->next = q->next;
  81.         data = q->data;
  82.         delete q;
  83.     }
  84.     return data;
  85. }
  86.  
  87. void pop(Node * &top, int data)
  88. {
  89.     Node* p = top;
  90.  
  91.     while (p && p->data != data)
  92.     {
  93.         p = p->next;
  94.     }
  95.     pop(top, p);
  96. }
  97.  
  98. Node* create_list(string s) {
  99.     Node* top = NULL;
  100.     for (int i = 0; i < s.length(); i++) {
  101.         if (s[i] == ' ') {
  102.             break;
  103.         }
  104.         else {
  105.             push(top, s[i]-48);
  106.         }
  107.     }
  108.     return top;
  109. }
  110.  
  111. Node* create_list(ifstream & f) {
  112.     int x;
  113.     string s;
  114.     Node* top = NULL;
  115.     getline(f, s);
  116.     top = create_list(s);
  117.     return top;
  118. }
  119.  
  120. Node** create_lists(ifstream& f, int n) {
  121.     int i = 0;
  122.     Node** list = new Node*[n];
  123.     string s;
  124.     while (getline(f, s)) {
  125.         list[i] = create_list(s);
  126.         i++;
  127.     }
  128.     return list;
  129. }
  130.  
  131. void sort(Node * top)
  132. {
  133.     int data;
  134.     Node* p, * q;
  135.     p = top;
  136.     while (p->next)
  137.     {
  138.         q = p->next;
  139.         while (q)
  140.         {
  141.             if (p->data > q->data)
  142.             {
  143.                 data = p->data;
  144.                 p->data = q->data;
  145.                 q->data = data;
  146.  
  147.             }
  148.             q = q->next;
  149.         }
  150.         p = p->next;
  151.     }
  152. }
  153.  
  154. bool elem_in_list(Node* top, int data)
  155. {
  156.     Node* q;
  157.     q = top;
  158.     int k = 0;
  159.     while (q)
  160.     {
  161.         if (q->data == data)
  162.             k++;
  163.         q = q->next;
  164.     }
  165.     if (k > 1)
  166.         return false;
  167.     else
  168.         return true;
  169.  
  170. }
  171. void into_array(Node * &top)
  172. {
  173.     Node* p = top->next;
  174.     while (p)
  175.     {
  176.         if (!elem_in_list(top, p->data))
  177.         {
  178.             Node* r = previous(top, p->data);
  179.             pop(top, p->data);
  180.             p = r;
  181.         }
  182.         p = p->next;
  183.     }
  184. }
  185. void preobr(Node * *&a, int n)
  186. {
  187.     for (int i = 0; i < n; i++)
  188.         into_array(a[i]);
  189. }
  190. bool prime(int x)
  191. {
  192.     bool p = true;
  193.     if (x <= 1)
  194.         return false;
  195.     if (x == 2)
  196.         return true;
  197.     for (int i = 2; i * i <= x && p; i++)
  198.     {
  199.         if (x % i == 0)
  200.             p = false;
  201.     }
  202.     return p;
  203. }
  204. bool arr_twins(Node* top)
  205. {
  206.     Node* p = top;
  207.     while (p)
  208.     {
  209.         if (prime(p->data))
  210.             if ((prime(p->data + 2) && elem_in_list(top, p->data + 2))
  211.                 || (prime(p->data + 2) && elem_in_list(top, p->data - 2)))
  212.                 return true;
  213.         p = p->next;
  214.     }
  215.     return false;
  216. }
  217. int max_in_list(Node * top)
  218. {
  219.     int max = top->data;
  220.     Node* p = top->next;
  221.     while (p)
  222.     {
  223.         if (max < p->data)
  224.             max = p->data;
  225.         p = p->next;
  226.     }
  227.     return max;
  228. }
  229. int max_in_lists(Node**a, int n)
  230. {
  231.     int i, max;
  232.     max = max_in_list(a[0]);
  233.     for (i = 1; i < n; i++)
  234.         if (max_in_list(a[i]) > max)
  235.             max = max_in_list(a[i]);
  236.     return max;
  237. }
  238. Node* list_power_2(int max)
  239. {
  240.     int a = 1;
  241.     Node* top = new Node;
  242.     while (a <= max)
  243.     {
  244.         push(top, a);
  245.         a *= 2;
  246.     }
  247.     return top;
  248. }
  249. bool almost_prime_list(int x)
  250. {
  251.     int k = 0;
  252.     for (int i = 2; i < x; i++)
  253.     {
  254.         if (x % i == 0)
  255.         {
  256.             if (prime(i))
  257.                 k++;
  258.             else
  259.                 return false;
  260.         }
  261.         if (k > 2)
  262.             return false;
  263.     }
  264.     return true;
  265. }
  266. bool truly_prime_list(Node* top)
  267. {
  268.     Node* p = top;
  269.     while (p)
  270.     {
  271.         if (!almost_prime_list(p->data))
  272.             return false;
  273.         p = p->next;
  274.     }
  275.     return true;
  276. }
  277. Node* intersection(Node * top1, Node * top2)//(17)
  278. {
  279.     Node* top3, *p;
  280.     top3 = NULL;
  281.     p = top1;
  282.     while (p)
  283.     {
  284.         if (elem_in_list(top2, p->data))
  285.             push(top3, p->data);
  286.         p = p->next;
  287.     }
  288.     return top3;
  289. }
  290. Node* merge(Node * top1, Node * top2)
  291. {
  292.     Node* top3, * p, * q;
  293.     top3 = NULL;
  294.     p = top1;
  295.     q = top2;
  296.     int k = 0;
  297.     while (q && p)
  298.     {
  299.         if (k % 2 == 0)
  300.         {
  301.             push(top3, p->data);
  302.             p = p->next;
  303.         }
  304.         else
  305.         {
  306.             push(top3, q->data);
  307.             q = q->next;
  308.         }
  309.     }
  310.     return top3;
  311. }
  312.  
  313. int main()
  314. {
  315.     ifstream in("in.txt");
  316.     int n; cin >> n;
  317. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement