Stos template...

dobra, tutaj jest kolejna, tym razem pełniejsza implementacja stosu... z użyciem szablonu oczywiście szczerze mówiąc, ta wersja nie przypadła mi do gustu, ale kto co woli
klik:


Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4.  
  5. template<class T, int N>
  6. class stos
  7. {
  8. private:
  9. T *t;
  10. int Size;
  11. public:
  12. stos();
  13. ~stos();
  14. bool empty();
  15. bool full();
  16. void push(T&);
  17. void pop();
  18. T& top();
  19. int size();
  20. };
  21.  
  22. template<class T, int N>
  23. stos<T,N>::stos()
  24. {
  25. t=new T[N];
  26. Size=0;
  27. };
  28.  
  29. template<class T, int N>
  30. stos<T,N>::~stos()
  31. {
  32. delete[] t;
  33. };
  34.  
  35. template<class T, int N>
  36. bool stos<T,N>::empty()
  37. {
  38. return Size==0;
  39. }
  40.  
  41. template<class T, int N>
  42. bool stos<T,N>::full()
  43. {
  44. return Size==N;
  45. }
  46.  
  47. template<class T, int N>
  48. void stos<T,N>::push(T& x)
  49. {
  50. t[Size]=x;
  51. Size+=1;
  52. }
  53.  
  54. template<class T, int N>
  55. void stos<T,N>::pop()
  56. {
  57. if (empty()) cout<<"Stos jest pusty"<<endl;
  58. else Size+=-1;
  59. }
  60.  
  61. template<class T, int N>
  62. T& stos<T,N>::top()
  63. {
  64. return t[Size-1];
  65. }
  66.  
  67. template<class T, int N>
  68. int stos<T,N>::size()
  69. {
  70. return Size;
  71. }
  72.  
  73.  
  74. int menu()
  75. {
  76. cout<<"\n......:::::MENU:::::......\n"<<endl;
  77. cout<<"1. Sprawdz czy stos jest pusty."<<endl;
  78. cout<<"2. Sprawdz rozmiar stosu."<<endl;
  79. cout<<"3. Najwyzszy element stosu"<<endl;
  80. cout<<"4. Dodaj element do stosu"<<endl;
  81. cout<<"5. Usun element ze stosu"<<endl;
  82. cout<<"6. Zakoncz program."<<endl;
  83. int a;
  84. cin>>a;
  85. return a;
  86. }
  87.  
  88. int main()
  89. {
  90. bool run = true;
  91. stos<string,3> s; //tu akurat można kłaść stringi, ale
  92.  
  93. while(run)
  94. {
  95. switch(menu())
  96. {
  97. case 1:
  98. {
  99. if(s.empty())
  100. {
  101. cout<<"stos jest pusty."<<endl;
  102. }
  103. else
  104. {
  105. cout<<"stos nie jest pusty."<<endl;
  106. }
  107. break;
  108. }
  109. case 2:
  110. {
  111. cout<<"Rozmaiar stosu: "<<s.size()<<endl;
  112. break;
  113. }
  114. case 3:
  115. {
  116.  
  117. if(s.empty())
  118. {
  119. cout<<"stos jest pusty."<<endl;
  120. }
  121. else
  122. {
  123. cout<<"Najwyzszy element na stosie: "<<s.top()<<endl;
  124. }
  125. break;
  126. }
  127. case 4:
  128. {
  129.  
  130. string i;
  131. cout<<"Podaj liczbe jaka chcesz umiescic na stosie:"<<endl;
  132. cin>>i;
  133. s.push(i);
  134. break;
  135. }
  136. case 5:
  137. {
  138. if(!s.empty())
  139. {
  140. s.pop();
  141. }
  142. else
  143. {
  144. cout<<"Nie mozna usunac elementu ze stosu poniewaz stos jest pusty."<<endl;
  145. }
  146. break;
  147. }
  148. case 6:
  149. {
  150. run = false;
  151. break;
  152. }
  153. };
  154. }
  155.  
  156. return 0;
  157. }

Komentarze

Popularne posty z tego bloga

[c++] Lista wskaznikowa

[c++] słowniki - haszowanie - haszowanie otwarte

[ANSI C][LINUX] Wysyłanie i obsługa sygnałów