Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: C Program #8

Thursday, January 3, 2013

C Program #8


A program to demonstrate different operation on list using array. You might get unexpected output if two different operation is done on the list in the same run.
#include<stdio.h>
#include<stdlib.h>
void menu(void);
void create(void);
void insert(int a[],int);
void deletee(int a[],int);
void merge(int a[],int);
void split(int a[],int);
void sort(int a[],int);
void search(int a[],int);
void append(int a[],int);
int list[50],list_size,c=0;
int main()
{
menu();
return 0;
}
void menu(void)
{
while(1)
{
int choice;
printf("\n ***********MENU FOR LIST*********** \n");
printf("\n Press 0 to create \n");
printf("\n Press 1 to inserte \n");
printf("\n Press 2 to delete \n");
printf("\n Press 3 to merge \n");
printf("\n Press 4 to split \n");
printf("\n Press 5 to sort \n");
printf("\n Press 6 to search \n");
printf("\n Press 7 to append \n");
printf("\n Press 8 to exit \n");
printf("\n Enter your choice\n");
scanf("%d",&choice);
switch(choice)
     {
     case 0:
     create();
     break;
     case 1:
     insert(list,list_size);
     break;
     case 2:
     deletee(list,list_size);
     break;
     case 3:
     merge(list,list_size);
     break;
     case 4:
     split(list,list_size);
     break;
     case 5:
     sort(list,list_size);
     break;
     case 6:
     search(list,list_size);
     break;
     case 7:
   append(list,list_size);
     break;
     case 8:
     exit(0);
     break;
     default:
     puts("Wrong choice");
     break;
     }
}
return;
}
void split(int list[],int list_size)
{
     if(c==0)
          {
              puts(" LIST NOT CREATED, CREATE LIST FIRST ");
             
          }
     else{
     int b[25],i,j,pos;
   printf("\n List items are: \n");
     for(i=0;i<list_size;i++)
     printf("\t %d",list[i]);
     printf("\n Enter the position at which the list will be splitted:\n");
     scanf("%d",&pos);
   printf("\n List after splitting:\n");
     printf("\n List #1 \n");
     for(i=pos;i<list_size;i++)
          {
          b[i-pos]=list[i];
          printf("\t %d",b[i-pos]);
          }
     printf("\n List #2 \n");
     for(i=0;i<pos;i++)
     printf("\t %d",list[i]);
     list_size=list_size-pos;
   }
}
void create(void)
{
     c++;
   int i;
     printf("\n Enter the list size \n");
     scanf("%d",&list_size);
     printf("\n Enter the elements:\n");
     for(i=0;i<list_size;i++)
          scanf("%d",&list[i]);
}
void insert(int list[],int list_size)
{
     int item,pos,i;
   if(c==0)
          {
              puts(" LIST NOT CREATED, CREATE LIST FIRST ");
             
          }
     else{
     printf("\n Enter the item to be inserted and position\n");
     scanf("%d %d",&item,&pos);
     printf("\n List before insertion \n");
     for(i=0;i<list_size;i++)
          printf("\t %d",list[i]);
     for(i=list_size;i>pos-1;i--)
          list[i]=list[i-1];
          list[pos-1]=item;
     list_size+=1;
     printf("\n List after insertion \n");
     for(i=0;i<list_size;i++)
          printf("\t %d",list[i]);
     }
return ;
}
void deletee(int list[],int list_size)
{
     int item,pos,flag=1,i;
   if(c==0)
          {
              puts(" LIST NOT CREATED, CREATE LIST FIRST ");
             
          }
     else{
     printf("\n Enter the item to be deleted:\n");
     scanf("%d",&item);
     for(i=0;i<list_size;i++)
          {
              if(item==list[i])
                   {
                   pos=i;
                   flag=1;
                   break;
                   }
           else
           flag=0;
           }
     if(flag==0)
              printf("\n Item not found, cannot be deleted:\n");
     else
     {
     printf("\n List before deletion \n");
     for(i=0;i<list_size;i++)
          printf("\t %d",list[i]);
     for(i=pos;i<list_size;i++)
          list[i]=list[i+1];
          list_size-=1;
     printf("\n List after deletion \n");
     for(i=0;i<list_size;i++)
          printf("\t %d",list[i]);
     }
     return ;
     }
}
void merge(int list[],int list_size)
{
     int a[50],n,i;
   if(c==0)
          {
              puts(" LIST NOT CREATED, CREATE LIST FIRST ");
             
          }
     else{
     printf("\n Enter the size of the list to be merged \n");
     scanf("%d",&n);
     printf("\n Enter the items of the new list:\n");
     for(i=0;i<n;i++)
     scanf("%d",&a[i]);
     for(i=list_size;i<list_size+n;i++)
     list[i]=a[i-list_size];
     printf("\n List before merging \n");
     for(i=0;i<list_size;i++)
          printf("\t %d",list[i]);
     list_size+=n;
     printf("\n List after merging \n");
     for(i=0;i<list_size;i++)
          printf("\t %d",list[i]);
      }
     return;
}
void sort(int list[],int list_size)
{
     int i,temp,j;
   if(c==0)
          {
              puts(" LIST NOT CREATED, CREATE LIST FIRST ");
             
          }
     else{
     printf("\n List before sorting \n");
     for(i=0;i<list_size;i++)
          printf("\t %d",list[i]);
     for(i=0;i<list_size;i++)
          {
              for(j=i;j<list_size-1;j++)
                   {
                        if(list[i]<list[j])
                             {
                                  temp=list[i];
                                  list[i]=list[j];
                                  list[j]=temp;
                             }
                   }
          }
     printf("\n List after sorting \n");
     for(i=0;i<list_size;i++)
          printf("\t %d",list[i]);
     }
     return;
}
void search(int list[],int list_size)
{
     int item,flag=1,i,count=0,a[10];
   if(c==0)
          {
              puts(" LIST NOT CREATED, CREATE LIST FIRST ");
             
          }
     else{
     printf("\n List items are: \n");
     for(i=0;i<list_size;i++)
          printf("\t %d",list[i]);
     printf("\n Enter the item to be searched:\n");
     scanf("%d",&item);
     for(i=0;i<list_size;i++)
          {
              if(item==list[i])
                   {
                   a[count]=i;
                   count++;
                   flag=1;
                   continue;
                   }
           else
           flag=0;
           }
     if(flag==0 && count==0)
              printf("\n Item not found:\n");
     else
          {
              printf("\n Item %d is at postion",item);
              for(i=0;i<count;i++)
                   printf("\t %d",a[i]+1);
          }
     }
     return;
}
void append(int list[],int list_size)
{
     int item,pos,i;
   if(c==0)
          {
              puts(" LIST NOT CREATED, CREATE LIST FIRST ");
             
          }
     else{
     printf("\n List items are: \n");
     for(i=0;i<list_size;i++)
          printf("\t %d",list[i]);
     printf("\n Enter the item to be appended & position:\n");
     scanf("%d %d",&item,&pos);
     list[pos-1]=item;
     printf("\n List after appending: \n");
     for(i=0;i<list_size;i++)
          printf("\t %d",list[i]);
     }
     return;
}


No comments:

Post a Comment