Friday, 8 February 2013

PROGRAM TO PERFORM POP OPERATION ON STACK USING ARRAY | Data Structures Tutorial pdf

/*PROGRAM TO PERFORM POP OPERATION ON STACK USING ARRAY*/
#include<stdio.h>
#include<conio.h>
#define max 100
int top = -1;
int flag = 0;
int stack[max];
void push(int *,int);
int pop(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;
}
}

/* Function to perform pop operation */
int pop(int stack[])
{
int item;
if(top < 0)
{
item= 0;
flag = 0;
}
else
{
flag = 1;
item = stack[top];
top--;
}
return (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 n,i,item,f=1;
int top = -1;
clrscr();
printf("\n\n\n\t\t ==============");
printf("\n\t\t Stack is empty");
printf("\n\t\t ==============");
printf("\n\n push some items onto stack");
printf("\n\n 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 ==============");
}
}
while(f==1)
{
item = pop(stack);
if(flag)
{
printf("\n\n The poped item is: %d",item);
printf("\n\n\n\t\t --------------------------");
printf("\n\n\t\t Stack after pop operation:\n");
printf("\n\t\t ---------------------------");
display(stack);
}
else
{
printf("\n\n\n\t\t ==============");
printf("\n\t\t Stack is empty");
printf("\n\t\t ==============");
break;
}
printf("\n\n Pop again (1/0): ");
scanf("%d",&f);
}
getch();
}

No comments: