Write a C program to implement various operations of linear list using arrays.

Leave a Comment
#include <stdio.h>
#include<stdlib.h>

#include<conio.h>

void insert(int a[], int n)
{int i;
printf("\nEnter the elements of the array!!");
for(i=0;i<n;i++)

 {
printf("\nEnter element %d:",i+1);
scanf("%d",&a[i]);
}
}
void print(int a[], int n)
{int i;

printf("\nThe elements of the array are!!");
for(i=0;i<n;i++)

 {
printf("%d\t",a[i]);
}
}
void ghusa(int a[],int *n, int loc)
{
loc--;
int t=*n;
while(t>loc)
{
a[t]=a[t-1];
t--;
}
(*n)--;
printf("\nNow enter the element:");
scanf("%d",&t);
a[loc]=t;
}
void del(int a[],int *n, int loc)
{
loc--;
while(*n>loc)
{
a[loc]=a[loc+1];
loc++;
}
(*n)--;
}
void sreach(int a[],int n)
{int i;
int t,flag=0;
printf("\nEnter the search element:");
scanf("%d",&t);
for(i=0;i<n;i++)

 {
if(a[i]==t)
{
flag=1;
break;
}
}
if(flag==0)
printf("\nThe element not found!!!!");
else
printf("\nThe element found at location %d", i+1);
}
void asc_sort(int a[],int n)
{
int i,j;
int t;
for(i=0;i<n;i++)

 {
for(j=0;j<n-i;j++)

 {
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
}
void dec_sort(int a[],int n)
{
int i,j;
int t;
for(i=0;i<n;i++)

{
for(j=0;j<n-i;j++)

{
if(a[j] {
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
}

int main()
{
int a[100],n;
printf("\nEnter the array size:");
scanf("%d",&n);

int o,loc;
while(o!=0)
{
printf("\nENTER YOUR OPTION!!\n");
printf("\nEnter 1 for INSERTION");
printf("\nEnter 2 for DELETION");
printf("\nEnter 3 for ADDING ELEMENT");
printf("\nEnter 4 for SORTING(ascending)");
printf("\nEnter 5 for SORTING(decending)");
printf("\nEnter 6 for PRINTING");
printf("\nEnter 7 for SEARCHING");
printf("\nEnter 0 for exit!!");
scanf("%d",&o);
switch(o)
{
case 1:{
insert(a,n);
break;
}
case 2:{
printf("\nEnter the location of the element to be deleted:");
scanf("%d",&loc);
del(a,&n,loc);
break;
}
case 3:{
printf("\nEnter the location of where u wanna insert:");
scanf("%d",&loc);
ghusa(a,&n,loc);
break;
}
case 4:{
asc_sort(a,n);
break;
}
case 5: {dec_sort(a,n);
break;
}
case 6:{print(a,n);break;
}
case 7:{sreach(a,n);break;}
case 0:break;
default:printf("\nU have entered the wrong choice!!!!!");

}
}
getche();
}

0 comments: