How to remove negative elements from a row?

0

Good afternoon guys,

I am trying to remove a negative element from a queue, we can put the elements in the queue but when removing the element it always removes the first element ...

This is the Teacher's Exercise: Given a row of integers, write a program that excludes all numbers negatives without changing the position of the other elements of the queue.

Here's the code we've implemented: Insert element in Queue:

void insere(node *FILA)
{
    node *novo=aloca();
    novo->prox = NULL;

    if(vazia(FILA))
        FILA->prox=novo;
    else{
        node *tmp = FILA->prox;

        while(tmp->prox != NULL)
            tmp = tmp->prox;

        tmp->prox = novo;
    }
    tam++;
}

Remove negative element from stack (The one that is in trouble):

void excluiNegativos(node *FILA)
{
    if(vazia(FILA)){
        printf("Fila vazia!\n\n");
        return ;
    }

    node *tmp;
    tmp = FILA->prox;
    while( tmp != NULL){
        if(tmp->num<0){
            tmp->num = "";
        }
        tmp = tmp->prox;
    }
    printf("\n\n");
}

Inserting elements into the queue is perfect, however deleting elements from the queue is bugging ...

Thank you in advance!

Complete code, compile using the option to insert and then use the option to remove negative integers.

#include <stdio.h>
#include <stdlib.h>

struct Node{
    int num;
    struct Node *prox;
};
typedef struct Node node;

int tam;

int menu(void);
void opcao(node *FILA, int op);
void inicia(node *FILA);
int vazia(node *FILA);
node *aloca();
void insere(node *FILA);
node *retira(node *FILA);
void exibe(node *FILA);
void libera(node *FILA);
void excluiNegativos(node *FILA);
void somaFilas(node *FILA, node *FILA2);


int main(void)
{
    node *FILA = (node *) malloc(sizeof(node));
    if(!FILA){
        printf("Sem memoria disponivel!\n");
        exit(1);
    }else{
    inicia(FILA);
    int opt;

    do{
        opt=menu();
        opcao(FILA,opt);
    }while(opt);

    free(FILA);
    return 0;
    }
}


int menu(void)
{
    int opt;

    printf("Escolha a opcao\n");
    printf("0. Sair\n");
    printf("1. Zerar fila\n");
    printf("2. Exibir fila\n");
    printf("3. Adicionar Elemento na Fila\n");
    printf("4. Retirar Elemento da Fila\n");
    printf("5. Retirar Elemento Negativos da Fila\n");
    printf("6. Somar 2 filas ordenadas\n");
    printf("Opcao: "); scanf("%d", &opt);

    return opt;
}

void opcao(node *FILA, int op)
{
    node *tmp;
    switch(op){
        case 0:
            libera(FILA);
            break;

        case 1:
            libera(FILA);
            inicia(FILA);
            break;

        case 2:
            exibe(FILA);
            break;

        case 3:
            insere(FILA);
            break;

        case 4:
            tmp= retira(FILA);
            if(tmp != NULL){
                printf("Retirado: %3d\n\n", tmp->num);
                libera(tmp);
            }
            break;
        case 5:
            tmp= retira(FILA);
            if(tmp != NULL){
                excluiNegativos(tmp);
            }
            break;
        case 6:
            printf("Digite os elementos da fila. Digite 0 para sair \n");
            int aux;
            node *tmp1;
            node *tmp2;
            inicia(tmp1);
            scanf("%d", &aux);
            while(aux!=0){
                insere(tmp1);
            }
            inicia(tmp2);
            scanf("%d", &aux);
            while(aux!=0){
                insere(tmp2);
            }
            somaFilas(tmp1, tmp2);
            break;  

        default:
            printf("Comando invalido\n\n");
    }
}

void inicia(node *FILA)
{
    FILA->prox = NULL;
    tam=0;
}

int vazia(node *FILA)
{
    if(FILA->prox == NULL)
        return 1;
    else
        return 0;
}

node *aloca()
{
    node *novo=(node *) malloc(sizeof(node));
    if(!novo){
        printf("Sem memoria disponivel!\n");
        exit(1);
    }else{
        printf("Novo elemento: "); scanf("%d", &novo->num);
        return novo;
    }
}

void insere(node *FILA)
{
    node *novo=aloca();
    novo->prox = NULL;

    if(vazia(FILA))
        FILA->prox=novo;
    else{
        node *tmp = FILA->prox;

        while(tmp->prox != NULL)
            tmp = tmp->prox;

        tmp->prox = novo;
    }
    tam++;
}


node *retira(node *FILA)
{
    if(FILA->prox == NULL){
        printf("Fila ja esta vazia\n");
        return NULL;
    }else{
        node *tmp = FILA->prox;
        FILA->prox = tmp->prox;
        tam--;
        return tmp;
    }

}


void exibe(node *FILA)
{
    if(vazia(FILA)){
        printf("Fila vazia!\n\n");
        return ;
    }

    node *tmp;
    tmp = FILA->prox;
    printf("Fila :");
    while( tmp != NULL){
        printf("%5d", tmp->num);
        tmp = tmp->prox;
    }
    printf("\n        ");
    int count;
    for(count=0 ; count < tam ; count++)
        printf("  ^  ");
    printf("\nOrdem:");
    for(count=0 ; count < tam ; count++)
        printf("%5d", count+1);


    printf("\n\n");
}

void libera(node *FILA)
{
    if(!vazia(FILA)){
        node *proxNode,
              *atual;

        atual = FILA->prox;
        while(atual != NULL){
            proxNode = atual->prox;
            free(atual);
            atual = proxNode;
        }
    }
}
void excluiNegativos(node *FILA)
{
    if(vazia(FILA)){
        printf("Fila vazia!\n\n");
        return ;
    }

    node *tmp;
    tmp = FILA->prox;
    while( tmp != NULL){
        if(tmp->num<0){
            tmp->num = "";
        }
        tmp = tmp->prox;
    }
    printf("\n\n");
}
void somaFilas (node tmp1, node tmp2){

}
    
asked by anonymous 12.10.2015 / 21:15

1 answer

4

The logic to remove an element while searching in the queue is to keep a pointer to the previous element because it is the previous element that has to be changed by removing the current element from the queue. For example, if the current element, which must be removed (because it is negative but could be any other condition), is pointed to by "tmp", you need to point the element before the "tmp" to the next element to "tmp" .

Remember that there is a special situation when there is only one element in the queue. In this case there is no previous element and you should update the header of the queue.

It is also necessary to deallocate the removed element from memory or save it to do something with it later.

The pseudo-code is as follows:

node* anterior = FILA;
node* corrente = anterior->prox;
while (corrente != NULL) {
    if (corrente->num < 0) {
        node* tmp = corrente;
        anterior->prox = corrente->prox;
        corrente = corrente->prox;
        /* fazer algo com o nó em tmp - possivelmente free(tmp) */
    }
    else {
        anterior = corrente;
        corrente = corrente->prox;
    }
}

Good luck!

    
13.10.2015 / 07:34