Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

PILHA 04.04.2019 ALUNOS DIEGO, GABRIEL E THALES




#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;
}


Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.