mituri

[C++](p) klasa stos

Jan 12th, 2016
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. // zad9hawryl2.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. /*
  5. Opracuj klasę do budowy stosu w oparciu o listę dowiązaniową. Nowe elementy są wstawiane na początku listy.
  6. Wykorzystaj następujące klasy:
  7. struct Element {
  8. int dane;
  9. Element * nast;
  10. // konstruktor
  11. Element(int n, Element* p) : dane(n), nast(p)
  12. {}
  13. };
  14.  
  15. class Stos {
  16. Element* szczyt; // wskaźnik na element na szczycie stosu
  17. int lb_elem; // rozmiar stosu
  18. public:
  19. Stos (); // konstruktor domyślny
  20. ~Stos(); // destruktor
  21. Stos(Stos & s); // konstruktor kopiujący
  22. Stos& operator=(Stos const &s); // przypisanie
  23. void push(int n); // wstawianie na stos
  24. void pop(); // zdejmowanie ze stosu
  25. int top() const; // udostepnienie liczby na szczycie stosu
  26. int & top(); // udostepnienie referencji do liczby
  27. // na szczycie stosu
  28. int size() const; // bieżący rozmiar stosu
  29. bool empty() const; // czy stos jest pusty
  30. }
  31. */
  32.  
  33. #include "stdafx.h"
  34.  
  35. #include <iostream>
  36.  
  37. using namespace std;
  38.  
  39. // Element( int dane, Element* nast)
  40. struct Element {
  41. int dane;
  42. Element * nast;
  43.  
  44. // konstruktor
  45. Element(int n, Element* p) : dane(n), nast(p) {}
  46. };
  47.  
  48. //Stos(Element* szczyt, int lb_elem);
  49. class Stos {
  50. Element* szczyt; // wskaźnik na element na szczycie stosu
  51. int lb_elem; // rozmiar stosu
  52. public:
  53.  
  54. // konstruktor domyślny
  55. Stos (){
  56. szczyt = NULL;
  57. lb_elem = 0;
  58. }
  59.  
  60. // destruktor
  61. ~Stos(){
  62. Element* nast;
  63. Element* tmp = szczyt;
  64. while(tmp != NULL){
  65. nast = tmp->nast;
  66. delete tmp;
  67. tmp = nast;
  68. }
  69. };
  70.  
  71. // konstruktor kopiujący
  72. Stos(Stos & s){
  73. if(s.szczyt != NULL){
  74.  
  75. Element* nast;
  76. Element* tmp = s.szczyt;
  77.  
  78. szczyt = s.szczyt;
  79.  
  80. while(tmp != NULL){
  81. nast = tmp->nast;
  82. tmp = nast;
  83. }
  84.  
  85. lb_elem = s.lb_elem;
  86. }
  87. }
  88.  
  89. // przypisanie
  90. Stos& operator=(Stos const &s){
  91. if(s.szczyt != NULL){
  92.  
  93. Element* nast;
  94. Element* tmp = s.szczyt;
  95.  
  96. szczyt = s.szczyt;
  97.  
  98. while(tmp != NULL){
  99. nast = tmp->nast;
  100. tmp = nast;
  101. }
  102.  
  103. lb_elem = s.lb_elem;
  104. }
  105. return *this;
  106. }
  107.  
  108. // wstawianie na stos
  109. void push(int n){
  110.  
  111. Element* nowyElem = new Element(n,szczyt);
  112. szczyt = nowyElem;
  113.  
  114. lb_elem++;
  115. }
  116.  
  117. // zdejmowanie ze stosu
  118. void pop(){
  119. lb_elem--;
  120. szczyt = szczyt->nast;
  121. }
  122.  
  123. // udostepnienie liczby na szczycie stosu
  124. int top() const{
  125. return szczyt->dane;
  126. }
  127.  
  128. // udostepnienie referencji do liczby na szczycie stosu
  129. int& top(){
  130. int* tmp = new int;
  131. *tmp = (szczyt->dane);
  132. return *tmp;
  133. }
  134.  
  135. // bieżący rozmiar stosu
  136. int size() const{
  137. return lb_elem;
  138. }
  139.  
  140. // czy stos jest pusty
  141. bool empty() const{
  142. if(lb_elem == 0 ) return true;
  143. else return false;
  144. }
  145.  
  146. friend ostream& operator<<(ostream& os, Stos& stos){
  147. Element* nast = stos.szczyt;
  148. Element* tmp = stos.szczyt;
  149. while(tmp != NULL){
  150. os << nast->dane << " ";
  151. nast = tmp->nast;
  152. tmp = nast;
  153. }
  154. os << "Rozmiar: " << stos.lb_elem << endl;
  155. return os;
  156. }
  157. };
  158.  
  159.  
  160. int _tmain(int argc, _TCHAR* argv[]){
  161.  
  162. Stos s1;
  163. s1.push(2);
  164. s1.push(4);
  165. s1.push(6);
  166.  
  167. cout << s1 << endl;
  168.  
  169. Stos s2 = s1;
  170. cout << s2 << endl;
  171.  
  172. s2.pop();
  173. cout << s2 << endl;
  174.  
  175. int* a = &s2.top();
  176. cout << a << endl;
  177.  
  178. getchar();
  179. return 0;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment