Pilha dinâmica em C
Publicado por Alexandre (azk) (última atualização em 08/03/2016)
[ Hits: 10.211 ]
Homepage: null
Implementação de uma simples pilha dinâmica em C.
Código em en_US.
azk4n
#include <stdio.h> #include <stdlib.h> typedef struct stackNode{ int number; struct stackNode *next; }no; int size; no *temp; void startStack(no *stack){ stack->next = NULL; size = 0; } int testEmpty(no *stack){ if(stack->next == NULL) return 1; else return 0; } no *pusher(){ no *new = (no*) malloc(sizeof(no)); if(!new) printf("\nno memory\n\n"); else{ printf("\ntake a new number: "); scanf("%d", &new->number); } return new; } void push(no *stack){ no *new = pusher(); new->next = NULL; if(testEmpty(stack)){ stack->next = new; size++; }else{ temp = stack->next; while(temp->next != NULL){ temp = temp->next; } temp->next = new; size++; } } no *pop(no *stack){ if(testEmpty(stack)){ printf("\n\nempty stack\n\n"); return NULL; }else{ no *last = stack->next; no *antlast = stack; while(last->next != NULL){ antlast = last; last = last->next; } antlast->next = NULL; size--; return last; } } void showStack(no *stack){ if(testEmpty(stack)){ printf("\n\nempty stack\n\n"); }else{ temp = stack->next; printf("\n"); while(temp != NULL){ printf("%d ", temp->number); temp = temp->next; } printf("\nSize of stack: %d\n\n", size); } } void freeStack(no *stack){ if(testEmpty(stack)){ printf("\n\nempty stack\n\n"); }else{ no *node = stack->next, *nxtNode; while(node != NULL){ nxtNode = node->next; free(node); node = nxtNode; } printf("\nstack cleaned\n\n"); } } int main(){ no *stack = (no*) malloc(sizeof(no)); if(!stack) printf("\nno memory\n"); else startStack(stack); int opt; do{ printf("0 -> quit\n"); printf("1 -> push\n"); printf("2 -> pop\n"); printf("3 -> show stack\n"); printf("4 -> free stack\n"); printf("opt: "); scanf("%d", &opt); switch(opt){ case 1: push(stack); break; case 2: temp = pop(stack); if(temp != NULL){ printf("\nelement removed: %d\n\n", temp->number); free(temp); } break; case 3: showStack(stack); break; case 4: freeStack(stack); startStack(stack); break; default: if(opt != 0) printf("take valid option\n\n"); } }while(opt != 0); }
Nenhum coment�rio foi encontrado.
Atualizando o Passado: Linux no Lenovo G460 em 2025
aaPanel - Um Painel de Hospedagem Gratuito e Poderoso
O macete do Warsaw no Linux Mint e cia
Visualizar arquivos em formato markdown (ex.: README.md) pelo terminal
Dando - teoricamente - um gás no Gnome-Shell do Arch Linux
Como instalar o Google Cloud CLI no Ubuntu/Debian
Mantenha seu Sistema Leve e Rápido com a Limpeza do APT!
Procurando vídeos de YouTube pelo terminal e assistindo via mpv (2025)
Pastas da raiz foram para a área de trabalho [RESOLVIDO] (10)
Será que eu deveria apreender C/C++ para desenvolver para Linux? (4)