Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define MAX 100
- struct Pilha{
- int vetor[MAX];
- int topo;
- };
- void iniciaPilha(struct Pilha *pilha){
- pilha->topo = -1;
- }
- int vaziaPilha(struct Pilha *pilha){
- //O indice do topo da pilha é utilizado como valor para checar o tamanho
- if(pilha->topo == -1){
- return 1;
- }else{
- return 0;
- }
- }
- int cheiaPilha(struct Pilha *pilha){
- if(pilha->topo == MAX - 1){
- return 1;
- }else{
- return 0;
- }
- }
- int tamanhoPilha(struct Pilha *pilha){
- return pilha->topo + 1;
- }
- int push(struct Pilha *pilha, int valor){
- if(cheiaPilha(pilha)){
- return 0;
- }else{
- pilha->vetor[ ++(pilha->topo) ] = valor;
- return 1;
- }
- }
- int pop(struct Pilha *pilha, int *dado){
- if(vaziaPilha(pilha)){
- return 0;
- }else{
- *dado = pilha->vetor[ pilha->topo ];
- --(pilha->topo);
- return 1;
- }
- }
- int topoPilha(struct Pilha *pilha, int *dado){
- if(vaziaPilha(pilha)){
- return 0;
- }else{
- *dado = pilha->vetor[ pilha->topo ];
- return 1;
- }
- }
- int mostrarPilha(struct Pilha *pilha){
- int count, posicao;
- if(vaziaPilha(pilha)){
- return 0;
- }
- printf("\nTamanho da pilha: %d elemento(s).\n", pilha->topo + 1);
- printf("----------------------------------------------------\n");
- //Demostra os elementos da pilha do baixo para cima e sua posição
- for(count = pilha->topo + 1, posicao = 1; count > 0; count--, posicao++){
- printf("\n\tPosicao: %d\n", posicao);
- printf("\tValor: %d\n", pilha->vetor[count - 1]);
- }
- printf("\n");
- return 1;
- }
- int removeValor(struct Pilha *pilha, int valor){
- //Pilha auxiliar
- struct Pilha aux;
- iniciaPilha(&aux);
- //Variavel auxiliar para desempilhar e empilhar um elemento qualquer
- int i;
- int total = 0;
- if(vaziaPilha(pilha)){
- return 0;
- }
- while(vaziaPilha(pilha) != 1){
- //Se o elemento for diferente do valor procurado, será desempilhado e empilhado na pilha auxiliar
- if(pilha->vetor[ pilha->topo ] != valor){
- pop(pilha, &i );
- push(&aux, i );
- }else{
- //Se o elemento for encontrado é apenas ignorado e pulará para o proximo elemento
- --(pilha->topo);
- total++;
- }
- }
- //Desempilha da auxiliar e empilha na pilha inicial todos os elementos que são diferentes do valor fornecido
- while(vaziaPilha(&aux)!=1){
- pop(&aux, &i );
- push(pilha, i );
- }
- //Retorna a quantidade de vezes que o valor dado foi encontrado
- return total;
- }
- int comparaPilha(struct Pilha *pilha, struct Pilha *pilha_2){
- int i;
- //Se for diferente o tamanho das pilhas será encerrada a função retornando FALSO
- if(tamanhoPilha(pilha) != tamanhoPilha(pilha_2)){
- return 0;
- }else{
- //Compara cada elemento da pilha, se um dos elementos for diferente a função retorna FALSO
- for(i = pilha->topo; i >= 0; i--){
- if( pilha->vetor[ i ] != pilha_2->vetor[ i ]){
- return 0;
- }
- }
- return 1;
- }
- }
- int invertePilha(struct Pilha *pilha){
- int i, aux;
- if(vaziaPilha(pilha)){
- return 0;
- }
- //Troca o primeiro elemento da pilha com o ultimo, o segundo com o penultimo e assim por diante...
- for( i = 0; i <= (pilha->topo / 2) ; i++){
- aux = pilha->vetor[ i ];
- pilha->vetor[ i ] = pilha->vetor[pilha->topo - i];
- pilha->vetor[pilha->topo - i] = aux;
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment