Wednesday, 5 December 2012

Program for stack using Array

// Program for stack using Array //

#include<stdio.h>

#include<conio.h>

#define max 10

int stack [max];

int top=-1;

 

void push(int val)

{

if(top==max-1)

{

printf("\n stack is overflow");

return;

}

top++;

stack[top]=val;

}

int pop()

{

int temp;

if(top==-1)

{

printf("\n stack underflow");

return 0;

}

temp=stack[top];

top-- ;

return temp;

}

void main()

{

int ch,val;

clrscr();

do

{

printf("\n!!-----MENU-----!!");

printf("\n  1. push  ");

printf("\n  2. pop  ");

printf("\n  3. exit ");

printf("\n------------------");

printf("\n enter the choice");

scanf("%d",&ch);

if(ch==1)

{

printf("\n enter the value");

scanf("%d",&val);

push(val);

}

else if(ch==2)

{

val=pop();

printf("\n item remove is %d",val);

}

else if(ch==3)

{

printf("\n good bye");

break;

}

else

{

printf("\n invalid choice");

}

}while(ch!=3);

getch();

}

No comments:

Post a Comment