Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <iostream>
- using namespace std;
- template <class T> class stos {
- private:
- T *t;
- unsigned int SIZE, MAX_SIZE;
- void Resize()
- {
- MAX_SIZE *= 10;
- T *Temp = new T[MAX_SIZE];
- for( unsigned int i = 0; i < SIZE; ++i ) Temp[i] = t[i];
- t = Temp;
- delete[] Temp;
- }
- public:
- stos(){
- MAX_SIZE=1;
- SIZE=0;
- Resize();
- }
- ~stos(){
- delete [] t; }
- bool empty(){
- if(SIZE==0) return true;
- else return false;
- }
- bool full(){
- if(SIZE==MAX_SIZE) return true;
- else return false; }
- void push(T x){
- if(SIZE == MAX_SIZE) Resize();
- else{
- t[SIZE]=x;
- SIZE++; }
- }
- void pop(){
- {
- SIZE--;}
- }
- T top(){
- return t[SIZE-1]; }
- unsigned int size(){
- return SIZE; }
- unsigned int max(){ return MAX_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. Sprawdz czy stos jest pelny"<<endl;
- cout<<"7. Zakoncz program."<<endl;
- int a;
- cin>>a;
- return a;
- }
- int main()
- {
- bool run = true;
- stos<int> s;
- 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<<"Rozmiar 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:
- {
- int 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:
- if(!s.full()){
- cout<<"zostalo: "<<s.max()-s.size()<<" wolnych miejsc"<<endl; }
- else cout<<"stos jest pelen"<<endl;
- break;
- case 7:
- {
- run = false;
- break;
- }
- };
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement