Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- //#include "pch.h"
- #include <iostream>
- using namespace std;
- template <typename T>
- class Kopiec
- { public:
- const int WSP_ROZSZERZENIA = 2;
- int m_dlugosc;
- int ilosc;
- T* tab;
- Kopiec()
- {
- m_dlugosc = 1;
- ilosc = 0;
- tab = new T[m_dlugosc];
- }
- ~Kopiec()
- {
- delete[] tab;
- }
- void dodaj(T nowyElement)
- {
- if (ilosc == m_dlugosc)
- {
- m_dlugosc = m_dlugosc * WSP_ROZSZERZENIA;
- T* nowaTab = new T[m_dlugosc];
- for (int i = 0; i < ilosc; i++)
- {
- nowaTab[i] = tab[i];
- }
- delete[] tab;
- tab = nowaTab;
- nowaTab[ilosc] = nowyElement;
- ilosc++;
- Kwg(nowyElement);
- }
- else
- {
- tab[ilosc] = nowyElement;
- ilosc++;
- Kwg(nowyElement);
- }
- }
- void Kwg(T element)
- {
- int tmp = ilosc - 1;
- T temp;
- while (tmp != 0)
- {
- if (tab[tmp] > tab[rodzic(tmp)])
- {
- temp[tmp] = tab[rodzic(tmp)];
- tab[rodzic(tmp)] = tab[tmp];
- tab[tmp] = temp[tmp];
- tmp = rodzic(tmp);
- }
- else
- break;
- }
- }
- /*void UsunNaj(T element)
- {
- T tym;
- tab[1] = tab[ilosc];
- int numeracja = ilosc;
- for (int i = 0; i < ilosc; i++)
- {
- if (tab[i] < tab[lewe_dziecko(i)])
- {
- tym[i] = tym[lewe_dziecko(i)];
- }
- }
- }*/
- int rodzic(int i)
- {
- return (i - 1) / 2;
- }
- int lewe_dziecko(int i)
- {
- return 2 * i + 1;
- }
- int prawe_dziecko(int i)
- {
- return 2 * i + 2;
- }
- };
- int main()
- {
- Kopiec<int>* kopiec = new Kopiec<int>();
- kopiec->Kwg(10);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement