Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef int TipoChave;
- typedef struct {
- int Chave;
- } TipoItem;
- typedef struct Celula_str *Apontador;
- typedef struct Celula_str {
- TipoItem Item;
- Apontador Prox;
- } Celula;
- typedef struct {
- Apontador Fundo, Topo;
- int Tamanho;
- } TipoPilha;
- void FPVazia(TipoPilha *Pilha){ //Faz a pilha ficar vazia.
- /*Pilha->Topo = (Apontador) malloc(sizeof(Celula));
- Pilha->Fundo = Pilha->Topo;
- Pilha->Topo->Prox = NULL;
- Pilha->Tamanho = 0;*/
- Pilha->Topo->Prox = NULL;
- Pilha->Topo = NULL;
- Pilha->Fundo = Pilha->Topo;
- Pilha->Tamanho = 0;
- }
- int Vazia(TipoPilha Pilha){ //Retorna true se a pilha está vazia; caso contrário, retorna false.
- return (Pilha.Topo == Pilha.Fundo);
- }
- void Empilha(TipoItem x, TipoPilha *Pilha){ //Empilha(x, Pilha). Insere o item x no topo da pilha.
- if(Vazia){
- Pilha->Fundo = Pilha->Topo;
- }
- Apontador Aux;
- Aux = (Apontador) malloc(sizeof(Celula));
- Aux->Item = x;
- Pilha->Topo = Aux;
- Pilha->Topo->Prox = NULL;
- Pilha->Tamanho++;
- /*Apontador Aux;
- Aux = (Apontador) malloc(sizeof(Celula));
- Pilha->Topo->Item = x;
- Aux->Prox = Pilha->Topo;
- Pilha->Topo = Aux;
- Pilha->Tamanho++;*/
- }
- void Desempilha(TipoPilha *Pilha, TipoItem *Item){ //Retorna o item x no topo da pilha, retirando-o da pilha.
- Apontador q;
- if (Vazia(*Pilha)){
- printf(" Erro: lista vazia\n");
- return;
- }
- q = Pilha->Topo;
- Pilha->Topo = q->Prox;
- *Item = q->Prox->Item;
- free(q);
- Pilha->Tamanho--;
- }
- int Tamanho(TipoPilha Pilha){ //Esta função retorna o número de itens da pilha.
- return (Pilha.Tamanho);
- }
- int main(){
- TipoPilha *Pilha;
- FPVazia(Pilha);
- TipoItem *Item;
- Item->Chave = 2;
- Empilha(Item, Pilha);
- Desempilha(Pilha, Item);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement