fork download
  1. #include<iostream>
  2. #include<stdexcept>
  3.  
  4. using namespace std;
  5.  
  6. template <class T>
  7. class QUEUE
  8. {
  9. struct node
  10. {
  11. T data;
  12. node *next;
  13. node(const T &x): data(x), next(NULL)
  14. {
  15. }
  16. };
  17.  
  18. typedef node * pnode;
  19. pnode head,tail;
  20. public:
  21. QUEUE();
  22. bool empty()const // czy to jest też dobrze napisane ??
  23. {
  24. return head == NULL;
  25. }
  26.  
  27. void put(T);
  28. T get();
  29. void wypisz()const;
  30. ~QUEUE();
  31. };
  32.  
  33. template<class T>
  34.  
  35. QUEUE<T>::QUEUE()
  36. {
  37. head = NULL;
  38. }
  39.  
  40. template <class T>
  41. void QUEUE<T>::put (T x)
  42. {
  43. if (empty())
  44. head = tail = new node(x);
  45. else
  46. {
  47. pnode pom = tail;
  48. tail = new node(x);
  49. pom ->next =tail;
  50. }
  51. }
  52.  
  53. template<class T>
  54. T QUEUE<T>::get()
  55. {
  56. if (empty())
  57. throw out_of_range ("Kolejka pusta");
  58. T temp = head -> data;
  59. pnode stara = head;
  60. head = head -> next;
  61. delete stara;
  62. return temp;
  63. }
  64.  
  65. template <class T>
  66. void QUEUE<T>::wypisz()const
  67. { pnode pomoc=head;
  68. while(pomoc != NULL)
  69. {
  70. cout<<pomoc->data<<endl;
  71. pomoc=pomoc->next;
  72.  
  73. }
  74. }
  75.  
  76.  
  77. template <class T> // Czy destruktor jest dobrze napisany ??
  78. QUEUE<T>::~QUEUE()
  79. {
  80. while(head != NULL)
  81. get();
  82. }
  83.  
  84.  
  85. int main()
  86. {
  87. QUEUE<int> kolejka;
  88. cout<<endl;
  89. cout<<"Czy kolejka pusta: "<<kolejka.empty()<<endl;
  90. kolejka.put(5);
  91. cout<<"Czy kolejka pusta: "<<kolejka.empty()<<endl;
  92. kolejka.put(10);
  93. kolejka.put(15);
  94. kolejka.put(11);
  95. kolejka.put(4);
  96. kolejka.put(3);
  97. kolejka.put(2);
  98. kolejka.put(1);
  99. kolejka.put(7);
  100. kolejka.put(8);
  101. kolejka.put(21);
  102. cout<<endl;
  103. kolejka.wypisz();
  104. cout<<endl;
  105. return 0;
  106. }
  107.  
  108.  
Success #stdin #stdout 0s 3232KB
stdin
Standard input is empty
stdout
Czy kolejka pusta: 1
Czy kolejka pusta: 0

5
10
15
11
4
3
2
1
7
8
21