Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<fstream>
- #include<string>
- using namespace std;
- struct Node
- {
- int data;
- Node* next;
- };
- bool is_Empty(Node*& top) {
- return top == NULL;
- }
- Node* previous(Node* top, Node* p) {
- Node* pr = new Node, * q = top;
- if (q == NULL)
- exit(-1);
- else {
- while (q && q != p) {
- pr = q;
- q = q->next;
- }
- }
- if (pr->next == p) return pr;
- return NULL;
- }
- Node* previous(Node * top, int data) {
- Node* pr = NULL, * q = top;
- if (!q)
- exit(-1);
- while (q && q->data != data) {
- pr = q;
- q = q->next;
- }
- if (pr->next == q) return pr;
- return NULL;
- }
- void show_list(Node* top) {
- Node* p = top;
- while (p) {
- cout << p->data << ' ';
- p = p->next;
- }
- }
- void show_lists(Node**& list, int n) {
- for (int i = 0; i < n; i++) {
- show_list(list[i]);
- cout << endl;
- }
- }
- void push(Node * &top, int data) {
- Node* p = new Node;
- p->data = data;
- p->next = top;
- top = p;
- }
- int pop(Node * &top) {
- Node* p = top->next;
- int data = top->data;
- delete top;
- top = p;
- return data;
- }
- int pop(Node * &top, Node * q)
- {
- int data;
- if (q == top)
- data = pop(top);
- else
- {
- Node* pr = previous(top, q);
- pr->next = q->next;
- data = q->data;
- delete q;
- }
- return data;
- }
- void pop(Node * &top, int data)
- {
- Node* p = top;
- while (p && p->data != data)
- {
- p = p->next;
- }
- pop(top, p);
- }
- Node* create_list(string s) {
- Node* top = NULL;
- for (int i = 0; i < s.length(); i++) {
- if (s[i] == ' ') {
- break;
- }
- else {
- push(top, s[i]-48);
- }
- }
- return top;
- }
- Node* create_list(ifstream & f) {
- int x;
- string s;
- Node* top = NULL;
- getline(f, s);
- top = create_list(s);
- return top;
- }
- Node** create_lists(ifstream& f, int n) {
- int i = 0;
- Node** list = new Node*[n];
- string s;
- while (getline(f, s)) {
- list[i] = create_list(s);
- i++;
- }
- return list;
- }
- void sort(Node * top)
- {
- int data;
- Node* p, * q;
- p = top;
- while (p->next)
- {
- q = p->next;
- while (q)
- {
- if (p->data > q->data)
- {
- data = p->data;
- p->data = q->data;
- q->data = data;
- }
- q = q->next;
- }
- p = p->next;
- }
- }
- bool elem_in_list(Node* top, int data)
- {
- Node* q;
- q = top;
- int k = 0;
- while (q)
- {
- if (q->data == data)
- k++;
- q = q->next;
- }
- if (k > 1)
- return false;
- else
- return true;
- }
- void into_array(Node * &top)
- {
- Node* p = top->next;
- while (p)
- {
- if (!elem_in_list(top, p->data))
- {
- Node* r = previous(top, p->data);
- pop(top, p->data);
- p = r;
- }
- p = p->next;
- }
- }
- void preobr(Node * *&a, int n)
- {
- for (int i = 0; i < n; i++)
- into_array(a[i]);
- }
- 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;
- }
- bool arr_twins(Node* top)
- {
- Node* p = top;
- while (p)
- {
- if (prime(p->data))
- if ((prime(p->data + 2) && elem_in_list(top, p->data + 2))
- || (prime(p->data + 2) && elem_in_list(top, p->data - 2)))
- return true;
- p = p->next;
- }
- return false;
- }
- int max_in_list(Node * top)
- {
- int max = top->data;
- Node* p = top->next;
- while (p)
- {
- if (max < p->data)
- max = p->data;
- p = p->next;
- }
- return max;
- }
- int max_in_lists(Node**a, int n)
- {
- int i, max;
- max = max_in_list(a[0]);
- for (i = 1; i < n; i++)
- if (max_in_list(a[i]) > max)
- max = max_in_list(a[i]);
- return max;
- }
- Node* list_power_2(int max)
- {
- int a = 1;
- Node* top = new Node;
- while (a <= max)
- {
- push(top, a);
- a *= 2;
- }
- return top;
- }
- bool almost_prime_list(int x)
- {
- int k = 0;
- for (int i = 2; i < x; i++)
- {
- if (x % i == 0)
- {
- if (prime(i))
- k++;
- else
- return false;
- }
- if (k > 2)
- return false;
- }
- return true;
- }
- bool truly_prime_list(Node* top)
- {
- Node* p = top;
- while (p)
- {
- if (!almost_prime_list(p->data))
- return false;
- p = p->next;
- }
- return true;
- }
- Node* intersection(Node * top1, Node * top2)//(17)
- {
- Node* top3, *p;
- top3 = NULL;
- p = top1;
- while (p)
- {
- if (elem_in_list(top2, p->data))
- push(top3, p->data);
- p = p->next;
- }
- return top3;
- }
- Node* merge(Node * top1, Node * top2)
- {
- Node* top3, * p, * q;
- top3 = NULL;
- p = top1;
- q = top2;
- int k = 0;
- while (q && p)
- {
- if (k % 2 == 0)
- {
- push(top3, p->data);
- p = p->next;
- }
- else
- {
- push(top3, q->data);
- q = q->next;
- }
- }
- return top3;
- }
- int main()
- {
- ifstream in("in.txt");
- int n; cin >> n;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement