/*Code for deleting from a sorted array: */
#include<stdio.h>
#include<stdlib.h>
int deleteSorted(int *a,int *size,int item);
int main()
{
int n,i;
int a[]={2,4,6,8,10};
printf("Original sorted array is:\n");
for(i=0;i<5;i++)
{
printf("%d ",a[i]);
}
printf("\nEnter element to be deleted:\n");
scanf("%d",&n);
int size=5;
deleteSorted(a,&size,n);
printf("\nAfter delete sorted array is:\n");
for(i=0;i<size;i++)
{
printf("%d ",a[i]);
}
printf("\n");
return 0;
}
int deleteSorted(int *a,int *size,int item)
{
printf("Inside deleteSorted:\n");
int i=0,j;
/*compare item with a[i] till i<size when matches come out*/
for(i=0;i<*size;i++)
{
if(a[i]==item)
{
break;
}
else if(i==*size-1)
{
printf("Not found \n");
return -1;
}
}
for(j=i;j<*size;j++)
{
a[j]=a[j+1];
}
(*size)--;
}
/*shift the elts to left*/
/*
Original sorted array is:
2 4 6 8 10
Enter element to be deleted:
2
Inside deleteSorted:
After delete sorted array is:
4 6 8 10
[user02@localhost ds]$ ./a.out
Original sorted array is:
2 4 6 8 10
Enter element to be deleted:
4
Inside deleteSorted:
After delete sorted array is:
2 6 8 10
[user02@localhost ds]$ ./a.out
Original sorted array is:
2 4 6 8 10
Enter element to be deleted:
5
Inside deleteSorted:
Not found
After delete sorted array is:
2 4 6 8 10
[user02@localhost ds]$ ./a.out
Original sorted array is:
2 4 6 8 10
Enter element to be deleted:
6
Inside deleteSorted:
After delete sorted array is:
2 4 8 10
[user02@localhost ds]$ ./a.out
Original sorted array is:
2 4 6 8 10
Enter element to be deleted:
55
Inside deleteSorted:
Not found
After delete sorted array is:
2 4 6 8 10
*/
/*
Code for deleting from a sorted array:
*/
#include<stdio.h>
#include<stdlib.h>
int deleteSorted(int *a,int *size,int item);
int main()
{
int n,i;
int a[]={2,4,6,8,10};
printf("Original sorted array is:\n");
for(i=0;i<5;i++)
{
printf("%d ",a[i]);
}
printf("\nEnter element to be deleted:\n");
scanf("%d",&n);
int size=5;
deleteSorted(a,&size,n);
printf("\nAfter delete sorted array is:\n");
for(i=0;i<size;i++)
{
printf("%d ",a[i]);
}
printf("\n");
return 0;
}
int deleteSorted(int *a,int *size,int item)
{
printf("Inside deleteSorted:\n");
int i=0,j;
/*compare item with a[i] till i<size when matches come out*/
while((a[i]!=item)&&(i<*size))
{
i++;
}
if(i==*size)
{
printf("Could not find the elet.\n");
return (-1);
}
/*shift the elts to left*/
for(j=i;j<*size-1;j++)
a[j]=a[j+1];
(*size)--;
}
/*
Original sorted array is:
2 4 6 8 10
Enter element to be deleted:
2
Inside deleteSorted:
After delete sorted array is:
4 6 8 10
[user02@localhost ds]$ ./a.out
Original sorted array is:
2 4 6 8 10
Enter element to be deleted:
4
Inside deleteSorted:
After delete sorted array is:
2 6 8 10
[user02@localhost ds]$ ./a.out
Original sorted array is:
2 4 6 8 10
Enter element to be deleted:
10
Inside deleteSorted:
After delete sorted array is:
2 4 6 8
*/
#include<stdio.h>
#include<stdlib.h>
int deleteSorted(int *a,int *size,int item);
int main()
{
int n,i;
int a[]={2,4,6,8,10};
printf("Original sorted array is:\n");
for(i=0;i<5;i++)
{
printf("%d ",a[i]);
}
printf("\nEnter element to be deleted:\n");
scanf("%d",&n);
int size=5;
deleteSorted(a,&size,n);
printf("\nAfter delete sorted array is:\n");
for(i=0;i<size;i++)
{
printf("%d ",a[i]);
}
printf("\n");
return 0;
}
int deleteSorted(int *a,int *size,int item)
{
printf("Inside deleteSorted:\n");
int i=0,j;
/*compare item with a[i] till i<size when matches come out*/
for(i=0;i<*size;i++)
{
if(a[i]==item)
{
break;
}
else if(i==*size-1)
{
printf("Not found \n");
return -1;
}
}
for(j=i;j<*size;j++)
{
a[j]=a[j+1];
}
(*size)--;
}
/*shift the elts to left*/
/*
Original sorted array is:
2 4 6 8 10
Enter element to be deleted:
2
Inside deleteSorted:
After delete sorted array is:
4 6 8 10
[user02@localhost ds]$ ./a.out
Original sorted array is:
2 4 6 8 10
Enter element to be deleted:
4
Inside deleteSorted:
After delete sorted array is:
2 6 8 10
[user02@localhost ds]$ ./a.out
Original sorted array is:
2 4 6 8 10
Enter element to be deleted:
5
Inside deleteSorted:
Not found
After delete sorted array is:
2 4 6 8 10
[user02@localhost ds]$ ./a.out
Original sorted array is:
2 4 6 8 10
Enter element to be deleted:
6
Inside deleteSorted:
After delete sorted array is:
2 4 8 10
[user02@localhost ds]$ ./a.out
Original sorted array is:
2 4 6 8 10
Enter element to be deleted:
55
Inside deleteSorted:
Not found
After delete sorted array is:
2 4 6 8 10
*/
/*
Code for deleting from a sorted array:
*/
#include<stdio.h>
#include<stdlib.h>
int deleteSorted(int *a,int *size,int item);
int main()
{
int n,i;
int a[]={2,4,6,8,10};
printf("Original sorted array is:\n");
for(i=0;i<5;i++)
{
printf("%d ",a[i]);
}
printf("\nEnter element to be deleted:\n");
scanf("%d",&n);
int size=5;
deleteSorted(a,&size,n);
printf("\nAfter delete sorted array is:\n");
for(i=0;i<size;i++)
{
printf("%d ",a[i]);
}
printf("\n");
return 0;
}
int deleteSorted(int *a,int *size,int item)
{
printf("Inside deleteSorted:\n");
int i=0,j;
/*compare item with a[i] till i<size when matches come out*/
while((a[i]!=item)&&(i<*size))
{
i++;
}
if(i==*size)
{
printf("Could not find the elet.\n");
return (-1);
}
/*shift the elts to left*/
for(j=i;j<*size-1;j++)
a[j]=a[j+1];
(*size)--;
}
/*
Original sorted array is:
2 4 6 8 10
Enter element to be deleted:
2
Inside deleteSorted:
After delete sorted array is:
4 6 8 10
[user02@localhost ds]$ ./a.out
Original sorted array is:
2 4 6 8 10
Enter element to be deleted:
4
Inside deleteSorted:
After delete sorted array is:
2 6 8 10
[user02@localhost ds]$ ./a.out
Original sorted array is:
2 4 6 8 10
Enter element to be deleted:
10
Inside deleteSorted:
After delete sorted array is:
2 4 6 8
*/
No comments:
Post a Comment