Você está na página 1de 2

#include <stdio.

h>
#include <stdlib.h>

typedef struct nodo {


int info;
struct nodo* prox;
} Nodo;

Nodo* topo = NULL;

void empilhar(int);
int desempilhar();
void listar();
void sair();

int main(){
int option;
int num;

while(1){
printf("1- Empilhar \n2- Desempilhar \n3- Listar \n4- Sair \nDigite a opcao
desejada: ");
scanf("%d", &option);

system("cls");

switch (option)
{
case 1:
printf("Digite o numero que voce deseja empilhar: ");
scanf("%d", &num);
empilhar(num);
break;
case 2:
printf("O numero %d foi desempilhado.", desempilhar());
break;
case 3:
listar();
break;
case 4:
sair();
exit(0);
default:
printf("Opcao invalida!\n");
break;
}

system("pause");
system("cls");
}
}

void empilhar (int num){


Nodo* novo;
novo = (Nodo*)malloc(sizeof(Nodo));

if (novo == NULL){
exit(1);
}
novo->info = num;

if (topo == NULL){
novo->prox = NULL;
}

else {
novo->prox = topo;
}

topo = novo;
}

int desempilhar(){
int num;
Nodo* aux;

if (topo == NULL){
printf("A pilha esta vazia!\n");
system("pause");
return;
}

num = topo->info;
aux = topo;
topo = topo->prox;
free(aux);

system("pause");
return num;
}

void listar(){
Nodo* aux;
aux = topo;

while (aux != NULL){


printf("%d\n", aux->info);
aux = aux->prox;
}

system("pause");
}

void sair(){
Nodo* aux;
aux = topo;

if(topo == NULL){
return;
}

while (aux != NULL){


aux = topo;
topo = topo->prox;
free(aux);
}
}

Você também pode gostar