Advertisement
Guest User

Untitled

a guest
Mar 5th, 2015
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <math.h>
  4.  
  5. using namespace std;
  6. struct TNode
  7. {
  8.     int data;
  9.     TNode* prev=NULL;
  10. };
  11.  
  12. int pop(TNode *&head)
  13. {
  14.     int ret = head->data;
  15.     TNode *p = head;
  16.     head = head->prev;
  17.     delete(p);
  18.     return ret;
  19. }
  20. void push (int data, TNode*&head)
  21. {
  22.     TNode *p = new TNode;
  23.     p->data = data;
  24.     p->prev = head;
  25.     head = p;
  26. }
  27.  
  28. int main()
  29. {
  30.     TNode* mainstack = NULL, *odd = NULL, *even = NULL;
  31.     ifstream in("in.txt");
  32.     ofstream out("out.txt");
  33.     int tmp;
  34.     while(in.good())
  35.     {
  36.         in>>tmp;
  37.         push(tmp, mainstack);
  38.     }
  39.     pop(mainstack);
  40.     while(mainstack)
  41.     {
  42.  
  43.         if(mainstack->data%2==0) push(mainstack->data,even);
  44.         else push(mainstack->data,odd);
  45.         mainstack = mainstack->prev;
  46.     }
  47.     while(even)
  48.     {
  49.         out<<pop(even)<<" ";
  50.     }
  51.     out<<endl;
  52.     while(odd)
  53.     {
  54.         out<<pop(odd)<<" ";
  55.     }
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement