Friday, March 11, 2016

C program to show add, append, sort, delete, display, search operation on array

#include<iostream.h>
#include<conio.h>
class List
 {
  int a[50];
  int c;
  public:
   void add(int x);
   void deleteall();
   int delete_index(int index);
   void delete_number(int x);
   int search(int x);
   void insert(int x,int index);
   void sort();
   void append(int x);
   void display();
   int get(int index);
   List();
 };
void List::List()
 {
  c=-1;
 }
void List::add(int x)
 {
  c++;
  a[c]=x;
 }
void List::deleteall()
 {
  int i;
  for(i=0;i<=c;i++)
   {
    a[i]=NULL;
   }
  c=-1;
 }
int List::delete_index(int index)
 {
  if(index>c)
   {
    cout<<"\nIndex is out of bound";
    return -1;
   }
  else
   {
    int r=a[index];
    int i;
    for(i=index;i<c;i++)
     {
      a[i]=a[i+1];
     }
    c--;
    return r;
   }
 }
void List::delete_number(int x)
 {
  int i;
  for(i=0;i<=c;i++)
   {
    if(a[i]==x)
     {
      break;
     }
   }
  if(i<=c)
   {
    for(int k=i;k<c;k++)
     {
      a[k]=a[k+1];
     }
     c--;
     cout<<"\nNumber deleted at index "<<i;
   }
  else
   {
    cout<<"\nEntered number not present";
   }
 }
int List::search(int x)
 {
  int i;
  for(i=0;i<=c;i++)
   {
    if(a[i]==x)
     {
      break;
     }
   }
  if(i<=c)
   {
    return i;
   }
  else
   {
    return -1;
   }
 }
void List::insert(int x,int index)
 {
  int i;
  if(index>(c+1))
   {
    cout<<"\nList is shorter";
   }
  else
   {
    for(i=c;i>=index;i--)
     {
      a[i+1]=a[i];
     }
    a[index]=x;
    c++;
   }
 }
void List::sort()
 {
  int i;
  int j;
  int temp;
  for(i=0;i<c;i++)
   {
    for(j=0;j<c-i;j++)
     {
      if(a[j]>a[j+1])
       {
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
       }
     }
   }
 }
void List::append(int x)
 {
  c++;
  a[c]=x;
 }
void List::display()
 {
  int i;
  for(i=0;i<=c;i++)
   {
    cout<<"\t"<<a[i];
   }
 }
int List::get(int index)
 {
  return (a[index]);
 }
void main()
 {
  int ch;
  int no,in,x;
  List List_object;
  do
   {
    cout<<endl<<"1.Add an item\n 2.Delete All \n3.Delete at perticular index\n4.Delete perticular number \n5.Search a number \n6.Insert a number at perticular index \n7.Sort List \n8.Append an item \n9.Display list \n10.Get item at perticular index";
    cin>>ch;
    switch(ch)
     {
      case 1:
       cout<<"\nEnter a number";
       cin>>no;
       List_object.add(no);
       break;
      case 2:
       List_object.deleteall();
       break;
      case 3:
       cout<<"\nEnter Index to delete";
       cin>>in;
       x=List_object.delete_index(in);
       cout<<"\nDeleted number="<<x;
       break;
      case 4:
       cout<<"\nEnter number to delete";
       cin>>no;
       List_object.delete_number(no);//Bool
       break;
      case 5:
       cout<<"\nEnter number to search";
       cin>>no;
       x=List_object.search(no);
       if(x==-1)
{
cout<<"\nNumber not present";
}
       else
{
cout<<"\nNumber present at "<<x;
}
       break;
      case 6:
       cout<<"\nEnter no and index";
       cin>>no>>in;
       List_object.insert(no,in);
      break;
     case 7:
       List_object.sort();
       List_object.display();
       break;
     case 8:
       cout<<"\nEnter a no to append";
       cin>>no;
       List_object.append(no);
       break;
     case 9:
       List_object.display();
       break;
     case 10:
       cout<<"\nEnter index to find number";
       cin>>in;
       x=List_object.get(in);
       cout<<"\nNumber is "<<x;
     }
   }while(ch!=11);
  getch();
 }

output
 1.Add an item
 2.Delete All 
 3.Delete at perticular index
 4.Delete perticular number 
 5.Search a number 
 6.Insert a number at perticular index 
 7.Sort List 
 8.Append an item 
 9.Display list 
 10.Get item at perticular index
 1
 Enter a number 6
 1.Add an item
 2.Delete All 
 3.Delete at perticular index
 4.Delete perticular number 
 5.Search a number 
 6.Insert a number at perticular index 
 7.Sort List 
 8.Append an item 
 9.Display list 
 10.Get item at perticular index
 8
 Enter a no to append 2
 1.Add an item
 2.Delete All 
 3.Delete at perticular index
 4.Delete perticular number 
 5.Search a number 
 6.Insert a number at perticular index 
 7.Sort List 
 8.Append an item 
 9.Display list 
 10.Get item at perticular index
 9
 6 2
 1.Add an item
 2.Delete All 
 3.Delete at perticular index
 4.Delete perticular number 
 5.Search a number 
 6.Insert a number at perticular index 
 7.Sort List 
 8.Append an item 
 9.Display list 
 10.Get item at perticular index 
 5 
 Enter number to search 8
 Number not present
 1.Add an item
 2.Delete All 
 3.Delete at perticular index
 4.Delete perticular number 
 5.Search a number 
 6.Insert a number at perticular index 
 7.Sort List 
 8.Append an item 
 9.Display list 
 10.Get item at perticular index 
 11

No comments:

Post a Comment