Buscar

Trabalho 1- Estrutura dados

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

#include <stdlib.h>
#include <stdio.h>
#define tam 10
struct registro{
	int topo;
	int item[tam];
};
	struct registro p1;
	struct registro p2;
	int vetor1[tam];
	int valor=0, cVetor = 0;
void push(){
	scanf("%d", &valor);
	p1.item[p1.topo] = valor;
	p1.topo++;
}
	
void popP1(){
	p2.item[p2.topo] = p1.item[p1.topo -1];
			
	if (p1.topo > 0){
		p1.topo--;
		p2.topo++;
	}
}
void popP2(){
	vetor1[cVetor] = p2.item[p2.topo-1];
	cVetor++;
	if (p2.topo > 0)
		p2.topo --;
}
void printPilhas(){
	int i, temp1=0, temp2=0;
	for(i=tam; i>=0; i--){	
		temp1 = 0;
		temp2 = 0;
		if(p1.topo > i)
			temp1 = p1.item[i];			 
		if(p2.topo > i)
			temp2 = p2.item[i];
		printf("\n%4d - %4d\t%4d\t%4d", i,temp1,temp2,vetor1[i]);	
	}
}
int countQtde(int i){
	switch(i){
		case 1:{return p1.topo; break;}
		case 2:{return p2.topo; break;}
		case 3:{return cVetor; break;}
	}
}
void menu(){
	printf("\n**************************************************\n");
	printf("[1-]ADC. PILHA1 [2-]MOVER PILHA2 [3-]MOVER VETOR\n");
	printf("[4-]VISUALIZAR [5-]CONTAR [6-]SAIR");
	printf("\n\n\tOPÇÃO: ");
}
void main(){
	
	int op = 0;
//	zera os topos
	p1.topo = 0;
	p2.topo = 0;
	system("clear");
	do{
		printPilhas();
		menu();
		scanf("%d", &op);
		switch(op){
			case 1:{
				printf("\tVALOR: ");
				push();
				system("clear");
				break;
			}
			case 2:{
				popP1();
				system("clear");
				break;
			}
			case 3:{
				popP2();
				system("clear");
				break;
			}
			case 4:{
				system("clear");
				printPilhas();	
				break;
			}
			case 5:{
				system("clear");
				printf("\nCount Pilha1: %d", countQtde(1));
				printf("\nCount Pilha2: %d", countQtde(2));
				printf("\nCount Vetor1: %d", countQtde(3));
				break;
			}
		}
	}while(op!=6);
}

Teste o Premium para desbloquear

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

Continue navegando