Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Создать стек с целыми числами
- и разбить его на два с четными
- и нечетными числами, нулевые
- числа удалить.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <iostream>
- #include <conio.h>
- struct Node {int k; Node *next;};
- Node* Push(Node *stack, char k)
- {
- Node *head=stack, *nexthead=new Node;
- nexthead->k=k;
- nexthead->next=head;
- return nexthead;
- }
- Node *Pop(Node *p)
- {
- if(!p) return 0;
- Node *t=p->next;
- delete p;
- return t;
- }
- int Top(Node *p)
- {
- if(p) return p->k;
- puts("\n the stack is empty");
- getchar();
- exit(1);
- }
- bool Empty(Node *p)
- {
- if(p) return false;
- return true;
- }
- void Print(Node *p)
- {
- int z=0;
- while(!Empty(p))
- {
- z=Top(p);
- printf("%d ",z);
- p=Pop(p);
- }
- }
- void main()
- {
- int n=10, i, a[10], z=0;
- Node *beg=NULL;
- Node *beg1=NULL;
- Node *beg2=NULL;
- srand(time(0));
- //заполнение стека числами
- for(i=0; i<n; ++i)
- {
- a[i]=rand()%10;
- printf("%d ",a[i]);
- beg=Push(beg,a[i]);
- }
- putchar('\n');
- //деление
- while(!Empty(beg))
- {
- z=Top(beg);
- switch (z)
- {
- case 0:
- beg=Pop(beg);
- break;
- default:
- if(z%2==0)
- {
- beg1=Push(beg1,z);
- beg=Pop(beg);
- }
- else
- {
- beg2=Push(beg2,z);
- beg=Pop(beg);
- }
- break;
- }
- }
- puts("\n Conclusion: ");
- Print(beg1);
- putchar('\n');
- Print(beg2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement