Sunday, 25 September 2016

DS experiment 11

algorithm to add an element
1. Start
2. Set J=N
3. Set N = N+1
4. Repeat steps 5 and 6 while J >= K
5. Set LA[J+1] = LA[J]
6. Set J = J-1
7. Set LA[K] = ITEM
8. Stop
 
program 
 

#include<iostream.h>
#include<conio.h>

 void main()
  {
  int i,a[5],no,pos;
  clrscr();
  cout<<"Enter data in Array: ";
  for(i=0;i<5;i++)
  {
  cin>>a[i];
  }
  cout<<"\n\nStored Data in Array: ";
  for(i=0;i<5;i++)
  {
  cout<<a[i];
  }
  cout<<"\n\nEnter position to insert number: ";
  cin>>pos;
  if(pos>5)
  {
  cout<<"\n\nThis is out of range";
  }
  else
  {
  cout<<"\n\nEnter new number: ";
  cin>>no;
  --pos;
  for(i=5;i>=pos;i--)
  {
  a[i+1]=a[i];
  }
  a[pos]=no;
  cout<<"\n\nNew data in Array: ";
  for(i=0;i<6;i++)
  {
  cout<<a[i];
  }
  }
  getch();
  } 

 
algorithm to delete an element 
1. Start
2. Set J=K
3. Repeat steps 4 and 5 while J < N
4. Set LA[J-1] = LA[J]
5. Set J = J+1
6. Set N = N-1
7. Stop 
 
program

#include<iostream.h>
#include<conio.h>

void main()
  {
   int i,a[5],no,pos;
   clrscr();
   cout<<"Enter data in array: ";
   for(i=0;i<5;i++)
   {
    cin>>a[i];
   }
   cout<<"\n\nStored Data in array:  ";
   for(i=0;i<5;i++)
   {
    cout<<a[i];
   }
   cout<<"\n\nEnter poss. of element to delete: ";
   cin>>pos;
   if(pos>5)
   {
   cout<<"\n\nThis value is out of range: ";
   }
   else
   {
   --pos;
   for(i=pos;i<=4;i++)
   {
    a[i]=a[i+1];
   }
   cout<<"\n\nNew data in array: ";
   for(i=0;i<4;i++)
   {
    cout<<a[i];
  }
  }
   getch();
 }
 
 

algorithm to search an element
1. Start
2. Set J=0
3. Repeat steps 4 and 5 while J < N
4. IF LA[J] is equal ITEM THEN GOTO STEP 6
5. Set J = J +1
6. PRINT J, ITEM
7. Stop
 
flowchart
FlowChart_Search_List 

program
 #include<iostream.h>
 #include<conio.h>

 int linear_search(int [],int,int);

 main()
    {
       clrscr();

       constint array_size=10;
       int array[array_size]={0};

       cout<<"\n Enter the contents of the array are : "<<endl;

       cout<<"\n     Elements :"<<"\t\t     Value:"<<endl;

       for(int count=0;count<array_size;count++)
      {
         cout<<"\t"<<" array ["<<count<<"]"<<"\t\t";
         cin>>array[count];
      }

       int searching_element=0;
       int flag=0;

       cout<<"\n\n Enter the element you want to find  =  ";
       cin>>searching_element;

       flag=linear_search(array,array_size,searching_element);

       if(flag!=-1)
      cout<<"\n The given element is found at the position  array["<<
                            flag<<"]"<<endl;

       else
      cout<<"\n The given element is not found. "<<endl;

       getch();
       return 0;
    }

 /*************************************************************************///--------------------  linear_search(int [],int,int)  ------------------///*************************************************************************/int linear_search(int array[],int size,int element)
    {
       for(int count=0;count<size;count++)
      {
         if(element==array[count])
        {
           return count;
        }
      }

       return -1;
    }

No comments:

Post a Comment