document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include <iostream>
  2. #include <stdlib.h>
  3.  
  4. using namespace std;
  5.  
  6. template <typename type>
  7. class Stack {    
  8.  
  9.  
  10.  
  11.     struct Element {
  12.         type value;
  13.         Element *prev;  //wskaźnik na poprzedni element
  14.     } *tail;            //ogon listy
  15.  
  16.  
  17. public:
  18.  
  19.     Stack(){
  20.         tail=NULL;          // ogon listy - wierzchołek stosu na poczatku jest NULL
  21.     };
  22.  
  23.     void push(type val){    //tworzy liste jedna kierunkowa ze wskaznikiem na wczesniejszy element listy
  24.         struct Element *e = new Element;
  25.         e->value = val;
  26.         e->prev = tail;
  27.         tail = e;           // nowy element staje sie ogonem - wierzcholkiem stosu
  28.  
  29.     };
  30.  
  31.     type pop(){
  32.         if(tail == NULL) {
  33.             cout << "\\nStos pusty!!!!\\n" << endl << endl; // jesli tail NULL to stos pusty
  34.         }
  35.         else{
  36.             type a = tail->value;
  37.             struct Element *tmp = tail->prev;
  38.             tail=NULL;
  39.             delete tail;
  40.             tail = tmp;
  41.             return a;
  42.         }
  43.     };
  44.  
  45.     void wypisz(){
  46.         if(tail==NULL){
  47.             cout << "\\nStos pusty!!!!\\n" << endl;
  48.  
  49.         }
  50.         else{
  51.             struct Element *tmp;
  52.             tmp = tail;
  53.             cout << "\\n---------- Stos: ----------\\n";
  54.             while(tail != NULL){
  55.                 cout << tail->value << endl;
  56.                 tail = tail -> prev;
  57.             }
  58.             tail = tmp;
  59.         }
  60.     };
  61.  
  62.     ~Stack(){
  63.  
  64.         while(tail!=NULL){
  65.             this->pop();
  66.         }
  67.         if(tail==NULL) cout<<"\\nUsunieto Stos\\n";
  68.     }
  69. };
  70.  
  71.  
  72.  
  73. int main(){
  74.     Stack<int> stack ;
  75.  
  76.     stack.push(10);
  77.     stack.push(22);
  78.     stack.push(36);
  79.     stack.push(172);
  80.  
  81.     stack.wypisz();
  82.     stack.pop();
  83.     stack.wypisz();
  84.     stack.pop();
  85.     stack.wypisz();
  86.  
  87.  
  88.     return 0;
  89. }
');