|
Get 9,000+ Interview Questions with Answers in an eBook
C Source Codes List | Source Code Home
Program : Stack operations through array.
///////////////////////////////////////////////////////////////////
// STACK IMPLEMENTATION THROUGH ARRAY //
// PUSH . POP //
//////////////////////////////////////////////////////////////////
#include
#include
# define MAXSIZE 200
int stack[MAXSIZE];
int top; //index pointing to the top of stack
void main()
{
void push(int);
int pop();
int will=1,i,num;
clrscr();
printf("\n\t\tProgram for stack demonstration through array by Amit Mathur and Aditya Tiwari.\n\n\n");
while(will ==1)
{
printf("\n\t\tMAIN MENU: \n\t1.Add element to stack\n\t2.Delete element from the stack\n");
scanf("%d",&will);
switch(will)
{
case 1:
printf("\nEnter the data... ");
scanf("%d",&num);
push(num);
break;
case 2: i=pop();
printf("\nValue returned from pop function is %d ",i);
break;
default: printf("Invalid Choice . ");
}
printf(" \n\n\t\t\tDo you want to do more operations on Stack ( 1 for yes, any other key to exit) ");
scanf("%d" , &will);
} //end of outer while
} //end of main
void push(int y)
{
if(top>MAXSIZE)
{
printf("\n\n\t\tSTACK FULL\n\n");
return;
}
else
{
top++;
stack[top]=y;
}
}
int pop()
{
int a;
if(top<=0)
{
printf("\n\n\t\tSTACK EMPTY\n\n\t\t");
return 0;
}
else
{
a=stack[top];
top--;
}
return(a);
}
C Source Codes List | Source Code Home
|
|
|