Home > Enterprise >  How implement the removal of elements with even numbers from the created stack?
How implement the removal of elements with even numbers from the created stack?

Time:04-13

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;           }}  }}

CodePudding user response:

You could pop all elements from the original stack, one by one, and add the ones you want to keep to a new (temporary) stack.

This new stack will be reversed, so you again need to pop all elements one by one from the new stack, and push them to the original stack.


While the above solution is the "proper" and generic way to handles such an issue with generic stacks, since you have control of the implementation you could treat it as a plain simple (single) linked list.

So you can make a shortcut by simply iterating over the list, removing elements as needed.

You need to handle the special case of the head of the list (the top of the stack) being a node you need to remove. Other than that you need to keep track of the previous node, as you need to change its next pointer.

CodePudding user response:

This function declaration

void  Del_even(Stack *p){

}

is incorrect. The pointer to the top node of the stack is passed to the function by value. However it can be changed within the function if it points to a node with an even value. As the function accepts the pointer by value then it means that it deals with a copy of the value of the original pointer and changing the copy within the function does not reflect on the value of the original pointer.

As your program is written in the C style then to pass the pointer by reference in the C meaning means passing the pointer indirectly through a pointer to it.

That is the function should have the same declaration as the function Del_All:

void Del_All(Stack **);

that is like

void  Del_even(Stack **);

The function can be implemented for example as a recursive function.

void  Del_even(Stack **p)
{
    if ( *p )
    {
        Del_even( &( *p )->next );

        if ( ( *p )->info % 2 == 0 )
        {
            Stack *current = *p;
            *p = ( *p )->next;
            delete current;
        }
    }
}     

And the function is called like

Del_even( &beg );

A non-recursive function can be defined the following way

void  Del_even(Stack **p)
{
    while ( *p )
    {
        if ( ( *p )->info % 2 == 0 )
        {
            Stack *current = *p;
            *p = ( *p )->next;
            delete current;
        }
        else
        {
            p = &( *p )->next;
        }
    }
}     

and called the same way as the recursive function that is

Del_even( &beg );
  • Related