How implement the removal of elements with even numbers from the created stack? Here is the code. I'm not well versed in this topic yet. I ask for help. I need to implement the removal of elements with even numbers from the created stack in function DelEven().I don't know how do it. Can someone help with this? Now, after the DelEven function is called, instead of a list, 0 and 1 remain
#include <iostream>
#include <stdlib.h>
using namespace std;
struct Stack {
int info;
Stack * next;
} *beg, *t;
Stack* InStack(Stack*, int);
void View(Stack*);
void Del_All(Stack **);
void Sort_p(Stack **);
void Sort_info(Stack *);
Stack* DelEven(Stack *);
int main(int argc, char* argv[]){
int i, in, n, kod;
while(true){
cout << "\n\tCreat - 1.\tAdd - 2.\tView - 3.\tDel - 4.\tSort1 - 5.\tSort2 - 6.\tInd_zad - 7.\tEXIT - 0. : " ;
cin >> kod;
switch(kod) {
case 1: case 2:
if(kod == 1 && beg != NULL){
cout << "Clear Memory!" << endl;
break;
}
cout << "Input kol = "; cin >> n;
for(i = 1; i <= n; i++) {
in = rand();
beg = InStack(beg, in); }
if (kod == 1) cout << "Create " << n << endl;
else cout << "Add " << n << endl;
break;
case 3: if(!beg){
cout << "Stack Pyst!" << endl;
break; }
cout << "--- Stack ---" << endl;
View(beg);
break;
case 4:
Del_All(&beg);
cout<<"Memory Free!"<<endl;
break;
case 5:
if(beg != NULL) Sort_p(&beg);
break;
case 6:
if(beg != NULL) Sort_info(beg);
break;
case 7:
if(beg != NULL) DelEven(beg);
break;
case 0:
if(beg != NULL)
Del_All(&beg); } }}
Stack* InStack(Stack *p, int in) {
Stack *t = new Stack;
t -> info = in;
t -> next = p;
return t;}
void View(Stack *p)
{
int c;
Stack *t = p;
while( t != NULL) {
c = t->info;
cout<<" "<<c<<endl;
t = t -> next; }}
void Del_All(Stack **p) {
while(*p != NULL) {
t = *p;
*p = (*p) -> next;
delete t; }}
void Sort_p(Stack **p) {
Stack *t = NULL, *t1, *r;
if ((*p) -> next -> next == NULL) return;
do {
for (t1=*p; t1-> next->next != t; t1=t1-> next)
if (t1->next->info > t1-> next-> next-> info){
r = t1->next->next;
t1 -> next -> next = r -> next;
r-> next =t1-> next;
t1-> next = r; }
t= t1-> next;
} while ((*p)-> next -> next != t);}
void Sort_info(Stack *p) {
Stack *t = NULL, *t1;
int r;
do {
for (t1=p; t1 -> next != t; t1 = t1-> next)
if (t1-> info > t1-> next -> info) {
r = t1-> info;
t1-> info = t1-> next -> info;
t1-> next -> info = r; }
t = t1; } while (p -> next != t);}
Stack* DelEven(Stack *p) {
Stack* t = p;
Stack* t1 = p;
while (t != NULL) {
if (t->info % 2 != 0) {
t1 = t;
t = t->next; }
else {
if (t == p) {
p = p->next;
delete t;
return p; }
else {
t1->next = t->next;
delete t;
t = t1->next; }} }}