Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct _pilha{
- int *vetor;
- int topo;
- int limite;
- }TpPilha;
- TpPilha * criarPilha(int size){
- TpPilha *p = (TpPilha *)malloc(sizeof(TpPilha));
- if(p){
- p->vetor = (int *)calloc(size,sizeof(int));
- }else{
- return NULL;
- }
- if(p->vetor){
- p->topo = -1;
- p->limite = size-1;
- return p;
- }else{
- return NULL;
- }
- }
- int eCheia(TpPilha *p,int size){
- if(p->topo == size){
- return 1;
- }else{
- return 0;
- }
- }
- int eVazia (TpPilha *p){
- if(p->topo == -1){
- return 1;
- }else{
- return 0;
- }
- }
- int Push(TpPilha *p,int e){
- if(eCheia(p,e)==0){
- p->topo++;
- p->vetor[p->topo] = e;
- return 1;
- }else{
- printf("Pilha cheia!");
- return 0;
- }
- }
- int Pop(TpPilha *p){
- if(eVazia(p)==0){
- p->topo--;
- return 1;
- }else{
- printf("Pilha Vazia!");
- return 0;
- }
- }
- int Exibe(TpPilha *p){
- if(eVazia(p)!=1){
- int i;
- for(i=p->topo;i>-1;i--){
- printf("\n%d",p->vetor[i]);
- }
- }else{
- printf("Pilha vazia!");
- }
- }
- int Consulta(TpPilha *p){
- if(eVazia(p)!=1){
- printf("Topo: %d",p->vetor[p->topo]);
- return 1;
- }else{
- printf("Pilha vazia!");
- return 0;
- }
- }
- int main(){
- int size,num,op = 0;
- printf("Informe o tamanho da Pilha");
- scanf("%d",&size);
- TpPilha *p;
- criarPilha(size);
- p=criarPilha(size);
- do{
- printf("\n\t----------Menu----------");
- printf("\n\t1: Inserir");
- printf("\n\t2: Extrair");
- printf("\n\t3: Exibir");
- printf("\n\t4: Consultar");
- printf("\n\t5: Sair");
- printf("\n\t------------------------");
- printf("\n\tEscolha uma Opcao");
- scanf("%d",&op);
- switch(op){
- case 1:
- printf("Digite o elemento a ser inserido");
- scanf("%d",&num);
- Push(p,num);
- break;
- case 2:
- Pop(p);
- break;
- case 3:
- Exibe(p);
- break;
- case 4:
- Consulta(p);
- break;
- }
- }while(op!=5);
- free(p);
- }
Add Comment
Please, Sign In to add comment