row on c doubt

Asked

Viewed 98 times

0

having the code of the queue need to remove value and position both chosen by the user

typedef struct reg {
   int         conteudo; 
   struct reg *prox;
} celula;

// Tira um elemento da fila fi e devolve
// o conteudo do elemento removido.
// Supõe que a fila não está vazia.

int busca_Remove (celula *fi,int num) {
   int x;
   celula *p;
   p = fi->prox;  // o primeiro da fila

   x = p->conteudo;
   fi->prox= p->prox;

   return x;
}


}


// Coloca um novo elemento com conteudo y
// na fila fi. Devolve o endereço da
// cabeça da fila resultante.

celula *Insere (int y, celula *fi, int vsize) { 
   celula *nova;
   nova = (celula*)malloc (sizeof(celula)*vsize);

   nova->prox = fi->prox;
   fi->prox = nova;
   fi->conteudo = y;

   return nova;
}

void show(celula *fi)
{
    celula *p;
    p = fi->prox; 
    printf("\n\n\nImprimindo a fila: \n");

    while(p != fi)
    {
        printf("%d  ", p->conteudo);
        p = p->prox;
    }
}


int main(){
    celula *fi;
    fi = (celula*)malloc (sizeof(celula));
    fi->prox = fi;
    int a,b,c;
    int i,tam;
    printf("Digite o tamanho: ");
    scanf("%d",&tam);   

    printf("Inserindo valores....\n "); 

    for (i = 1; i <= tam; i++){
        printf("Digite o valor: ");
        scanf("%d",&a); 
        fi = Insere(a, fi,tam);
    }

    show(fi);
    printf("Digite o valor: ");
    scanf("%d",&c);
    printf("\nRemove elementos da fila\n");



    printf("\n\nRemovendo os elementos.... ");

    free(fi);
    printf("\n\n");
    return 0;
}

1 answer

0

This you want to do hurts the queue implementation, because in the queue can only be removed the first one in the queue and the insertion is done at the end of the queue. Anyway, for this problem, you create a cell pointer and go through your queue comparing to what the user typed.When you find it, you need to tidy up the queue so it doesn’t trigger,creates another pointer to save the previous node and chain your queue and gives the free on the cell pointer you created.

Browser other questions tagged

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