#include<stdio.h>
#include<stdlib.h>
typedef struct noLista {
int info;
struct noLista *prox;
} Elemento;
Elemento* criarNovo(int Caractere);
Elemento* Push(Elemento *Topo, int Caractere);
Elemento* Pop(Elemento *Topo);
Elemento* Top(Elemento *Topo);
int main () {
int Dados, i, op;
Elemento *Pilha = NULL, *aux, *Pilha2 = NULL;
do {
printf("1 - Adicionar elemento\n"); printf("2 - Remover elemento\n");
switch(op) {
case 1:
printf("Digite um inteiro: ");
Pilha = Push(Pilha, Dados);
printf("Elemento adicionado\n\n"); break;
case 2:
if (Top(Pilha2) == NULL){
while (Top(Pilha) != NULL){
int removido = Top(Pilha)->info;
Pilha = Pop(Pilha);
Pilha2 = Push(Pilha2, removido);
}
}
if(Top(Pilha2) != NULL) {
int removido = Top(Pilha2)->info;
Pilha2 = Pop(Pilha2);
printf("Elemento %d removido\n\n", removido
); } else {
printf("A pilha esta vazia\n\n"); }
break;
case 3:
break;
}
} while(op!=0);
return 0;
}
Elemento* criarNovo(int Caractere) {
Elemento *novo;
novo
= (Elemento
*) malloc(sizeof(Elemento
)); novo->info = Caractere;
novo->prox = NULL;
return novo;
}
Elemento* Push(Elemento *Topo, int Caractere) {
Elemento *novo;
novo = criarNovo(Caractere);
novo->prox = Topo;
Topo = novo;
return Topo;
}
Elemento* Pop(Elemento *Topo) {
Elemento *aux;
aux = Topo;
if(Topo != NULL) {
Topo = Topo->prox;
}
return Topo;
}
Elemento* Top(Elemento *Topo) {
return Topo;
}
I2luY2x1ZGU8c3RkaW8uaD4KI2luY2x1ZGU8c3RkbGliLmg+Cgp0eXBlZGVmIHN0cnVjdCBub0xpc3RhIHsKICAgIGludCBpbmZvOwogICAgc3RydWN0IG5vTGlzdGEgKnByb3g7Cn0gRWxlbWVudG87CgpFbGVtZW50byogY3JpYXJOb3ZvKGludCBDYXJhY3RlcmUpOwpFbGVtZW50byogUHVzaChFbGVtZW50byAqVG9wbywgaW50IENhcmFjdGVyZSk7CkVsZW1lbnRvKiBQb3AoRWxlbWVudG8gKlRvcG8pOwpFbGVtZW50byogVG9wKEVsZW1lbnRvICpUb3BvKTsKCmludCBtYWluICgpIHsKICAgIGludCBEYWRvcywgaSwgb3A7CiAgICBFbGVtZW50byAqUGlsaGEgPSBOVUxMLCAqYXV4LCAqUGlsaGEyID0gTlVMTDsKCiAgICBkbyB7CiAgICAgICAgcHJpbnRmKCIxIC0gQWRpY2lvbmFyIGVsZW1lbnRvXG4iKTsKICAgICAgICBwcmludGYoIjIgLSBSZW1vdmVyIGVsZW1lbnRvXG4iKTsKICAgICAgICBwcmludGYoIjAgLSBFbmNlcnJhclxuXG4iKTsKCiAgICAgICAgcHJpbnRmKCJPcGNhbzogIik7CiAgICAgICAgc2NhbmYoIiVkIiwgJm9wKTsKCiAgICAgICAgc3dpdGNoKG9wKSB7CiAgICAgICAgY2FzZSAxOgogICAgICAgICAgICBwcmludGYoIkRpZ2l0ZSB1bSBpbnRlaXJvOiAiKTsKICAgICAgICAgICAgc2NhbmYoIiVkIiwgJkRhZG9zKTsKCiAgICAgICAgICAgIFBpbGhhID0gUHVzaChQaWxoYSwgRGFkb3MpOwogICAgICAgICAgICBwcmludGYoIkVsZW1lbnRvIGFkaWNpb25hZG9cblxuIik7CiAgICAgICAgICAgIGJyZWFrOwoKICAgICAgICBjYXNlIDI6CiAgICAgICAgICAgIGlmIChUb3AoUGlsaGEyKSA9PSBOVUxMKXsKICAgICAgICAgICAgICAgIHdoaWxlIChUb3AoUGlsaGEpICE9IE5VTEwpewogICAgICAgICAgICAgICAgICAgIGludCByZW1vdmlkbyA9IFRvcChQaWxoYSktPmluZm87CiAgICAgICAgICAgICAgICAgICAgUGlsaGEgPSBQb3AoUGlsaGEpOwogICAgICAgICAgICAgICAgICAgIFBpbGhhMiA9IFB1c2goUGlsaGEyLCByZW1vdmlkbyk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KCiAgICAgICAgICAgIGlmKFRvcChQaWxoYTIpICE9IE5VTEwpIHsKICAgICAgICAgICAgICAgIGludCByZW1vdmlkbyA9IFRvcChQaWxoYTIpLT5pbmZvOwogICAgICAgICAgICAgICAgUGlsaGEyID0gUG9wKFBpbGhhMik7CiAgICAgICAgICAgICAgICBwcmludGYoIkVsZW1lbnRvICVkIHJlbW92aWRvXG5cbiIsIHJlbW92aWRvKTsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIHByaW50ZigiQSBwaWxoYSBlc3RhIHZhemlhXG5cbiIpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGJyZWFrOwoKICAgICAgICBjYXNlIDM6CgogICAgICAgICAgICBicmVhazsKCiAgICAgICAgfQogICAgfSB3aGlsZShvcCE9MCk7CiAgICByZXR1cm4gMDsKfQoKRWxlbWVudG8qIGNyaWFyTm92byhpbnQgQ2FyYWN0ZXJlKSB7CiAgICBFbGVtZW50byAqbm92bzsKCiAgICBub3ZvID0gKEVsZW1lbnRvKikgbWFsbG9jKHNpemVvZihFbGVtZW50bykpOwogICAgbm92by0+aW5mbyA9IENhcmFjdGVyZTsKICAgIG5vdm8tPnByb3ggPSBOVUxMOwoKICAgIHJldHVybiBub3ZvOwp9CgpFbGVtZW50byogUHVzaChFbGVtZW50byAqVG9wbywgaW50IENhcmFjdGVyZSkgewogICAgRWxlbWVudG8gKm5vdm87CgogICAgbm92byA9IGNyaWFyTm92byhDYXJhY3RlcmUpOwoKICAgIG5vdm8tPnByb3ggPSBUb3BvOwogICAgVG9wbyA9IG5vdm87CiAgICByZXR1cm4gVG9wbzsKfQoKRWxlbWVudG8qIFBvcChFbGVtZW50byAqVG9wbykgewogICAgRWxlbWVudG8gKmF1eDsKCiAgICBhdXggPSBUb3BvOwogICAgaWYoVG9wbyAhPSBOVUxMKSB7CiAgICAgICAgVG9wbyA9IFRvcG8tPnByb3g7CiAgICAgICAgZnJlZShhdXgpOwogICAgfQogICAgcmV0dXJuIFRvcG87Cn0KCkVsZW1lbnRvKiBUb3AoRWxlbWVudG8gKlRvcG8pIHsKICAgIHJldHVybiBUb3BvOwp9Cg==