fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template <class TD>
  5. class STOS
  6. {
  7. struct element
  8. {
  9. TD dana;
  10. element* nast;
  11. element(TD x, element* w)
  12. {
  13. dana = x;
  14. nast = w;
  15. }
  16. };
  17.  
  18. element* wierzch;
  19.  
  20. public:
  21. STOS()
  22. {
  23. wierzch = NULL;
  24. }
  25.  
  26. bool Pusty() const
  27. {
  28. return wierzch == NULL;
  29. }
  30.  
  31. void DoStosu(TD d)
  32. {
  33. wierzch = new element(d, wierzch);
  34. }
  35.  
  36. TD ZeStosu();
  37. ~STOS();
  38. };
  39.  
  40. template <class TD>
  41. TD STOS<TD>::ZeStosu()
  42. {
  43. TD rob = wierzch->dana;
  44. element* t = wierzch->nast;
  45. delete wierzch;
  46. wierzch = t;
  47. return rob;
  48. }
  49.  
  50. template <class TD>
  51. STOS<TD>::~STOS()
  52. {
  53. while (wierzch != NULL)
  54. ZeStosu();
  55. }
  56.  
  57. int main()
  58. {
  59. STOS<int> s;
  60. s.DoStosu(5);
  61. s.DoStosu(-11);
  62. s.DoStosu(34);
  63. while (!s.Pusty())
  64. {
  65. cout << s.ZeStosu() << endl;
  66. }
  67.  
  68. return 0;
  69. }
Success #stdin #stdout 0s 3272KB
stdin
Standard input is empty
stdout
34
-11
5