TrabalhosGratuitos.com - Trabalhos, Monografias, Artigos, Exames, Resumos de livros, Dissertações
Pesquisar

Estruturas De Dados Pilhas

Pesquisas Acadêmicas: Estruturas De Dados Pilhas. Pesquise 860.000+ trabalhos acadêmicos

Por:   •  27/11/2013  •  1.655 Palavras (7 Páginas)  •  452 Visualizações

Página 1 de 7

/*Enunciado Trab1 - Pilhas: - Escrever um procedimento que retira os valores ímpares da pilha P1.

Utilize push e pop e uma pilha auxiliar. Importante: 1) Fazer o programa em C (com a chamada ao procedimento e sua validação)

e apresentar em aula; 2) Postar o programa fonte no dia indicado no cronograma.*/

// VERSÃO FINAL

#include <stdio.h>

#include <malloc.h>

#include <stdlib.h>

/*==============================

| DECLARAÇÃO DE PILHA |

==============================*/

struct pilha{

int dado;

struct pilha *proximo;

};

/*==============================

| PUSH - EMPILHAR |

==============================*/

void PUSH (struct pilha **topo, int valor, int *sinal){

struct pilha *n;

n = (struct pilha *) malloc (sizeof(struct pilha));

if (n == NULL){

*sinal = 0;

}else{

n->dado = valor;

n->proximo = *topo;

*topo = n;

*sinal=1;

}

}

/*==============================

| POP - DESEMPILHAR |

==============================*/

int POP (struct pilha **topo, int *sinal){

int valor;

struct pilha *n;

if (*topo == NULL){

*sinal = 0;

}else{

valor = (*topo)->dado;

n = *topo;

*topo = n->proximo;

free(n);

*sinal=1;

}

return valor;

}

/*==============================

| RETIRA_IMPAR |

==============================*/

void retira_impar(struct pilha **topo1){

struct pilha *topoaux;

topoaux = NULL;

int ok=1;

int valor;

while ((*topo1) != NULL)

{

if ((*topo1)->dado % 2 == 0){

PUSH(&topoaux, POP(topo1, &ok), &ok);

}else{

//printf("\n ========================= ");

printf("\n O numero %d foi excluido!" , POP(topo1, &ok));

//printf("\n ========================= \n \n \n ");

}

}

while (topoaux != NULL){

PUSH(topo1, POP(&topoaux, &ok), &ok);

}

}

/*==============================

| MOSTRA_PILHA |

==============================*/

void mostra_pilha(struct pilha *topo, int *verifica){ // verifica serve para informar se nao houve inserção de dados ou se nenhum é par

struct pilha *aux;

if(topo == NULL){

printf("\n \n PILHA 1: \n ");

printf("\n ***** ESTA PILHA ESTA VAZIA POIS OS DADOS NAO FORAM INSERIDOS! ***** \n \n");

*verifica = 1;

}else{

printf("\n \n VALORES INSERIDOS NA PILHA 1: \n ");

...

Baixar como (para membros premium)  txt (4.8 Kb)  
Continuar por mais 6 páginas »
Disponível apenas no TrabalhosGratuitos.com