Guest User

Untitled

a guest
Jul 17th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. // zad4.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include<iostream>
  6. #include<cstdio>
  7. #include<cstdlib>
  8. using namespace std;
  9. class Elem {
  10.  
  11. public:
  12. int l;
  13. Elem *nast;
  14. };
  15.  
  16. class Stos {
  17. public:
  18.  
  19. Elem *ostatni;
  20. //void init() { ostatni=NULL; }
  21. Stos ()
  22. {
  23.     ostatni=NULL;
  24.     cout<<"Wywolano konstruktor: "<<this<<endl;
  25. }
  26.  
  27. void pop()
  28. {
  29. Elem* tmp=ostatni;
  30. ostatni=ostatni->nast;
  31. delete tmp;
  32. }
  33. void push (int l)
  34. {
  35. Elem* nowy=new Elem;
  36. nowy->nast=ostatni;
  37. nowy->l=l;
  38. ostatni=nowy;
  39. }
  40. int top() { return ostatni->l; }
  41. int full() {return 0; }
  42. int empty() {return ostatni==NULL; }
  43. /*void destroy() {
  44. while (! this->empty()) this->pop();
  45. }*/
  46.  
  47. ~Stos()
  48. {
  49.     while (! this->empty()) this->pop();
  50.     cout<<"Wywolano destruktor: "<<this<<endl;
  51. }
  52.  
  53. };
  54.  
  55. void Wypisz(Stos& s)
  56. {
  57. Stos kopia;
  58. while (!s.empty())
  59. {
  60.  cout << " " << s.top();
  61.  kopia.push(s.top());
  62.  s.pop();
  63. }
  64.  cout<<endl;
  65. while (!kopia.empty())
  66. {
  67.  s.push(kopia.top());
  68.  kopia.pop();
  69. }
  70.  
  71. }
  72.  
  73.  
  74. int _tmain(int argc, _TCHAR* argv[])
  75. {
  76. //automatycznie:
  77. {
  78.  Stos s1;
  79.  s1.push(0);
  80.  s1.push(1);
  81.  s1.push(2);
  82.  s1.push(3);
  83.  s1.push(4);
  84.  s1.push(5);
  85.  s1.push(6);
  86.  s1.push(7);
  87.  s1.push(8);
  88.  s1.push(9);
  89.  Wypisz(s1);
  90. }
  91.  
  92.  
  93. //dynamicznie:
  94. Stos *s2=NULL;
  95. s2 = new Stos;
  96. delete s2;
  97.  
  98.  
  99.  
  100. system("pause");
  101. return 0;
  102. }
Add Comment
Please, Sign In to add comment