push() in stack Array by R4R Team

What is Stack ?
Stack is a linear data structure which follows a particular order in which the operations are performed.
The order may be LIFO(Last In First Out) or FILO(First In Last Out).

What is push operation in Stack ?
-Insertion in stack is called as push operation in stack.
-These insertion are done at the top of the stack.

Function for push() function:

void push()
{
if(top==max)
{
printf("Stack is Overflow");
return 0;
}
top++;
printf("Enter Number to push in Stack\n");
scanf("%d",&stack[top]);
}



program-

#include< stdio.h>
#include< conio.h>
#define max 50
int stack[max],top=-1;

void create()
{
char ch;
do
{
top++;
printf("Enter Numbern");
scanf("%d",&stack[top]);
printf("Continue(y/n)n");
ch=getch();
}while(ch=='y');
}

void traverse()
{
int i;
printf("Stack is :n");
for(i=top;i>=0;i--){
printf("%d ",stack[i]);
}
}

void push()
{
if(top==max)
{
printf("Stack is Overflow");
return 0;
}
top++;
printf("Enter Number to push in Stackn");
scanf("%d",&stack[top]);
}

void main()
{
clrscr();
create();
traverse();
push();
traverse();
getch();
}


output-

Enter Number
3
Continue(y/n)?
Enter Number
4
Continue(y/n)?
Enter Number
5
Continue(y/n)?
Stack is :
5 4 3
Enter Number to push in Stack
8
Stack is :
8 5 4 3




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!