Advertisement
makuartur

Вывод Стека

Jun 17th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 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. T *pop(){
  36. T*r=new T();
  37. r = &header->data;
  38.  
  39. header = header->tail;
  40.  
  41. return r;
  42. }
  43. /*Конец вашего кода*/
  44. /*Памятка! Если вы стёрли строки доступные для редактирования - нажмите F5*/
  45.  
  46.  
  47. bool empty() {
  48. return header == NULL;
  49. }
  50. };
  51.  
  52.  
  53. class product{
  54. public:
  55. char name[32];
  56. float price;
  57.  
  58. product() {
  59.  
  60. }
  61.  
  62. product(char* n, float p) {
  63. strcpy(name, n);
  64. price = p;
  65. }
  66. };
  67.  
  68.  
  69. int main()
  70. {
  71. product* p1 = new product("Milk", 55.0f);
  72. product* p2 = new product("Bread", 10.5f);
  73. product* p3 = new product("Cheese", 300.0f);
  74.  
  75. stack<product> s;
  76. s.push(p1);
  77. s.push(p2);
  78. s.push(p3);
  79.  
  80. while (!s.empty())
  81. {
  82. product* tmp = s.pop();
  83. cout << tmp->name << endl;
  84. }
  85.  
  86. return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement