Advertisement
kokokozhina

dynamic_2

Mar 22nd, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <stdio.h>
  4.  
  5. using namespace std;
  6.  
  7. struct stack{
  8.     int inf;
  9.     stack* next;
  10. };
  11.  
  12. stack *init(){
  13.     return NULL;
  14. }
  15.  
  16. void push(stack *&h, int x){
  17.     stack *top = new stack();
  18.     top->inf = x;
  19.     top->next = h;
  20.     h = top;
  21. }
  22.  
  23. int top(stack *&h){
  24.     return h->inf;
  25. }
  26.  
  27. int pop(stack *&h){
  28.     int i = h->inf;
  29.     stack *cur = h;
  30.     h=h->next;
  31.     delete cur;
  32.     return i;
  33. }
  34.  
  35. int main()
  36. {
  37. #ifdef _DEBUG
  38.     freopen("input.txt", "r", stdin);
  39.     freopen("output.txt", "w", stdout);
  40. #endif
  41.  
  42.     int n;
  43.     scanf("%d", &n);
  44.  
  45.     stack *a = init();
  46.     stack *odd = init();
  47.     stack *even = init();
  48.  
  49.     for(int i = 0; i < n; i++){
  50.         int cur; scanf("%d", &cur);
  51.         if(cur % 2) push(odd, cur);
  52.         else push(even, cur);
  53.     }
  54.     while(odd)
  55.         push(a, pop(odd));
  56.  
  57.     while(even)
  58.         push(a, pop(even));
  59.    
  60.     while(a)
  61.         printf("%d ", pop(a));
  62.  
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement