Data Structure using Array

Stack:

#include<stdio.h>
void main()
{
int ar[10];
int item;
int top=-1;
int ch,i;
printf("1-Push\n2-Pop\n3-Display\n");
printf("Enter your choice\n");
scanf("%d",&ch);
do
{
if(ch==1)
{
if(top==10)
{
printf("Stack if full\n");
}
else
{
printf("Enter number\n");
scanf("%d",&item);
top=top+1;
ar[top]=item;
}
}
else if(ch==2)
{
if(top==-1)
{
printf("Underflow\n");
}
else
{
item=ar[top];
top=top-1;
printf("%d\n",item);
}
}
if(ch==3)
{
if(top==-1)
{
printf("Stack is empty\n");
}
else
{
for(i=top;i>=0;i--)
{
printf("%d\n",ar[i]);
}
}
}
printf("Enter your choice\n");
scanf("%d",&ch);
}while(ch!=0);
}


Queue:


#include<stdio.h>
void ins();
void del();
void dis();
int x,r=-1,f=-1,max,ar[5];
void main()
{
int ch;
printf("1-Insertion\n2-Deletion\n3-Display\nEnter your choice\n");
scanf("%d",&ch);
do
{
switch(ch)
{
case 1:
ins();
break;

case 2:
del();
break;

case 3:
dis(); 
break;
}
printf("Enter your choice\n");
scanf("%d",&ch);
}while(ch!=0);
}

void ins()
{
if(r==4)
{
printf("Overflow\n");
}
else
{
if(f==-1)
{
f=0;
}
for(x=0;x<=4;x++)
{
printf("Enter number\n");
scanf("%d",&ar[x]);
r=r+1;
}
}
}

void del()
{
if(f==-1 || f==5)
printf("Underflow\n");
else
{
x=ar[f];
f=f+1; 
printf("%d\n",x);
}
}

void dis()
{
if(f==-1)
{
printf("Queue is empty\n");
}
else
{
for(x=f;x<=r;x++)
{
printf("%d\n",ar[x]);
}
}
}

Post a Comment

0 Comments