Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // test.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include<cstdlib>
- #include<iostream>
- using namespace std;
- struct elem
- {
- int a;
- elem *next;
- };
- elem *head = NULL;
- elem *head2 = NULL;
- void push(int a)
- {
- elem *p = new elem;
- p->a = a;
- p->next = head;
- head = p;
- }
- int pop()
- {
- if (head == NULL)return NULL;
- int x = head->a;
- elem *p = head;
- head = head->next;
- delete p;
- return x;
- }
- int pop2()
- {
- if (head2 == NULL)return NULL;
- int x = head2->a;
- elem *p = head2;
- head2 = head2->next;
- delete p;
- return x;
- }
- bool empty()
- {
- if (head == NULL)return true;
- return false;
- }
- bool empty2()
- {
- if (head2 == NULL)return true;
- return false;
- }
- void zamienPierwszeDwaParzyste()
- {
- if (head == NULL || head->next == NULL)
- {
- cout << "Stos ma mniej niz dwa elementy." << endl;
- return;
- }
- int x=0;
- elem *p1=NULL, *p2=NULL, *h1=head;
- for (; h1 != NULL; h1 = h1->next)
- {
- if ((h1->a) % 2 == 0)
- {
- if (x == 0)
- {
- p1 = h1;
- x = 1;
- }
- else
- {
- p2 = h1;
- x = 2;
- break;
- }
- }
- }
- if (x != 2)
- {
- cout << "Nie znaleziono dwoch elementow parzystych." << endl;
- return;
- }
- swap(p1->a, p2->a);
- cout << "Zamieniono!" << endl;
- }
- bool przerzucSpelniajaceNaDrugi()
- {
- //warunek: klucz ma byc nieparzysty
- head2 = NULL;
- if (head == NULL)return false;
- elem *pom, *h1=head;
- while (head != NULL && head->a % 2 == 1)
- {
- pom = head->next;
- head->next = head2;
- head2 = head;
- head = pom;
- }
- for (; h1->next != NULL; h1 = h1->next)
- {
- if (h1->next->a % 2 == 1)
- {
- pom = h1->next;
- h1->next = pom->next;
- pom->next = head2;
- head2 = pom;
- }
- }
- return true;
- }
- void wypiszStos(elem *p)
- {
- while (p != NULL)
- {
- cout << p->a << endl;
- p = p->next;
- }
- }
- int main()
- {
- push(5);
- push(4);
- push(10);
- push(3);
- push(1);
- zamienPierwszeDwaParzyste();
- wypiszStos(head);
- if (przerzucSpelniajaceNaDrugi())
- {
- cout << "Przerzucono!" << endl;
- cout << "Pierwszy stos:" << endl;
- wypiszStos(head);
- cout << "Drugi stos:" << endl;
- wypiszStos(head2);
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement