Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define TAMANHO 10
- struct VaPilha{
- int Item[TAMANHO]; //tipo de variavel a ser armazenada
- int TOPO; //Posição do vetor
- };
- void Inicia(VaPilha *p){ //inicia pilha
- p->TOPO=-1; //passa pilha como referencia
- }
- int VaPilha_vazia(VaPilha *p){
- if(p->TOPO==-1){
- return 1; //pilha vazia retorna 1}
- else{
- return 0; //caso contrario 0}
- }
- int VaPilha_Cheia(VaPilha *p){
- if(p->TOPO == TAMANHO){
- return 1; //Pilha cheia retorna 1}
- else{
- return 0;}
- }
- void VaPilha_Insere(VaPilha *p, int x){
- if(VaPilha_Cheia(p)==1){//verifica topo, passa como parametro a pilha
- printf("Pilha cheia\n");}
- else{
- p->TOPO++;
- p->Item[p->TOPO]=x; //x é o valor passado
- }
- }
- int VaPilha_Retira(VaPilha *p){
- int AUX=0;
- if(VaPilha_vazia(p)==1){
- printf("Pilha vazia\n");}
- else{
- AUX=p->Item[p->TOPO];
- p->TOPO--; //diminui topo
- return AUX; //retorna valor tirado}
- }
- int main(int argc, char** argv){
- VaPilha Pilha; //define pilha -- nas funçoes 'p'
- Inicia(&Pilha); //inicia mandando o endereço como parametro
- VaPilha_Insere(&Pilha, 1);
- VaPilha_Insere(&Pilha, 2);
- VaPilha_Insere(&Pilha, 3);
- int aux; //saida
- aux=VaPilha_Retira(&Pilha);
- printf("SAIU %d\n", aux); //SAIU 3
- aux=VaPilha_Retira(&Pilha);
- printf("SAIU %d\n", aux); //SAIU 2
- aux=VaPilha_Retira(&Pilha);
- printf("SAIU %d\n", aux); //SAIU 1
- aux=VaPilha_Retira(&Pilha);
- printf("SAIU %d\n", aux); //SAIU 1 //SAIU 0
- return 0;}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement