Skip to content

Instantly share code, notes, and snippets.

@kimkidong
Created October 11, 2012 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kimkidong/3872044 to your computer and use it in GitHub Desktop.
Save kimkidong/3872044 to your computer and use it in GitHub Desktop.
Realization Stack with Using Array
#include <stdio.h>
bool push(int data);
int pop();
bool isFull();
bool isEmpty();
void PrintStack();
#define STACK_SIZE 4
#define STACK_EMPTY_POSITION -1
int Stack[STACK_SIZE];
int top = STACK_EMPTY_POSITION;
void main()
{
push(1),push(2),push(3),push(4);
PrintStack();
pop(),pop();
PrintStack();
}
bool push(int data)
{
if(isFull())
{
printf("Stack is Full \n");
return false;
}
Stack[++top] = data;
return true;
}
int pop()
{
if(isEmpty())
{
printf("Stack is Empty \n");
return false;
}
int ret_val = Stack[top];
Stack[top--] = 0;
return ret_val;
}
bool isFull()
{
return (top+1) == STACK_SIZE ? true : false;
}
bool isEmpty()
{
return top == STACK_EMPTY_POSITION ? true : false;
}
void PrintStack()
{
for(int i = top ; i >= 0 ; --i)
printf("%2d", Stack[i]);
printf("\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment