New Want to Write for Computaholics ? if Yes Contact us at "contact@computaholics.in"

Stack implementation using array

Stack implementation using array
Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition.

Pop: Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition

#include<stdio.h>
#include<stdlib.h>
#define MAX_SIZE 100

int a[MAX_SIZE];
int Top=-1;

void push(int n)
{

if(Top==MAX_SIZE-1)  //check if Top is at last index of array
{
printf("Error:Stack is full\n");
return;
}

Top++;
a[Top]=n;

}

void pop()
{

if(Top==-1)  //if stack is empty Top==-1
{
printf("Error:Stack is Empty\n");
return;
}

Top--;

}

void print()

{
int i;

printf("Stack is:\t");

for(i=0;i<=Top;i++)
{

printf("%d\t",a[i]);

}
printf("\n");
}

main()
{

push(1);
print();
push(2);
print();
push(3);
print();
push(4);
print();
push(5);
print();
push(6);
print();
push(7);
print();
push(8);
print();
push(9);
print();

pop();
print();
pop();
print();
pop();
print();
pop();
print();
pop();
print();
pop();
print();
pop();
print();
pop();
print();
}

Copyright © C Program | Java | OpenGL Programming | Hadoop at Computaholics