Advertisement
Val_Kir

е6ал я все это летом делать

Aug 8th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.49 KB | None | 0 0
  1. /* Создать стек с целыми числами
  2.     и разбить его на два с четными
  3.     и нечетными числами, нулевые
  4.     числа удалить.
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <time.h>
  10. #include <iostream>
  11. #include <conio.h>
  12.  
  13. struct Node {int k; Node *next;};
  14.  
  15. Node* Push(Node *stack, char k)
  16. {
  17.     Node *head=stack, *nexthead=new Node;
  18.     nexthead->k=k;
  19.     nexthead->next=head;
  20.     return nexthead;
  21. }
  22.  
  23. Node *Pop(Node *p)
  24. {
  25.     if(!p) return 0;
  26.     Node *t=p->next;
  27.     delete p;
  28.     return t;
  29. }
  30.  
  31. int Top(Node *p)
  32. {
  33.     if(p) return p->k;
  34.     puts("\n the stack is empty");
  35.     getchar();
  36.     exit(1);
  37. }
  38.  
  39. bool Empty(Node *p)
  40. {
  41.     if(p) return false;
  42.     return true;
  43. }
  44.  
  45. void Print(Node *p)
  46. {
  47.     int z=0;
  48.     while(!Empty(p))
  49.     {
  50.         z=Top(p);
  51.         printf("%d  ",z);
  52.         p=Pop(p);
  53.     }
  54. }
  55.  
  56.  
  57. void main()
  58. {
  59.     int n=10, i, a[10], z=0;
  60.     Node *beg=NULL;
  61.     Node *beg1=NULL;
  62.     Node *beg2=NULL;
  63.  
  64.  
  65.     srand(time(0));
  66.  
  67.     //заполнение стека числами
  68.     for(i=0; i<n; ++i)
  69.     {
  70.         a[i]=rand()%10;
  71.         printf("%d ",a[i]);
  72.         beg=Push(beg,a[i]);
  73.     }
  74.     putchar('\n');
  75.  
  76.     //деление
  77.     while(!Empty(beg))
  78.     {
  79.         z=Top(beg);
  80.         switch (z)
  81.         {
  82.         case 0:
  83.             beg=Pop(beg);
  84.             break;
  85.         default:
  86.             if(z%2==0)
  87.             {
  88.                 beg1=Push(beg1,z);
  89.                 beg=Pop(beg);
  90.             }
  91.             else
  92.             {
  93.                 beg2=Push(beg2,z);
  94.                 beg=Pop(beg);
  95.             }
  96.             break;
  97.         }
  98.     }
  99.  
  100.     puts("\n Conclusion: ");
  101.     Print(beg1);
  102.     putchar('\n');
  103.     Print(beg2);
  104.    
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement