Skip to content

Instantly share code, notes, and snippets.

@RamonLopezEscudero
Created December 22, 2016 19:41
Show Gist options
  • Save RamonLopezEscudero/d40ee51cb7bed7825ef6ee3ca8fdaf1e to your computer and use it in GitHub Desktop.
Save RamonLopezEscudero/d40ee51cb7bed7825ef6ee3ca8fdaf1e to your computer and use it in GitHub Desktop.
Implementación de pilas en C
#include <stdio.h>
#include <stdlib.h>
int size_stack = 7;
void print_stack(int *stack, int *top)
{
int i;
for(i = 0; i < *top; i++) printf(" %i ", *(stack + i));
}
void push(int *stack, int *top, int val)
{
*top += 1;
if (*top > size_stack)
{
printf("Stack Overflow!");
exit(0);
}
*(stack + (*top - 1)) = val;
}
void pop(int *top)
{
*top -= 1;
if (*top < 0)
{
printf("Stack Underflow!");
exit(0);
}
}
void main()
{
int *stack, *top;
stack = (int*)malloc(sizeof(int) * size_stack);
top = (int*)malloc(sizeof(int));
*top = 0;
push(stack, top, 15);
push(stack, top, 6);
push(stack, top, 2);
push(stack, top, 9);
pop(top);
print_stack(stack, top);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment