Advertisement
TvoiNikto

Untitled

Jun 17th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. #include <cstring>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. template<class T> class node {
  7. public:
  8. T* data;
  9. node<T>* tail;
  10. node() {
  11. tail = NULL;
  12. }
  13. };
  14.  
  15.  
  16. template<class T> class stack {
  17. private:
  18. node<T>* header;
  19.  
  20. public:
  21. stack() {
  22. header = NULL;
  23. }
  24.  
  25. void push(T* obj) {
  26. node<T>* n = new node<T>();
  27. n->tail = header;
  28. n->data = obj;
  29. header = n;
  30. }
  31.  
  32.  
  33. /*Начало вашего кода*/
  34. //TODO: Пишите код тут
  35. void pop() {
  36. while (header != nullptr) {
  37. cout << header->data << endl;
  38. header = header->tail;
  39. }
  40. delete header;
  41. }
  42. /*Конец вашего кода*/
  43. /*Памятка! Если вы стёрли строки доступные для редактирования - нажмите F5*/
  44.  
  45.  
  46. bool empty() {
  47. return header == NULL;
  48. }
  49. };
  50.  
  51.  
  52. class product{
  53. public:
  54. char name[32];
  55. float price;
  56.  
  57. product() {
  58.  
  59. }
  60. product(char* n, float p) {
  61. strcpy(name, n);
  62. price = p;
  63. }
  64. };
  65.  
  66.  
  67. int main()
  68. {
  69. product* p1 = new product("Milk", 55.0f);
  70. product* p2 = new product("Bread", 10.5f);
  71. product* p3 = new product("Cheese", 300.0f);
  72.  
  73. stack<product> s;
  74. s.push(p1);
  75. s.push(p2);
  76. s.push(p3);
  77.  
  78. while (!s.empty())
  79. {
  80. product tmp = s.pop();
  81. cout << tmp.name << endl;
  82. }
  83.  
  84. return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement