Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //============= ARQUIVO PILHA.H ================
- #ifndef PILHA_H
- #define PILHA_H
- #define MAX_TAM 1000 //tamanho qualquer predefinido
- typedef int apontador;
- typedef char tipo_chave;
- typedef struct{
- tipo_chave chave;
- }tipo_item;
- typedef struct{
- tipo_item elementos[MAX_TAM];
- apontador topo;
- }tipo_pilha;
- typedef tipo_pilha *pilha;
- pilha cria_pilha();
- void termina_pilha(pilha *p);
- int pilha_cheia(pilha p);
- int pilha_vazia(pilha p);
- int pop(pilha p, tipo_item *e); //retira um item da pilha e armazena seu valor em *e
- int push(pilha p, tipo_item e); //coloca o item "e" na pilha
- #endif //fim PILHA_H
- //========== FIM DO ARQUIVO PILHA.H ================
- //========== ARQUIVO PILHA.C ===============
- #include <stdio.h>
- #include <stdlib.h>
- #include "pilha.h"
- pilha cria_pilha()
- {
- pilha p = (pilha)malloc(sizeof(tipo_pilha));
- if (!p)
- return 0;
- p->topo = -1; //pilha vazia
- return p;
- }
- void termina_pilha(pilha *p)
- {
- free(*p);
- }
- int pilha_cheia(pilha p)
- {
- return p->topo == MAX_TAM - 1;
- }
- int pilha_vazia(pilha p)
- {
- return p->topo==-1;
- }
- int pop(pilha p, tipo_item *e) //desempilha
- {
- if (p->topo == -1) //pilha vazia
- return 0;
- *e = p->elementos[p->topo];
- p->topo--;
- return 1;
- }
- int push(pilha p, tipo_item e) //empilha
- {
- if (p->topo == MAX_TAM -1) //ta cheia
- return 0;
- p->topo++;
- p->elementos[p->topo] = e;
- return 1;
- }
- // ============ FIM DO ARQUIVO PILHA.C ================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement