Sunday, January 26, 2014

C Program to illustrate Array Implementation of Stack

with 0 Comment
C Program to illustrate Array Implementation of Stack:

Main Program:

#include<stdio.h>
#include<conio.h>
#include "c:\tc\bin\subais.c"
void main()
{
int c,d;
clrscr();
while(1)
{
printf("\nEnter your choice\n");
printf("\n1.Push\n2.Pop\n3.display\n4.Exit");
scanf("%d",&c);
switch(c)
{
case 1:printf("Enter the element to be pushed\n");
scanf("%d",&d);
push(d);
printstack();
break;
case 2:d=pop();
if(d!=-1)
{
printf("The element is Popped is %d\n",d);
printstack();
}
else
printf("The stack is Empty\n");
break;
case 3:printstack();
break;
case 4:exit(0);
}
}
}

Sub-Program: ( Saved as 'subais.c' ) in the same folder of Main Program

#define size 10
int a[size];
int top=-1;
void printstack()
{
int j;
if(top==-1)
printf("Stack is empty");
else
printf("\nAfter the operation stack is \n");
for(j=0;j<=top;j++)
printf("%d \t ",a[j]);
}

void push(int d)
{
if(top==size-1)
printf("\nStack is full");
else
{
top++;
a[top]=d ;
}
}
int pop()
{
int data;
if(top==-1)
data=-1;
else
{
data=a[top];
top--;
}
return(data);

}

0 comments:

Post a Comment

Powered by Blogger.

Blog Archive