Pilha dinâmica em C
Publicado por Alexandre (azk) (última atualização em 08/03/2016)
[ Hits: 10.239 ]
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); }
Exibi os números primos de um numero recebido pelo usuário, sem estrutura de repetição
Nenhum coment�rio foi encontrado.
Servidor de Backup com Ubuntu Server 24.04 LTS, RAID e Duplicati (Dell PowerEdge T420)
Visualizar câmeras IP ONVIF no Linux sem necessidade de instalar aplicativos
Atualizar Debian Online de uma Versão para outra
Instalar driver Nvidia no Debian 13
Redimensionando, espelhando, convertendo e rotacionando imagens com script
Debian 13 Trixie para Iniciantes
Convertendo pacotes DEB que usam ZSTD (Padrão Novo) para XZ (Padrão Antigo)
baschrc customizado pegeui vários. (3)
Rust é o "C da nossa geração"? (8)