Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. //
  2.  
  3. //#include "pch.h"
  4. #include <iostream>
  5. using namespace std;
  6. template <typename T>
  7. class Kopiec
  8. {   public:
  9.  
  10.     const int WSP_ROZSZERZENIA = 2;
  11.     int m_dlugosc;
  12.     int ilosc;
  13.     T* tab;
  14.  
  15.     Kopiec()
  16.     {
  17.         m_dlugosc = 1;
  18.         ilosc = 0;
  19.         tab = new T[m_dlugosc];
  20.     }
  21.     ~Kopiec()
  22.     {
  23.         delete[] tab;
  24.     }
  25.  
  26.     void dodaj(T nowyElement)
  27.     {
  28.         if (ilosc == m_dlugosc)
  29.         {
  30.             m_dlugosc = m_dlugosc * WSP_ROZSZERZENIA;
  31.             T* nowaTab = new T[m_dlugosc];
  32.             for (int i = 0; i < ilosc; i++)
  33.             {
  34.                 nowaTab[i] = tab[i];
  35.             }
  36.             delete[] tab;
  37.             tab = nowaTab;
  38.             nowaTab[ilosc] = nowyElement;
  39.             ilosc++;
  40.             Kwg(nowyElement);
  41.         }
  42.         else
  43.         {
  44.  
  45.             tab[ilosc] = nowyElement;
  46.             ilosc++;
  47.             Kwg(nowyElement);
  48.         }
  49.     }
  50.  
  51.     void Kwg(T element)
  52.     {
  53.         int tmp = ilosc - 1;
  54.         T temp;
  55.         while (tmp != 0)
  56.         {
  57.             if (tab[tmp] > tab[rodzic(tmp)])
  58.             {
  59.                 temp[tmp] = tab[rodzic(tmp)];
  60.                 tab[rodzic(tmp)] = tab[tmp];
  61.                 tab[tmp] = temp[tmp];
  62.                 tmp = rodzic(tmp);
  63.             }
  64.             else
  65.                 break;
  66.         }
  67.     }
  68.     /*void UsunNaj(T element)
  69.     {
  70.         T tym;
  71.         tab[1] = tab[ilosc];
  72.         int numeracja = ilosc;
  73.         for (int i = 0; i < ilosc; i++)
  74.         {
  75.             if (tab[i] < tab[lewe_dziecko(i)])
  76.             {
  77.                 tym[i] = tym[lewe_dziecko(i)];
  78.             }
  79.         }
  80.     }*/
  81.  
  82.     int rodzic(int i)
  83.     {
  84.         return (i - 1) / 2;
  85.     }
  86.     int lewe_dziecko(int i)
  87.     {
  88.         return 2 * i + 1;
  89.  
  90.     }
  91.     int prawe_dziecko(int i)
  92.     {
  93.         return 2 * i + 2;
  94.     }
  95.  
  96.  
  97.  
  98.  
  99. };
  100. int main()
  101. {
  102.     Kopiec<int>* kopiec = new Kopiec<int>();
  103.     kopiec->Kwg(10);
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement