Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // stos szablon.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- template<typename T>
- class CStos
- {
- T* m_iWartosci; //Tablica przechowywanych elementów
- int m_iIloscElem; //Ilość obecnie przechowywanych elementów
- public:
- CStos(void);
- ~CStos(void);
- void Wrzuc(T Element); //Wrzuć nowy element na stos
- T Zdejmij(void); //Zdejmij element ze stosu
- void Wyswietl(void); //Wyswietl zawartosc stosu
- void Wyczysc(void); //Usuń wszystkie dane ze stosu
- inline bool JestPusty(void) { return !m_iIloscElem; }
- inline int DajDlugosc(void) { return m_iIloscElem; }
- };
- template<typename T>
- CStos<T>::CStos(void) : m_iWartosci(0), m_iIloscElem(0){}
- template<typename T>
- CStos<T>::~CStos(void)
- {
- Wyczysc();
- }
- template<typename T>
- void CStos<T>::Wrzuc(T Element)
- {
- T* tmp = NULL;
- tmp = new T [++m_iIloscElem];
- tmp[0] = Element;
- for (int i = 1; i<m_iIloscElem; i++)
- tmp[i] = m_iWartosci[i - 1];
- delete[] m_iWartosci;
- m_iWartosci = tmp;
- }
- template<typename T>
- T CStos<T>::Zdejmij(void)
- {
- T Pobrany = 0;
- if (m_iWartosci != NULL) {
- Pobrany = m_iWartosci[0];
- if (--m_iIloscElem) {
- T* tmp = NULL;
- tmp = new T[m_iIloscElem]; //Pomniejszona tablica
- for (int i = 0; i<m_iIloscElem; i++)
- tmp[i] = m_iWartosci[i + 1]; //Kopiowanie do mniejszej
- delete[] m_iWartosci;
- m_iWartosci = tmp; //Podlaczanie nowej-mniejszej tablicy
- }
- else Wyczysc();
- }
- else std::cerr << "Brak elementow na stosie!" << std::endl;
- return Pobrany;
- }
- template<typename T>
- void CStos<T>::Wyswietl(void)
- {
- for (int i = 0; i<m_iIloscElem; i++) std::cout << m_iWartosci[i] << ", ";
- std::cout << std::endl;
- }
- template<typename T>
- void CStos<T>::Wyczysc(void)
- {
- if (m_iWartosci != NULL) delete[] m_iWartosci;
- m_iWartosci = NULL;
- m_iIloscElem = 0;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement