Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- const int max_size = 50;
- struct magacin {
- int idx, niza[max_size];
- void init() {
- idx = -1;
- }
- bool isEmpty() {
- if(idx == -1) {
- return true;
- }
- return false;
- }
- bool isFull() {
- if(idx == max_size - 1) {
- return true;
- }
- return false;
- }
- void push(int x) {
- if(isFull()) {
- cout << "Stekot e full" << endl;
- return;
- }
- idx++;
- niza[idx] = x;
- }
- int pop() {
- if(isEmpty()) {
- cout << "Prazen stek" << endl;
- exit(0);
- }
- return niza[idx--];
- }
- int top() {
- if(isEmpty()) {
- cout << "prazen stek" << endl;
- exit(0);
- }
- return niza[idx];
- }
- };
- struct node {
- int val;
- node *next, *prev;
- };
- struct DoublyLinkedList {
- node *head, *tail;
- void init() {
- head = NULL;
- tail = NULL;
- }
- void dodadiPrv(int x) {
- node *tmp = new node;
- tmp->val = x;
- if(head == NULL) {
- head = tmp;
- tail = head;
- }
- else {
- tmp->next = head;
- head->prev = tmp;
- head = tmp;
- }
- }
- void dodadiPosleden(int x) {
- node *tmp = new node;
- tmp->val = x;
- if(head == NULL) {
- head = tmp;
- tail = head;
- }
- else {
- tail->next = tmp;
- tmp->prev = tail;
- tail = tmp;
- }
- }
- void brishiPrv() {
- if(head->next == NULL) {
- delete head;
- head = NULL;
- tail = NULL;
- }
- if(head != NULL) {
- node *tmp = head;
- head = head->next;
- delete tmp;
- }
- }
- void brishiPosleden() {
- if(head->next == NULL) {
- delete head;
- head = NULL;
- tail = NULL;
- }
- else {
- node *tmp = tail;
- tail = tail->prev;
- delete tail;
- }
- }
- void brishiLista() {
- while(head != NULL) {
- brishiPrv();
- }
- }
- void pechati() {
- node *tmp = head;
- while(tmp != NULL) {
- cout << tmp->val << " <--> ";
- tmp = tmp->next;
- }
- cout << endl;
- }
- bool dodadiPosle(int el) {
- node *tmp = head;
- bool ok = false;
- while(tmp != NULL) {
- if(tmp->val == el) {
- ok = true;
- break;
- }
- tmp = tmp->next;
- }
- if(ok) {
- node * new_node = new node;
- new_node->val = el;
- if(tmp->next == NULL) {
- dodadiPosleden(el);
- return ok;
- }
- node *sleden = tmp->next;
- tmp->next = new_node;
- new_node->prev = tmp;
- new_node->next = sleden;
- sleden->prev = new_node;
- }
- return ok;
- }
- };
- void raspredelba(magacin m, DoublyLinkedList & even, DoublyLinkedList & odd) {
- while(!m.isEmpty()) {
- int el = m.top();
- m.pop();
- if(el != 0) {
- if(el % 2 == 0 && el <= 10) {
- if(!even.dodadiPosle(el)) {
- even.dodadiPosleden(el);
- }
- }
- else if(el % 2 == 1 && el > 10){
- if(!odd.dodadiPosle(el)) {
- odd.dodadiPosleden(el);
- }
- }
- }
- }
- }
- int main()
- {
- magacin m;
- m.init();
- for(int i = 0; i < 13; i++) {
- int x;
- cin >> x;
- m.push(x);
- }
- DoublyLinkedList even, odd;
- even.init();
- odd.init();
- raspredelba(m, even, odd);
- cout << "Parni: ";
- even.pechati();
- cout << "Neparni: ";
- odd.pechati();
- return 0;
- }
- // 8 0 17 4 9 16 5 25 0 10 17 33 8
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement