Sephinroth

page_74_own_queue

May 21st, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include <fstream>
  2. using namespace std;
  3. ifstream in ("input.txt");
  4. ofstream out ("output.txt");
  5.  
  6. class Queue {
  7.     struct Element {
  8.         int inf;
  9.         Element *next;
  10.         Element(int x) : inf(x), next(0) {}
  11.     };
  12.     Element *head, *tail;
  13. public:
  14.     Queue() : head(0), tail(0) {}
  15.     bool empty() { return head == 0;}
  16.     int get() { if (empty()) return 0; else
  17.         {
  18.             Element *t = head;
  19.             int i = t->inf;
  20.             head = t->next;
  21.             if (head == NULL) tail = NULL;
  22.             delete t;
  23.             return i;}}
  24.     void pop() {
  25.         //if (empty()) return 0;
  26.         Element *t = head;
  27.         head = head->next;
  28.         if (head == NULL) tail = NULL;
  29.         delete t;}
  30.     int front() {
  31.         return head->inf;}
  32.     void push(int data){
  33.         Element *t = tail;
  34.         tail = new Element(data);
  35.         if (!head) head = tail; else t->next = tail;}
  36. };
  37.  
  38. int main (){
  39.     Queue Q;
  40.     int x, val;
  41.     in >> x;
  42.     while (in >> val)
  43.         Q.push(val);
  44.     in.close();
  45.     while (!Q.empty()){
  46.         if (Q.front() == x) { Q.pop(); continue;} else
  47.             out << Q.front() << '\t';
  48.         Q.pop();}  
  49.     out.close();       
  50.     return 0;  
  51. }
Add Comment
Please, Sign In to add comment