Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <climits>
- using namespace std;
- template <class Item>
- class Queue
- {
- struct Element
- {
- Item inf;
- Element *next;
- Element (Item x):inf(x),next(0)
- {
- }
- };
- Element *head, *tail;
- public:
- Queue():head(0),tail(0)
- {
- }
- int Get()
- {
- Element *t = head;
- int i = t->inf;
- head = t->next;
- if (head == NULL)
- {
- tail=NULL;
- }
- delete t;
- return i;
- }
- void Push(int data)
- {
- Element *t = tail;
- tail = new Element(data);
- if (!head)
- {
- head=tail;
- }
- else
- {
- t->next = tail;
- }
- }
- };
- int main(void)
- {
- Queue <int> q;
- Queue <int> res;
- int i;
- int size = 0;
- ifstream in("input.txt");
- ofstream out("output.txt");
- while (in>>i)
- {
- q.Push(i);
- size++;
- }
- int min = INT_MAX;
- int max = INT_MIN;
- for (int i = size; i > 0; i--)
- {
- int item = q.Get();
- if (item > max) max = item;
- else if (item < min) min = item;
- res.Push(item);
- }
- for (int i = size; i > 0; i--)
- {
- int item = res.Get();
- if (item == min) q.Push(max);
- else if (item == max) q.Push(min);
- else q.Push(item);
- }
- while (size!=NULL)
- {
- out << q.Get() << " ";
- size--;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment