Advertisement
Guest User

Untitled

a guest
May 27th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. #include <fstream>
  2. #include <cstdlib>
  3. #include <iostream>
  4. using namespace std;
  5. ifstream in("in2.txt");
  6. ofstream out("out1.txt");
  7. template <class Item> //класс-шаблон очередь
  8. class Stack
  9. {
  10. struct Element
  11. {
  12. Item inf;
  13. Element* next;
  14. Element(Item x,Element *p) :inf(x), next(p)
  15. {
  16. }
  17. };
  18.  
  19. Element* head;
  20.  
  21. public:
  22. Stack():head(0)
  23. {
  24. }
  25.  
  26. bool Empty()
  27. {
  28. return head == 0;
  29. }
  30.  
  31. Item Pop()
  32. {
  33. if (Empty())
  34. {
  35. cout <<"Error"<< std::endl;
  36. }
  37. else {
  38.  
  39. Element* r = head;
  40. Item i = r->inf;
  41. head = r->next;
  42. delete r;
  43. return i;
  44. }
  45. }
  46.  
  47. void Push(Item data) {
  48. head = new Element(data, head);
  49. }
  50. int Top()
  51. {
  52. if (Empty()) {
  53. return 0;
  54. }
  55. else{
  56. return head->inf; //иначе новый узел помещаем в конец очереди
  57. }
  58. }
  59. };
  60.  
  61. int main()
  62. {
  63. Stack <int> I;
  64. Stack<int>J;
  65. int i,x,c;
  66. if(!in) out<<"ERR";
  67. while(in>>i){
  68.  
  69. I.Push(i);
  70. cout<<i;
  71. }
  72. in.close();
  73. x=INT_MAX;
  74. while(!I.Empty()){
  75. c=I.Pop();
  76. if (c<x)x=c;
  77. cout<<c;
  78. J.Push(c);}
  79. while(!J.Empty()){
  80. c=J.Pop();
  81. cout<<c;
  82. if(c!=x)I.Push(c);}
  83. while(!I.Empty()){
  84. out<<I.Pop();}
  85. out.close();
  86. return 0;}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement