Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <locale.h>
- typedef struct pilhaStruct {
- int dado;
- struct pilhaStruct *prox;
- } pilha;
- void inserirPilha(pilha **topo, int numero) {
- pilha *novoElemento;
- novoElemento = malloc(sizeof(pilha));
- novoElemento->dado = numero;
- if (*topo == NULL) {
- novoElemento->prox = NULL;
- } else {
- novoElemento->prox = *topo;
- *topo = novoElemento;
- }
- *topo = novoElemento;
- }
- void removerPilha(pilha **topo) {
- pilha *elementoRemover;
- if (*topo != NULL) {
- elementoRemover = *topo;
- *topo = (*topo)->prox;
- free(elementoRemover);
- }
- }
- void printarPilha(pilha **topo) {
- pilha *elTemp;
- elTemp = *topo;
- while (elTemp != NULL)
- {
- printf("%d ", elTemp->dado);
- elTemp = elTemp->prox;
- }
- printf("\n");
- }
- int main () {
- setlocale(LC_ALL, "");
- pilha *topo = NULL;
- int escolha = 0, n = 0;
- while (escolha != 4)
- {
- printf("\n[1] - Inserir na pilha\n[2] - Remover da pilha\n[3] - Listar pilha\n[4] - Sair\nDigite uma opção: ");
- scanf("%d", &escolha);
- switch (escolha) {
- case 1:
- printf("Digite o numero para o topo: ");
- scanf("%i", &n);
- printf("Numero %i adicionado\n", n);
- inserirPilha(&topo, n);
- break;
- case 2:
- removerPilha(&topo);
- break;
- case 3:
- printarPilha(&topo);
- break;
- case 4:
- printf("Obrigado por usar o programa... Estou saindo...");
- break;
- default:
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement