1. Write a menu-driven program that implements the following operations (using separate functions) on a linear array a. Insert a new element at the beginning b. Delete an element whose value is given.
also the complexity and algorithm
Answers
Operations to be performed:
Operations to be performed:traverse(): To see the contents of the linked list, it is necessary to traverse the given linked list. The given traverse() function traverses and prints the content
Answer:
operations (using separate functions) on a linear array:
Insertion − Adds an element at the given index.
Deletion − Deletes an element at the given index.
Explanation:
a. Insert a new element at the beginning
int insert(int *a)
{
int i,n;
printf("Enter the no. of elements in array:\t");
scanf("%d",&n);
printf("\nEnter %d elements in array:\t",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
a[i]='\0';
return *a;
}
b. Delete an element
int del(int *a)
{
int c,k,posi;
for(k=0;a[k]!=NULL;k++)
{
}
printf("Enter the position to delete element:\t");
scanf("%d",&posi);
if(posi<=k)
{
for(c=posi-1;c<k-1;c++)
{
a[c]=a[c+1];
}
printf("\nArray after Deletion");
for(c=0;c<k-1;c++)
{
printf("\n%d",a[c]);
}
}
return *a;
}
* Complexity :
traverse the list to the location of deletion/insertion, which takes O(n) time. ** The deletion cost is O(log n) for the minimum or maximum, O(n) for an arbitrary element. There is some confusion in deletion in array.