Created
October 11, 2012 12:41
-
-
Save kimkidong/3872044 to your computer and use it in GitHub Desktop.
Realization Stack with Using Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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