#include<stdio.h>
#include<conio.h>
int n,a[10];
void display();
void insert(int);
int del();
int main()
{
int i,item=0,j;
n=1;
printf("enter the numbers:");
for(i=1;i<6;i++)
scanf("%d",&a[i]);
for(j=1;j<5;j++)
{
insert(a[j+1]);
} }
while(n>1)
{
item=del();
a[n+1]=item;
}
display(a);
getch();
}
void display()
{
int i;
for(i=1;i<6;i++)
{
printf("%d ",a[i]);
}
}
void insert(int item1)
{
int ptr,par;
n++;
ptr=n;
while(ptr>1)
{
par=ptr/2;
if(item1<a[par])
{
a[ptr]=item1;
display(a);
return;
}
a[ptr]=a[par];
ptr=par;
}
a[1]=item1;
display(a);
}
int del()
{
int item,left,right,ptr,last;
item=a[1];
last=a[n];
n--;
ptr=1;
left=2;
right=3;
while(right<=n)
{
if(last>a[left] && last>a[right])
{
a[ptr]=last;
return(item);
}
if(a[left]>a[right])
{
a[ptr]=a[left];
ptr=left;
}
else
{
a[ptr]=a[right];
ptr=right;
}
left=ptr*2;
right=left+1;
}
if(left==n && last<a[left])
{
ptr=left;
a[ptr]=last;
}
return(item);
}
please tell me why is it not working..? there is no syntax error. and insertion is going very well. but facing problem when deletion is there. any help will be appreciated.
thanx.
(m just a beginner)