Skip to content

Instantly share code, notes, and snippets.

@kaleocheng
Created June 21, 2014 10:00
Show Gist options
  • Save kaleocheng/65b0320ba6291a9db188 to your computer and use it in GitHub Desktop.
Save kaleocheng/65b0320ba6291a9db188 to your computer and use it in GitHub Desktop.
Stack
#include<stdio.h>
#include<assert.h>
#include"stack.h"
#define MAX 10
static STACK_TYPE arry[MAX];
static int topElement = -1;
#include"StackOption.c"
int main()
{
push(2);
push(4);
push(6);
push(8);
push(10);
printf("%d ",top());
pop();
printf("%d ",top());
pop();
printf("%d ",top());
pop();
printf("%d ",top());
pop();
printf("%d ",top());
printf("\n");
}
#define STACK_TYPE int
void push(STACK_TYPE value);
void pop(void);
STACK_TYPE top(void);
int isFull(void);
int isEmpty(void);
/*
push
*/
void push(STACK_TYPE value)
{
assert(!isFull());
topElement += 1;
arry[topElement] = value;
}
/*
pop
*/
void pop(void)
{
assert(!isEmpty());
topElement -= 1;
}
/*
top
*/
STACK_TYPE top(void)
{
assert(!isEmpty());
return(arry[topElement]);
}
/*
isFull
*/
int isFull(void)
{
return topElement == MAX-1;
}
/*
isElement
*/
int isEmpty(void)
{
return topElement == -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment