Sunday 7 April 2013

Stack operation using array in c program with output

STACK OPERATION USING ARRAY


#include<stdio.h>
#include<conio.h>
#define SIZE 5
int top=-1,data;
int stack[SIZE];
void push(int data)
{
if(top==SIZE-1)
{
printf("Stack is full\n");
return;
}
else
{
top=top+1;
stack[top]=data;
printf("Item added=%d\n",data);
}
}
void pop()
{
if(top<0)
{
printf("stack is empty\n");
return;
}
else
{
data=stack[top];
top=top-1;
printf("Popped element is=%d\n",data);
}
}
int main()
{
push(1);
push(2);
push(3);
push(4);
pop();
pop();
pop();
pop();
getch();
return;
}

 

Output
 
Item added=1
Item added=2
Item added=3
Item added=4
Popped element is=4
Popped element is=3
Popped element is=2
Popped element is=1

No comments:

Post a Comment