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. 


#include<stdio.h>
#include<stdlib.h>
#include<malloc.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 c=1;
int main()
{
int i,*list,*list_size,n;
printf("\n Enter the list size \n");
scanf("%d",&n);
list_size=&n;
list=(int *)malloc(n*sizeof(int));
printf("\n Enter the elements:\n");
for(i=0;i<n;i++)
scanf("%d",(list+i));
while(1)
{
int choice;
printf("\n ***********MENU FOR LIST*********** \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 1:
insert(list,&n);
break;
case 2:
deletee(list,&n);
break;
case 3:
merge(list,&n);
break;
case 4:
split(list,&n);
break;
case 5:
sort(list,&n);
break;
case 6:
search(list,&n);
break;
case 7:
append(list,&n);
break;
case 8:
exit(0);
break;
default:
puts("Wrong choice");
break;
}
}
return 0;
}
void insert(int *list,int *list_size)
{
int item,pos,i;
printf("\n List before insertion \n");
for(i=0;i<*list_size;i++)
   printf("\t %d",list[i]);
printf("\n Enter the item to be inserted and position\n");
scanf("%d %d",&item,&pos);
if(pos>*list_size)
{
puts(" Item cannot be inserted");
}
else{
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;
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;
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 split(int *list,int *list_size)
{
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=pos;
return;
}
void sort(int *list,int *list_size)
{
int i,temp,j;
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];
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;
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);
if(pos>*list_size)
{
puts(" Item cannot be appended");
}
else{
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