Skip to content

Instantly share code, notes, and snippets.

@ganobrega
Forked from milon/stack.cpp
Last active April 25, 2018 19:22
Show Gist options
  • Save ganobrega/5bb15ae504f9b757cad80cb1d664ac10 to your computer and use it in GitHub Desktop.
Save ganobrega/5bb15ae504f9b757cad80cb1d664ac10 to your computer and use it in GitHub Desktop.
Stack Example
//Stack using array
//Author: Milon
//Update: Gabriel
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ITENS 4
void push(char *a);
void pop();
void display();
int top=0;
int stack[MAX_ITENS];
void main(){
push("Gabriel");
push("Maria");
push("Fernando");
pop();
push("José");
display();
}
void push(char *nome){
if(top==MAX_ITENS){
printf("\nA Pilha está sobrecarregada.");
return;
}
stack[top]=nome;
top=top+1;
}
void pop(){
if(top==0){
printf("\nA Pilha está vazia.");
//getch();
return;
}
stack[top] = "";
top=top-1;
}
void display(){
if(top==0){
printf("\nA Pilha está vazia.");
return;
}
printf("\n--------------------------------------------------------------");
for(int i=0;i < top;i++){
printf("\n%d --> %s",i, stack[i]);
}
printf("\n--------------------------------------------------------------");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment