Buscar

Dada uma pilha contendo números inteiros quaisquer, construir uma função que coloca os pares na base da pilha e os ímpares no topo da pilha. Usar duas pilhas como auxiliares.

Esta é uma pré-visualização de arquivo. Entre para ver o arquivo original

#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
typedef struct pilha{
	int Pilha[SIZE];
 int top; 
} PILHA;
void push(PILHA *p, int x);
int stackpop(PILHA *p);
 
int main(int argc,char *argv[]){
 	PILHA p;
 	int i,j,aux,x;
 p.top = -1;
 
 for(i = 0; i < 5; i++){
		scanf("%d",&x);
		push(&p,x);
		//printf("%d",p.Pilha[i]);	
	}
	printf("\nTOPO: %d",stackpop(&p));
	printf("\n\n");
	
	//faz a troca de elementos. EX: pilha[0,1,2,3,4].
	for(i = 0; i < 5; i++){
		for(j = i+1; j < 5; j++){
			if(p.Pilha[i] % 2 != 0){
				aux = p.Pilha[i]; //aux = 0
				p.Pilha[i] = p.Pilha[j]; //pilha[i] = 1
				p.Pilha[j] = aux;//pilha[j] = 0 
			}else{
				break;
			}	
		}	
	}
	
	for(i = 0; i < 5; i++){
		printf("%d",p.Pilha[i]);	
	}	
	
	printf("\nTOPO: %d",stackpop(&p));
 
 return 0; 
}
 
void push(PILHA *p, int x){
 if(p->top == SIZE-1){
		printf("\nEstouro de Pilha!");
 system("pause");
 exit(1);
 }
 p->Pilha[++p->top] = x; 
}
int stackpop(PILHA *p){
	return p->Pilha[p->top];
}

Teste o Premium para desbloquear

Aproveite todos os benefícios por 3 dias sem pagar! 😉
Já tem cadastro?

Outros materiais