Pilha dinâmica em C
Publicado por Alexandre (azk) (última atualizaçăo em 08/03/2016)
[ Hits: 10.200 ]
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); }
Cálculo de Fatorial usando Loop
Nenhum coment�rio foi encontrado.
Aprenda a Gerenciar Permissões de Arquivos no Linux
Como transformar um áudio em vídeo com efeito de forma de onda (wave form)
Como aprovar Pull Requests em seu repositório Github via linha de comando
Aplicativo simples para gravar tela
Quebra de linha na data e hora no Linux Mint
trocar linhas [RESOLVIDO] (11)
Firefox năo abre em usuário năo administradores (2)
Ubuntu com problemas no áudio (1)