Advertisement
pablosoares

pilha

Apr 19th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.06 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct no {
  5.     int valor;
  6.     struct no *prox;
  7. };
  8.  
  9. struct no *base=NULL;
  10.  
  11. void push(int x){
  12.     if(base ==  NULL){
  13.         base = (struct no *) malloc(sizeof(struct no*));
  14.         base->valor = x;
  15.         return;
  16.     }
  17.     struct no *aux = base;
  18.     while(aux->prox != NULL){
  19.         aux = aux->prox;
  20.     }
  21.     struct no *novo = (struct no *) malloc(sizeof(struct no*));
  22.     aux->prox = novo;
  23.     novo->valor = x;
  24.     printf("%d\n",base->valor);
  25. }
  26.  
  27. void mostrar(){
  28.     if(base == NULL)    return;
  29.     while(base != NULL){
  30.         printf("%d\n", base->valor);
  31.         base = base->prox;
  32.     }
  33. }
  34.  
  35. void pop(){
  36.     if(base == NULL)    return;
  37.     if(base->prox == NULL){
  38.         free(base);
  39.         base = NULL;
  40.     }
  41.     struct no *aux = base;
  42.     struct no *ant;
  43.     while(aux->prox != NULL){
  44.         ant = aux;
  45.         aux = aux->prox;
  46.     }
  47.     free(aux);
  48.     ant->prox = NULL;
  49.     return;
  50. }
  51.  
  52.  
  53. int main(){
  54.     push(8);
  55.     push(9);
  56.     push(10);
  57.    
  58.     //mostrar();
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement