Advertisement
Garey

danny_zad_3_SAA

Apr 16th, 2018
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 KB | None | 0 0
  1.  
  2. struct element {
  3.     int key;
  4.     element*next;
  5.     element*prev;
  6. }*spisyk1 = nullptr, *spisyk2 = nullptr, *kombiniran = nullptr;
  7.  
  8. void list(element *& list)
  9. {
  10.     if (list) {
  11.         element *pointer = list;
  12.         while (pointer) {
  13.             cout << pointer->key << "\t";
  14.             pointer = pointer->next;
  15.         }
  16.     }
  17.     else
  18.         cout << "\nPrazen spisyk!!";
  19. }
  20.  
  21. void add_b(element*& list, int n)
  22. {
  23.     element * pointer = list;
  24.     list = new element;
  25.     list->key = n;
  26.     list->prev = nullptr;
  27.     list->next = pointer;
  28.     if (pointer)
  29.         pointer->prev = list;
  30. }
  31.  
  32. void add_e(element*& list, int n)
  33. {
  34.     element *q = new element;
  35.     q->next = nullptr;
  36.     q->key = n;
  37.     if (list) {
  38.         element *pointer = list;
  39.         while (pointer->next)
  40.             pointer = pointer->next;
  41.         pointer->next = q;
  42.         q->prev = pointer;
  43.     }
  44.     else {
  45.         list = q;
  46.         q->prev = nullptr;
  47.     }
  48. }
  49.  
  50. int del_k(element *& list, int k)
  51. {
  52.     if (list) {
  53.         element *p = list;
  54.         while (p->key != k && p->next)
  55.             p = p->next;
  56.         if (p->key == k) {
  57.             if (list->key == k) {
  58.                 list = list->next;
  59.                 if (list)
  60.                     list->prev = nullptr;
  61.             }
  62.             else {
  63.                 p->prev->next = p->next;
  64.                 if (p->next)
  65.                     p->next->prev = p->prev;
  66.             }
  67.             delete p;
  68.             return 1;
  69.         }
  70.         else {
  71.             cout << "\nNqma kvo da trie !";
  72.             return 0;
  73.         }
  74.     }
  75.     else {
  76.         cout << "\nPrazen spisyk";
  77.         return 0;
  78.     }
  79. }
  80.  
  81. void kombinirane() {
  82.  
  83.     if (spisyk1 == nullptr || spisyk2 == nullptr) {
  84.         cout << "Spisycite sa prazni";
  85.         exit(0);
  86.     }
  87.     else {
  88.         element *pomoshten;
  89.  
  90.         kombiniran = spisyk1;
  91.         pomoshten = kombiniran;
  92.  
  93.         while (pomoshten->next != nullptr)
  94.             pomoshten = pomoshten->next;
  95.  
  96.         pomoshten->next = spisyk2;
  97.         spisyk2->prev = pomoshten;
  98.  
  99.         while (pomoshten->next != nullptr)
  100.             pomoshten = pomoshten->next;
  101.     }
  102. }
  103.  
  104. int main()
  105. {
  106.     add_b(spisyk1, 3);
  107.     add_b(spisyk1, 7);
  108.     add_b(spisyk1, 9);
  109.  
  110.     add_b(spisyk2, 5);
  111.     add_b(spisyk2, 13);
  112.     add_b(spisyk2, 20);
  113.  
  114.     cout << "Spisyk 1: \n";
  115.     list(spisyk1);
  116.     cout << "\n\nSpisyk 2: \n";
  117.     list(spisyk2);
  118.  
  119.     cout << "\n\nKombiniran spisyk: \n";
  120.     kombinirane();
  121.     list(kombiniran);
  122.  
  123.     cout << endl << endl;
  124.  
  125.     return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement