Advertisement
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.peek()!=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 addLast(node * & head1, node * & tail, node * p)
- {
- tail->next = p;
- tail = p;
- }
- void newEl(node*&q, int x)
- {
- q = new node;
- q->info = x;
- q->next = NULL;
- }
- void createListFromFileTail(node * & head1, ifstream & f)
- {
- head1 = new node;
- setZero(head1->next);
- node * q, *tail = head1;
- int x;
- while (f.peek() != EOF)
- {
- f >> x;
- createElem(q, x);
- addLast(head1, tail, q);
- }
- q = head1;
- moove(head1);
- delete q;
- }
- void task26a(node*head1, node*head2, node*& head)
- {
- head = new node;
- setZero(head->next);
- node *q1, *q2, *q, *tail;
- setHead(head1, q1);
- setHead(head2, q2);
- setHead(head,tail);
- int x;
- if (ListIsEmpty(head1) && ListIsEmpty(head2))
- cout << "два пустых листа";
- else
- {
- if (ListIsEmpty(head1))
- cout << "Первый лист пустой"<<endl;
- if (ListIsEmpty(head2))
- cout << "Второй лист пустой"<<endl;
- }
- while (!nodeIsNull(q1) && !nodeIsNull(q2))
- {
- if (q1->info <= q2->info)
- {
- x = q1->info;
- moove(q1);
- }
- else
- {
- x = q2->info;
- moove(q2);
- }
- newEl(q, x);
- addLast(head, tail, q);
- }
- if (nodeIsNull(q1))
- while (!nodeIsNull(q2))
- {
- x = q2->info;
- moove(q2);
- newEl(q, x);
- addLast(head, tail, q);
- }
- if (nodeIsNull(q2))
- while (!nodeIsNull(q1))
- {
- x = q1->info;
- moove(q1);
- newEl(q, x);
- addLast(head, tail, q);
- }
- q = head;
- moove(head);
- delete q;
- }
- void task26b(node*&head1, node*&head2)
- {
- node*q1, *q2, *p;
- q2=head2;
- q1=head1;
- if (ListIsEmpty(head1))
- cout << "Первый лист пустой" << endl;
- if (ListIsEmpty(head2))
- cout << "Второй лист пустой" << endl;
- if (!ListIsEmpty(head2) && !ListIsEmpty(head1))
- {
- while (!nodeIsNull(q1->next) && !nodeIsNull(q2->next))
- {
- while ((head2->info) <= (head1->info))
- {
- p = head2;
- moove(head2);
- p->next = head1;
- head1 = p;
- q2 = head2;
- q1 = head1;
- }
- if (q2->info <= q1->next->info)
- {
- p = q2;
- moove(q2);
- p->next = q1->next;
- q1->next = p;
- }
- else moove(q1);
- }
- if (nodeIsNull(q1->next) && !nodeIsNull(q2->next))
- {
- q1->next = q2;
- }
- }
- }
- int main()
- {
- node * L1;
- node * L2;
- node * L3;
- setlocale(LC_ALL, "RUS");
- ifstream f1("input.txt");
- createListFromFileTail(L1, f1);
- showlist(L1);
- f1.close();
- cout << endl;
- ifstream f2("input1.txt");
- createListFromFileTail(L2, f2);
- showlist(L2);
- f2.close();
- cout << endl;
- /*
- task26a(L1, L2, L3);
- showlist(L3);
- */
- task26b(L1, L2);
- showlist(L1);
- cout << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement