Advertisement
progamer_denys

Pilha-ex-1

Dec 11th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. typedef struct elemento* Pilha;
  5. struct elemento
  6. {
  7.     int dado;
  8.     struct elemento *prox;
  9. };
  10. typedef struct elemento Elem;
  11. void menu();
  12. Pilha* PILHA_construtor();//CONTTRUIR
  13. bool PILHA_vazia(Pilha* pi);//VAZIA
  14. bool PILHA_push(Pilha* pi, int dado);//PUSh
  15. void PILHA_pop(Pilha* pi);//REMOVER
  16. void PILHA_imprimir(Pilha* pi);//IMPRIMIR
  17. int PILHA_tamanho(Pilha* pi);//TAMANHO
  18.  
  19.  
  20.  
  21.  
  22. int main()
  23. {
  24.     int op, dado;
  25.     Pilha* pi;
  26.     do{
  27.     menu();
  28.     scanf("%d", &op);
  29.     switch(op)
  30.     {
  31.     case 1://criar pilha
  32.         system("cls");
  33.         pi = PILHA_construtor();
  34.         printf("---Pilha Criada Com Sucesso---\n");
  35.  
  36.         break;
  37.     case 2://PUSH - INSERIR
  38.         system("cls");
  39.         printf("Entre com o dado a ser inserido\n");
  40.         scanf("%d", &dado);
  41.         PILHA_push(pi, dado);
  42.         system("cls");
  43.  
  44.         break;
  45.     case 3://POP
  46.         system("cls");
  47.         if(PILHA_vazia(pi))
  48.         {
  49.             printf("---Erro Pilha Vazia---\n");
  50.         }else{
  51.             PILHA_pop(pi);
  52.         }
  53.         break;
  54.     case 4://TAMANHO
  55.         system("cls");
  56.         if(PILHA_vazia(pi))
  57.         {
  58.             printf("---Pilha Vazia---\n");
  59.         }else{
  60.             printf("---A Pilha Possui %d Elementos---\n", PILHA_tamanho(pi));
  61.         }
  62.         break;
  63.     case 5:
  64.         system("cls");
  65.         if(PILHA_vazia(pi))
  66.         {
  67.             printf("---Erro Pilha vazia---\n");
  68.         }else{
  69.             PILHA_imprimir(pi);
  70.  
  71.         }
  72.         break;
  73.     case 6:
  74.         system("cls");
  75.         printf("---Programa encerrado---\n");
  76.         exit(0);
  77.         break;
  78.     default:
  79.         printf("Opcao invalida\n");
  80.         break;
  81.     }
  82. }while(op !=6 );
  83.  
  84.  
  85. return 0;
  86.  
  87. }
  88. //MENU
  89. void menu()
  90. {
  91.  
  92.     printf("Escolha uma opcao:\n1 - Criar pilha\n2 - Inserir elementos\n3 - Remover\n4 - Tamanho da Pilha\n5 - Imrpimir Elementos\n6 - Sair\n");
  93. }
  94. //CRIAR Pilha
  95. Pilha* PILHA_construtor()
  96. {
  97.     Pilha* pi = (Pilha*) malloc(sizeof(Pilha));
  98.     if(pi != NULL)
  99.         *pi = NULL;
  100.     return pi;
  101. }
  102. //PUSH INSERIR
  103. bool PILHA_push(Pilha* pi, int dado)
  104. {
  105.     if(pi == NULL)
  106.         return false;
  107.     Elem* no;
  108.     no = (Elem*) malloc(sizeof(Elem));
  109.     if(no == NULL)
  110.         return false;
  111.     no->dado = dado;
  112.     no->prox = (*pi);
  113.     *pi = no;
  114.     return true;
  115. }
  116. //POP PILHA
  117. void PILHA_pop(Pilha* pi)
  118. {
  119.     Elem *no = *pi;
  120.     *pi = no->prox;
  121.     free(no);
  122. }
  123.  
  124. //IMPRIMIR
  125. void PILHA_imprimir(Pilha* pi)
  126. {
  127.     Elem* no = *pi;
  128.     while(no != NULL){
  129.         printf("DADO: %d\n",no->dado);
  130.  
  131.         printf("------------\n");
  132.         no = no->prox;
  133.     }
  134. }
  135. //PILHA VAZIA
  136. bool PILHA_vazia(Pilha* pi)
  137. {
  138.     if(pi == NULL)
  139.         return true;
  140.     if(*pi == NULL)
  141.         return true;
  142.     return false;
  143. }
  144. //PILHA TAMANHO
  145. int PILHA_tamanho(Pilha* pi)
  146. {
  147.     if(pi == NULL)
  148.         return 0;
  149.     int cont = 0;
  150.     Elem* no = *pi;
  151.     while(no != NULL){
  152.         cont++;
  153.         no = no->prox;
  154.     }
  155.     return cont;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement