Stos template...
- Pobierz link
- X
- Inne aplikacje
dobra, tutaj jest kolejna, tym razem pełniejsza implementacja stosu...
z użyciem szablonu oczywiście
szczerze mówiąc, ta wersja nie przypadła mi do gustu, ale kto co woli
klik:
klik:
- #include<iostream>
- #include<string>
- using namespace std;
- template<class T, int N>
- class stos
- {
- private:
- T *t;
- int Size;
- public:
- stos();
- ~stos();
- bool empty();
- bool full();
- void push(T&);
- void pop();
- T& top();
- int size();
- };
- template<class T, int N>
- stos<T,N>::stos()
- {
- t=new T[N];
- Size=0;
- };
- template<class T, int N>
- stos<T,N>::~stos()
- {
- delete[] t;
- };
- template<class T, int N>
- bool stos<T,N>::empty()
- {
- return Size==0;
- }
- template<class T, int N>
- bool stos<T,N>::full()
- {
- return Size==N;
- }
- template<class T, int N>
- void stos<T,N>::push(T& x)
- {
- t[Size]=x;
- Size+=1;
- }
- template<class T, int N>
- void stos<T,N>::pop()
- {
- if (empty()) cout<<"Stos jest pusty"<<endl;
- else Size+=-1;
- }
- template<class T, int N>
- T& stos<T,N>::top()
- {
- return t[Size-1];
- }
- template<class T, int N>
- int stos<T,N>::size()
- {
- return Size;
- }
- int menu()
- {
- cout<<"\n......:::::MENU:::::......\n"<<endl;
- cout<<"1. Sprawdz czy stos jest pusty."<<endl;
- cout<<"2. Sprawdz rozmiar stosu."<<endl;
- cout<<"3. Najwyzszy element stosu"<<endl;
- cout<<"4. Dodaj element do stosu"<<endl;
- cout<<"5. Usun element ze stosu"<<endl;
- cout<<"6. Zakoncz program."<<endl;
- int a;
- cin>>a;
- return a;
- }
- int main()
- {
- bool run = true;
- stos<string,3> s; //tu akurat można kłaść stringi, ale
- while(run)
- {
- switch(menu())
- {
- case 1:
- {
- if(s.empty())
- {
- cout<<"stos jest pusty."<<endl;
- }
- else
- {
- cout<<"stos nie jest pusty."<<endl;
- }
- break;
- }
- case 2:
- {
- cout<<"Rozmaiar stosu: "<<s.size()<<endl;
- break;
- }
- case 3:
- {
- if(s.empty())
- {
- cout<<"stos jest pusty."<<endl;
- }
- else
- {
- cout<<"Najwyzszy element na stosie: "<<s.top()<<endl;
- }
- break;
- }
- case 4:
- {
- string i;
- cout<<"Podaj liczbe jaka chcesz umiescic na stosie:"<<endl;
- cin>>i;
- s.push(i);
- break;
- }
- case 5:
- {
- if(!s.empty())
- {
- s.pop();
- }
- else
- {
- cout<<"Nie mozna usunac elementu ze stosu poniewaz stos jest pusty."<<endl;
- }
- break;
- }
- case 6:
- {
- run = false;
- break;
- }
- };
- }
- return 0;
- }
- Pobierz link
- X
- Inne aplikacje
Komentarze
Prześlij komentarz