Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include"pch.h"
- #include <iostream>
- #include <fstream>
- using namespace std;
- struct node
- {
- int data;
- node*next;
- };
- void create(node*&top, int n)
- {
- node*p = new node;
- int i;
- top = NULL;
- for (i = 0; i < n; i++)
- {
- cout << "Enter p->data";
- cin >> p->data;
- p->next = top;
- top = p;
- }
- }
- int lenght_of_list(node*top)
- {
- int l = 0;
- node*p = new node;
- p = top;
- while (p)
- {
- l++;
- p = p->next;
- }
- return l;
- }
- void count_info(node*&top, int x)
- {
- int s = 0;
- node*p = new node;
- p = top;
- while (p && (p->data != x))
- p = p->next;
- s++;
- }
- int different_info(node*&top)
- {
- int i;
- int k = 0;
- bool f;
- int n = sizeof(top);
- int *a = new int[n];
- node*p = new node;
- p = top;
- for (i = 0; i < n; i++)
- {
- a[i] = p->data;
- p = p->next;
- }
- int j;
- for (i = 0; i < n; i++)
- {
- f = true;
- for (j = 0; j < n; j++)
- if (a[i] == a[j] && i != j)
- f = false;
- }
- if (f)
- k++;
- return k;
- }
- node* find_address(node*top, int k)
- {
- node*p;
- p = top;
- while (p && k > 0)
- {
- p = p->next;
- k--;
- }
- return p;
- }
- node* find_data(node*top, int info)// zadanye 2
- {
- node*p = top;
- while (p && (p->data != info))
- p = p->next;
- return p;
- }
- void create_list(node* &top, ifstream&f)//exercise 3
- {
- int i;
- node* p;
- top = new node;
- top = NULL;
- while (f)
- {
- p = new node;
- f >> p->data;
- p->next = top;
- top = p;
- }
- }
- bool prime(int x)
- {
- bool p = true;
- if (x <= 1)
- return false;
- if (x == 2)
- return true;
- for (int i = 2; i*i <= x && p; i++)
- {
- if (x%i == 0)
- p = false;
- }
- return p;
- }
- void create_prime(node*top, int n)
- {
- node*p;
- top = new node;
- top = NULL;
- for (int i = 0; n <= 0; i++)
- {
- if (prime(i))
- {
- p = new node;
- p->data = i;
- p->next = top;
- top = p;
- n--;
- }
- }
- }
- node* find_address_last_member(node*top)//нахождение адреса последнего элемента
- {
- node*p = top;
- while (p->next)
- p = p->next;
- return p;
- }
- void add(node*top1, node*top2)//соединение 2 списков
- {
- node*p = find_address_last_member(top1);
- p->next = top2;
- }
- int min(node*top)
- {
- int m = top->data;
- while (top)
- {
- if (top->data < m)
- m = top->data;
- top = top->next;
- }
- return m;
- }
- int sum(node*top)
- {
- int s = 0;
- node*p = top;
- while (p)
- {
- s += p->data;
- p = p->next;
- }
- return s;
- }
- void delete_top(node*&top)
- {
- node*p;
- if (top != NULL)
- {
- p = top->next;
- delete top;
- top = p;
- }
- }
- void delete_k(node*&top, int k)
- {
- node*p = top;
- int s = 0;
- for (s = 0; s < k; s++)
- p = p->next;
- delete_top(p);
- }
- void delete_info(node*&top, int x)
- {
- node*p = top;
- while (p->data != x)
- p = p->next;
- delete_top(p);
- }
- void delete_p(node*&top, node*&p)
- {
- while (top->next != p->next)
- top = top->next;
- delete_top(top);
- }
- void add_first(node*&top, int data)
- {
- node*p;
- p->data = data;
- p->next = top;
- top = p;
- }
- void add_last(node*&top, int data)
- {
- node*l;
- node*q;
- l = find_address_last_member(top);
- q->data = data;
- q->next = NULL;
- l->next = q;
- }
- void two_lists(node*top, int k)
- {
- node*p1;
- node*p2;
- p1 = NULL;
- p2 = NULL;
- int s = 0;
- for (s = 0; s < k; s++)
- add_first(p1, top->data);
- add_first(p2, top->data);
- }
- void two_lists1(node*top)
- {
- node*p1;
- node*p2;
- p1 = NULL;
- p2 = NULL;
- for (int i = 1; i <= lenght_of_list(top); i++)
- {
- if (i % 2 == 0)
- add_first(p2, top->data);
- else
- add_first(p1, top->data);
- }
- }
- node* previous(node* top, node* p)
- {node* pr, *q;
- q = top;
- pr = NULL;
- while (q != p && q)
- { pr = q;
- q = q->next;
- }
- if (q == p) return pr;
- return NULL;
- }
- void reverse_list(node*&top, int n)
- {
- node*q = top;
- node*p;
- node*m = new node;
- p = find_address_last_member(top);
- for (int i = 0; i = n / 2; i++)
- {
- m = top;
- top = p;
- p = m;
- top = top->next;
- p = previous(top, p);
- }
- }
- bool underlist(node*p, node*q)
- {
- bool t = true;
- if (p->data != q->data)
- p = p->next;
- else
- {
- if (p->data = q->data && p && q)
- {
- p = p->next;
- q = q->next;
- }
- else t = false;
- }
- return t;
- }
- void sort(node*&top)//сортировка списка
- {
- for (node*p = top; p->next; p = p->next)
- for (node* q = p->next; q; q = q->next)
- {
- if (p->data < q->data)
- {
- int r = p->data;
- p->data = q->data;
- q->data = r;
- }
- }
- }
- node*&mix(node*p, node*q)
- {
- node*u;
- u = find_address_last_member(p);
- u->next = q;
- }
- node*&mix_lists(node*&q, node*&p)
- {
- sort(q);
- sort(p);
- sort(mix(p, q));
- }
- bool is_Member(node* top, int data)
- {
- if (find_address(top, data)) return true; else return false;
- }
- node* set_U(node*s, node*t)
- {
- node*U = NULL;
- while (s)
- {
- add_first(U, s->data);
- s = s->next;
- }
- while (t)
- {
- if (!is_Member(s, t->data))
- {
- add_first(s, t->data);
- t = t->next;
- }
- }
- return U;
- }
- node* set_V(node*s, node*t)
- {
- node*V = NULL;
- while (t)
- {
- if (is_Member(s, t->data))
- add_first(V, t->data);
- t = t->next;
- }
- return V;
- }
- node* set_W(node*s, node*t)
- {
- node*W = NULL;
- while (s)
- {
- add_first(W, s->data);
- s = s->next;
- }
- if (is_Member(W, t->data))
- {
- delete_info(W, t->data);
- t = t->next;
- }
- return W;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement