Sunday, 16 October 2011

implementation of list using array

Implementation of list using array



PROGRAM:


//DELETING  AN ELEMENT IN THE ARRAY
#include<stdio.h>
void main()
{
 int a[10],n,i,d,t;
 printf("enter the number of elements");  
 scanf("%d\n",&n);
 printf("\nenter array elements");     /*GETTING ARRAY ELEMENTS*/
for(i=0;i<n;i++)
          {
          scanf("%d\n",&a[i]);
          }
 printf("\nenter the number to be deleted");   /*DELETING THE NUMBER*/
 scanf("%d\n",&d);
for(i=0;i<n;i++)
          {
                   if(a[i]==d)
                   {
                    a[i]=a[i+1];
                    i++; 
                    n--;
                             while(i<n)
                             {
                              a[i]=a[i+1];
                             i++;
                             }
                      } 
          }
printf("\narray after deletion");
          for(i=0;i<n;i++)              /*DISPLAYING ARRAY AFTER DELETION*/
          {
          printf("%d\n",a[i]);
          }
}




OUTPUT:

enter the number of elements
5
enter array elements
3
4
11
5
8
enter the number to be deleted
5
array after deletion
3
4
11
8











                                                                         



PROGRAM:
/*INSERTING A NUMBER IN AN ARRAY*/
#include<stdio.h>
void main()
{
 int a[10],n,i,p,w,k;
 printf("enter the number of elements…");
 scanf("%d",&n);
 printf("\nenter the array elements");        /*GETTING ARRAY ELEMENTS*/
          for(i=0;i<n;i++)
          {
          scanf("%d",&a[i]);
          }
printf("\nenter the element to be inserted…");                     /*GETTING THE ELEMENT TO BE INSERTED*/
scanf("%d",&w);
printf("\nenter the position of the number…");   /*GETTING THE POSITION*/
scanf("%d",&p);
p--;
k=n;
k--;
while(k>=p)
          {
           a[k+1]=a[k];
           k--;
           }
 a[p]=w;
printf("\nnew array");
for(i=0;i<=n;i++)
          {
          printf("%d\n",a[i]);
          }
}
                               

    
  



OUTPUT:

enter the number of elements…4
enter the array elements
5
6
9
11
enter the element to be inserted…7
enter the position of the number…3
new array
5
6
7
9
11
















PROGRAM:
/*SEARCHING AN ELEMENT IN THE ARRAY*/
#include<stdio.h>
void main()
{
 int a[10],i,n,x,b=0;
 printf("enter the number of elements…");
 scanf("%d",&n);
 printf("\nenter the array elements");       /*GETTING ARRAY ELEMENTS*/
          for(i=0;i<n;i++)
          {
          scanf("%d",&a[i]);
          }
 printf("\nenter the element to be searched…");
 scanf("%d",&x);
          for(i=0;i<n;i++)                   /*SEARCHING THE ELEMENT*/
         {
                   if(a[i]==x)
                   {
                   b++;
                   break;
                   }
         }
           if(b==1)
           {
           printf("\nelement found");
           }
           else
           {
           printf("\nelement not found");
           }
}    








OUTPUT:
enter the number of elements…4
enter the array elements
6
7
31
22
enter the element to be searched…7
element found




enter the number of elements…3
enter the array elements
3
6
8
enter the element to be searched…11
element not found




















PROGRAM:
/*SORTING THE ARRAY ELEMENTS*/
#include<stdio.h>
void main()
{
 int a[10],i,n,j,t;
 printf("enter the number of elements…");
 scanf("%d",&n);
 printf("\nenter the array elements\n");    /*GETTING ARRAY ELEMENTS*/
for(i=0;i<n;i++)
          {
          scanf("%d",&a[i]);
          }
          for(i=0;i<n;i++)
          {
                   for(j=i+1;j<n;j++)
                   {
                             if(a[i]>a[j])
                             {
                             t=a[i];
                             a[i]=a[j];
                             a[j]=t;
                             }
                   }
             }
    printf("\nsorted array is\n");         /*DISPLAYING SORTED ARRAY*/
                    for(i=0;i<n;i++)
                    {
                    printf("%d\n",a[i]);
                    }
}
         






OUTPUT:
enter the number of elements…5
enter the array elements
4
1
3
6
2
sorted array
1
2
3
4
6

No comments:

Post a Comment