#include <stdio.h> #include <stdlib.h> #define MAX 100 typedef struct pilha { int topo; int dados[MAX]; } Pilha; Pilha *criarPilha(void) { Pilha *p = (Pilha *) malloc(sizeof(Pilha)); if (p != NULL) { p->topo = 0; } return p; } int eVazia(Pilha *p) { if (p->topo == 0){ return 1; } else { return 0; } } Pilha *empilhar(Pilha *p, int valor) { if (p == NULL || p->topo == MAX) { return p; } p->dados[p->topo] = valor; p->topo++; return p; } Pilha *desempilhar(Pilha *p) { if (p == NULL || p->topo == 0) { return p; } p->topo--; return p; } int topo(Pilha *p) { return p->dados[p->topo - 1]; } int main() { printf( "Diego Mendelson RA 14171 Gabriel Brito RA 14147 Thales Henrique RA 14494") ; // criando pilha Pilha* minhaPilha; minhaPilha = criarPilha(); // INSERE 100 ELEMENTOS int i = 1; for (i=1; i<=100; i++) { empilhar(minhaPilha, i); printf(" %d \n", topo(minhaPilha)); } // REMOVE 50 ELEMENTOS for (i=0; i<50; i++) { printf(" %d \n", topo(minhaPilha)); desempilhar(minhaPilha); } // TOPO DA LISTA printf("TOPO = %d \n", topo(minhaPilha)); return 0; }
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more