/*PROGRAM TO PERFORMS PUSH OPERATION ON STACK USING ARRAY*/
#include<stdio.h>
#define max 100
int top = -1;
int flag = 0;
int stack[max];
void push(int *, int);
void display(int *);
/* Function to perform push operation */
void push(int stack[], int item)
{
if(top ==(max-1))
flag = 0;
else
{
flag = 1;
top++;
stack[top] = item;
}
}
/* Fuction to display stack */
void display(int stack[])
{
int i;
if(top == -1)
{
printf("\n\n\t\t\t Stack is empty");
}
else
{
for(i=top;i>=0;i--)
printf("\n\n\n\t\t\t| stack[%d] = %d |",i,stack[i]);
}
}
/* Main Function */
void main()
{
int item;
int n,i;
int top = -1;
clrscr();
printf(" How many items you want to push:");
scanf("%d",&n);
for(i=1; i<=n; i++)
{
printf("\n\n\n Enter item:");
scanf("%d",&item);
push(stack,item);
if(flag)
{ printf("\n\n\n\t\t ---------------------------");
printf("\n\t\t Stack after Push operation:\n");
printf("\n\t\t ---------------------------");
display(stack);
}
else
{
printf("\n\n\n\t\t ==============");
printf("\n\t\t Stack is full");
printf("\n\t\t ==============");
}
}
getch();
}
#include<stdio.h>
#define max 100
int top = -1;
int flag = 0;
int stack[max];
void push(int *, int);
void display(int *);
/* Function to perform push operation */
void push(int stack[], int item)
{
if(top ==(max-1))
flag = 0;
else
{
flag = 1;
top++;
stack[top] = item;
}
}
/* Fuction to display stack */
void display(int stack[])
{
int i;
if(top == -1)
{
printf("\n\n\t\t\t Stack is empty");
}
else
{
for(i=top;i>=0;i--)
printf("\n\n\n\t\t\t| stack[%d] = %d |",i,stack[i]);
}
}
/* Main Function */
void main()
{
int item;
int n,i;
int top = -1;
clrscr();
printf(" How many items you want to push:");
scanf("%d",&n);
for(i=1; i<=n; i++)
{
printf("\n\n\n Enter item:");
scanf("%d",&item);
push(stack,item);
if(flag)
{ printf("\n\n\n\t\t ---------------------------");
printf("\n\t\t Stack after Push operation:\n");
printf("\n\t\t ---------------------------");
display(stack);
}
else
{
printf("\n\n\n\t\t ==============");
printf("\n\t\t Stack is full");
printf("\n\t\t ==============");
}
}
getch();
}
No comments:
Post a Comment