How to remove negative elements from a queue?

Asked

Viewed 1,166 times

0

Good afternoon, you guys,

I’m 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 exercise passed by the teacher: Given a row of integers, write a program that deletes all numbers negative without changing the position of the other items in the queue.

This is the code we implemented: Insert widget into 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 the stack (The one with problems):

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 in the queue is perfect, but deleting elements from the queue is bugging...

Thanks in advance!

Complete code, Compile using the insert option and then use the option to remove negative integer numbers.

#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){

}
  • And what is the bug? What is the structure of the node?

  • It is always deleting the first in the queue and not all negatives, we are entering with list....

  • How do you know which is the first one? I’ll ask you one more time. Put the knot structure. For me the insertion is wrong. Without seeing this code working, I don’t think I can answer this. I think he has another problem.

  • Ready, I edited and put the complete code I’m using.

  • The code doesn’t even compile. It may be that you are compiling for you because those who do not know the compiler well and the development process, end up missing errors, but if you compile to see all the errors, do not compile. I fixed some mistakes, but there are others, some very basic ones. You can’t touch it.

  • 1

    I will try to do otherwise and improve my understanding about the code and the errors, so q I know best sent edited the question again... Thanks for the help

  • An important thing, is this a queue or a linked list? The name used is queue, but it does not appear to be queue. The nameplates are not queued. Where does the queue start and end? This information is not in the code and there is no way to manage a queue without this information. I read the code more carefully and does not do what is stated in the question.

Show 2 more comments

1 answer

4


The logic to remove an element during the queue search is to keep a pointer to the previous element, as it is the previous element that has to be changed when 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 "tmp" to the element following "tmp".

Remember that there is a special situation which is when there is only one element in the queue. In this case there is no previous element and the row header should be updated.

It is also necessary to de-locate 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!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.