Seal_of_approval

p75e11

Jun 8th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <climits>
  4. using namespace std;
  5.  
  6. template <class Item>
  7. class Queue
  8. {
  9.     struct Element
  10.     {
  11.         Item inf;
  12.         Element *next;
  13.         Element (Item x):inf(x),next(0)
  14.         {
  15.         }
  16.     };
  17.     Element *head, *tail;
  18.  
  19. public:
  20.     Queue():head(0),tail(0)
  21.     {
  22.     }
  23.  
  24.    
  25.     int Get()
  26.     {
  27.         Element *t = head;
  28.         int i = t->inf;
  29.         head = t->next;
  30.         if (head == NULL)
  31.         {
  32.             tail=NULL;
  33.         }
  34.         delete t;
  35.         return i;
  36.     }
  37.  
  38.     void Push(int data)
  39.     {
  40.         Element *t = tail;
  41.         tail = new Element(data);
  42.         if (!head)
  43.         {
  44.             head=tail;
  45.         }
  46.         else
  47.         {
  48.             t->next = tail;
  49.         }
  50.     }
  51. };
  52.  
  53. int main(void)
  54. {
  55.     Queue <int> q;
  56.     Queue <int> res;
  57.     int i;
  58.     int size = 0;
  59.     ifstream in("input.txt");
  60.     ofstream out("output.txt");
  61.  
  62.     while (in>>i)
  63.     {
  64.         q.Push(i);
  65.         size++;
  66.     }
  67.  
  68.     int min = INT_MAX;
  69.     int max = INT_MIN;
  70.  
  71.     for (int i = size; i > 0; i--)
  72.     {
  73.         int item = q.Get();
  74.         if (item > max) max = item;
  75.         else if (item < min) min = item;
  76.         res.Push(item);
  77.     }
  78.    
  79.     for (int i = size; i > 0; i--)
  80.     {
  81.         int item = res.Get();
  82.         if (item == min) q.Push(max);
  83.         else if (item == max) q.Push(min);
  84.         else q.Push(item);
  85.     }
  86.  
  87.     while (size!=NULL)
  88.     {
  89.         out << q.Get() << " ";
  90.         size--;
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment