Skip to content

Instantly share code, notes, and snippets.

@viveksyngh
Last active August 29, 2015 14:25
Show Gist options
  • Save viveksyngh/6bd6771db62b6cfffb86 to your computer and use it in GitHub Desktop.
Save viveksyngh/6bd6771db62b6cfffb86 to your computer and use it in GitHub Desktop.
This is an array Implementation of Stack
/* Array Implementation of Stack */
/* Author Vivek Singh */
/* Last Modified on 19/7/2015 */
#include<stdio.h>
#include<stdlib.h>
#define MAX_SIZE 101
int A[MAX_SIZE];
int TOP = -1;
void Push(int item)
{
/* Insert the item at TOP of the stack */
if(TOP == MAX_SIZE - 1)
{
printf("STACK OVERFLOW!");
return;
}
A[++TOP] = item;
}
void Pop()
{
/* Remove an item from the top of the Stack */
if(TOP == -1)
{
printf("STACK UNDERFLOW");
return;
}
TOP--;
}
void Top()
{
/* Returns TOP element of the stack */
return A[TOP];
}
int isEmpty()
{
/* Checks whether Stack is empty or not */
if(TOP == -1)
return 1;
else
return 0;
}
void Print()
{
int i = 0;
for(i=0; i <= TOP; i++)
printf("%d \n", A[i]);
}
int main()
{
printf("%d\n", isEmpty());
Push(1);
Push(2);
Push(3);
Push(4);
Print();
printf("%d\n",isEmpty());
Pop();
Pop();
Print();
Pop();
Pop();
Pop();
Print();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment