Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- struct node
- {
- int info;
- node* next;
- };
- void createElem(node *& p, int x)
- {
- p = new node;
- p->info = x;
- p->next = NULL;
- }
- void addFirst(node *& head, node * p)
- {
- p->next = head;
- head = p;
- }
- void setZero(node *& p)
- {
- p = NULL;
- }
- void createlistFromFile(node *& head, ifstream & f)
- {
- node*q;
- int x;
- setZero(head);
- while (!f.eof())
- {
- f >> x;
- createElem(q, x);
- addFirst(head, q);
- }
- }
- void setHead(node * head, node *& p)
- {
- p = head;
- }
- bool nodeIsNull(node *p)
- {
- return p == NULL;
- }
- void showNode(node * p)
- {
- cout << p->info << ' ';
- }
- void moove(node *& p)
- {
- p = p ->next;
- }
- void showlist(node*head)
- {
- node *q;
- setHead(head,q);
- while (!nodeIsNull(q))
- {
- showNode(q);
- moove(q);
- }
- }
- bool ListIsEmpty(node * head)
- {
- return head == NULL;
- }
- bool CompInfo(node * p, node * q)
- {
- return p->info == q->info;
- }
- void LastEl(node *& p)
- {
- while (!nodeIsNull(p->next))
- moove(p);
- }
- void PrevEl(node*&q, node*&p)
- {
- while (!nodeIsNull(q->next->next))
- moove(q);
- p = q->next;
- }
- bool task23a(node * L1, node * L2)
- {
- node *l1;
- node *l2;
- setHead(L1,l1);
- setHead(L2,l2);
- bool t = true;
- while (!nodeIsNull(l1) && !nodeIsNull(l2) && t)
- {
- if (!CompInfo(l1, l2))
- t = false;
- moove(l1);
- moove(l2);
- }
- return t && nodeIsNull(l1) && nodeIsNull(l2);
- }
- bool task23c(node * L1)
- {
- node *l1;
- node *l2;
- setHead(L1, l1);
- bool f = false;
- while (!nodeIsNull(l1))
- {
- l2 = l1->next;
- while (!nodeIsNull(l2) )
- {
- if (l1->info == l2->info)
- {
- f = true;
- }
- moove(l2);
- }
- moove(l1);
- }
- return f;
- }
- void task23d(node *& L1)
- {
- if (nodeIsNull(L1->next))
- {
- cout << "error!" << endl;
- }
- else
- {
- node *q, *p;
- setHead(L1, p);
- moove(L1);
- setHead(L1, q);
- while (!nodeIsNull(q->next))
- {
- moove(q);
- }
- q->next=p;
- p->next = NULL;
- }
- }
- void task23e(node *& L1)
- {
- if (nodeIsNull(L1->next))
- {
- cout << "error!" << endl;
- }
- else
- {
- node *l1;
- setHead(L1, l1);
- node *l2 = new node;
- while (!nodeIsNull(l1->next->next))
- moove(l1);
- l2 = l1->next;
- l1->next = NULL;
- l2->next = L1;
- L1 = l2;
- }
- }
- bool task23B(node *head1, node *head2)
- {
- if (!nodeIsNull(head1) && !nodeIsNull(head2))
- {
- node *q = head1;
- node *p = head2;
- bool allT = true;
- bool E;
- while (p != NULL && allT)
- {
- E = false;
- while (q != NULL && !E && (p->info >= q->info))
- {
- if (CompInfo(p, q))
- E = true;
- else
- moove(q);
- }
- if (!E)
- allT = false;
- moove(p);
- }
- return allT;
- }
- else
- return false;
- }
- bool check(node*head1, node*head2)
- {
- bool t = true;
- node *l1, *l2;
- setHead(head1, l1);
- setHead(head2, l2);
- while (t && !nodeIsNull(l1) && !nodeIsNull(l2))
- {
- if (!(CompInfo(l1, l2)))
- t = false;
- moove(l1);
- moove(l2);
- }
- return (t&&nodeIsNull(l2));
- }
- bool task23b_Not_Upor_Point(node * L1, node * L2)
- {
- node *p1, *p2;
- setHead(L1, p1);
- setHead(L2, p2);
- bool f = false;
- while (!nodeIsNull(p1) && !f)
- {
- if (check(p1, p2))
- f = true;
- moove(p1);
- }
- return f;
- }
- void task23f(node *& L1, node *& L2)
- {
- if (L1->next == NULL)
- L1 = L2;
- else
- {
- node *l1, *l2;
- node *l3 = new node;
- setHead(L1, l1);
- setHead(L2, l2);
- while (!nodeIsNull(l2))
- {
- l3->next = NULL;
- while (!nodeIsNull(l1->next))
- {
- moove(l1);
- }
- l3 = l2;
- l1->next = l3;
- moove(l2);
- }
- }
- }
- void task23g(node *& L1, int x, node *& L2)
- {
- if (nodeIsNull(L1))
- cout << "eror1" << endl;
- else if (nodeIsNull(L2))
- cout << "eror2" << endl;
- else
- {
- node* q1, *q2, *p;
- bool fl = true;
- setHead(L1, q1);
- setHead(L2, q2);
- while (!nodeIsNull(q1) && fl)
- {
- if (q1->info == x)
- {
- p=q1->next;
- q1->next=q2;
- LastEl(q2);
- q2->next=p;
- fl = false;
- }
- else
- moove(q1);
- }
- }
- }
- void task23z(node *& L1)
- {
- if (L1->next == NULL)
- cout << "eror" << endl;
- else if (nodeIsNull(L1->next))
- {
- node *l1;
- l1 = L1->next,
- L1->next = NULL;
- l1->next = L1;
- L1 = l1;
- }
- else
- {
- node* l2 = NULL, *l3, *l4;
- setHead(L1, l4);
- LastEl(L1);
- while (l2 != l4)
- {
- setHead(l4, l2);
- PrevEl(l2, l3);
- l3->next = l2;
- l2->next = NULL;
- }
- }
- }
- void task23i(node *& L1)
- {
- node *l1,*l2,*l3,*l4;
- setHead(L1, l1);
- if (L1->next == NULL)
- cout << "eror ";
- else
- {
- while (!nodeIsNull(l1->next->next))
- {
- if (l1->info == l1->next->info)
- {
- l2 = l1->next;
- l1->next = l1->next->next;
- delete l2;
- }
- else
- moove(l1);
- }
- if (l1->next->info == l1->info)
- {
- l3 = l1;
- l4 = l3->next;
- l3->next = NULL;
- delete l4;
- }
- }
- }
- void deleteElement(node *& q)
- {
- node*p;
- p=q->next;
- q->next=q->next->next;
- delete p;
- }
- void task23k(node *& L1)
- {
- if (!nodeIsNull(L1))
- {
- node *p;
- node *q = L1;
- node *c;
- int Neto;
- while (!nodeIsNull(q))
- {
- p = q;
- c = q;
- moove(p);
- Neto = q->info;
- while (!nodeIsNull(p))
- {
- if (Neto == p->info)
- {
- moove(p);
- deleteElement(c);
- }
- else
- {
- moove(p);
- moove(c);
- }
- }
- moove(q);
- }
- showlist(L1);
- }
- }
- int main()
- {
- node * L1;
- node * L2;
- setlocale(LC_ALL, "RUS");
- ifstream f1("input.txt");
- createlistFromFile(L1, f1);
- showlist(L1);
- f1.close();
- cout << endl;
- ifstream f2("input2.txt");
- createlistFromFile(L2, f2);
- showlist(L2);
- f2.close();
- cout << endl;
- /* if (!ListIsEmpty(L1) && !ListIsEmpty(L2))
- cout << task23a(L1,L2);
- */
- if (!ListIsEmpty(L1) && !ListIsEmpty(L2))
- cout << task23B(L1, L2);
- /*if (!ListIsEmpty(L1) && !ListIsEmpty(L2))
- cout<< task23b_Not_Upor_Point(L1, L2);
- */
- /*
- if (!ListIsEmpty(L1))
- cout << task23c(L1);
- */
- /*
- task23d(L1);
- showlist(L1);
- */
- /*
- task23e(L1);
- showlist(L1);
- */
- /*
- task23f(L1, L2);
- showlist(L1);
- */
- /*
- if (!ListIsEmpty(L1) && !ListIsEmpty(L2))
- {
- task23g(L1, 2, L2);
- showlist(L1);
- }
- */
- /*
- task23z(L1);
- showlist(L1);
- */
- /*
- task23i(L1);
- showlist(L1);
- */
- // task23k(L1);
- cout << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment