The Programming Project

Wednesday, February 13, 2013

C Program #10

/* Circular Link List */

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct circular_list
{
int d;
struct circular_list *next;
};
typedef struct circular_list node;
void create(node *);
void display(node *,node *);
node *search(node *,node *);
void insert(node *,node *);
void displaypos(node *,node *);
int main()
{
int choice;
node *root,*head,*tag;
root=(node *)malloc(sizeof(node));
head=root;
printf("\n ***********************MENU FOR LIST******************* \n");
printf("\n Press 1 to create:");
printf("\n Press 2 to display:");
printf("\n Press 3 to search:");
printf("\n Press 4 to insert");
printf("\n Press 5 to display with respect to an element in the list:");
printf("\n Press 6 to exit \n");
while(1)
{
printf("\n Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
create(root);
break;
case 2:
display(root,head);
break;
case 3:
tag=search(root,head);
if(tag==NULL)
printf("\n Element not found:\n");
else
printf("\n The element is %d",tag->d);
break;
case 4:
insert(root,head);
break;
case 5:
displaypos(root,head);
break;
case 6:
exit(0);
break;
default:
break;
}
}
return 0;
}
void create(node *record)
{
int n,i;
node *p;
p=record;
printf("\n Enter the number of elements to be added:");
scanf("%d",&n);
printf("\n Enter the elements:\n");
scanf("%d",&record->d);
for(i=1;i<=n-1;i++)
{
record->next=(node *)malloc(sizeof(node));
record=record->next;
scanf("%d",&record->d);
}
record->next=p;
return;
}
void display(node *record,node *head)
{
printf("\n List elements are:\n");
while(1)
{
printf(" %d ",record->d);
record=record->next;
if(record==head)
break;
}
return;
}
node *search(node *record,node *head)
{
display(record,head);
node *p;
int n,flag=0;
printf("\n Enter the item to be searched:");
scanf("%d",&n);
while(record->next!=NULL)
{
if(record->d==n)
{
flag=1;
p=record;
break;
}
record=record->next;
if(record==head)
          break;
}
if(flag==1)
return (p);
else
return (NULL);
}
void insert(node *record,node *head)
{
node *pos,*p,*temp;
printf("\n Search the element after which the item has to be inserted:");
pos=search(record,head);
if(pos==NULL)
{
printf("\n Element cannot be inserted:");
}
else
{
p=(node *)malloc(sizeof(node));
printf("\n Enter the item to be inserted:");
scanf("%d",&p->d);
temp=pos->next;
pos->next=p;
p->next=temp;
}

}
void displaypos(node *record,node *head)
{
node *tag,*p;
printf("\n Search the item w.r.t which the list will be displayed:");
tag=search(record,head);
if(tag==NULL)
{
printf("\n List Cannot be displayed:\n");
}
else
{
p=tag;
printf("\n List with respect to %d",tag->d);
while(tag->next!=p)
{
printf(" %d ",tag->next->d);
tag=tag->next;
}
}
return;
}

C Program #9

/*Doubly Linked List */

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct list_element
{
int d;
struct list_element *next,*prev;
};
typedef list_element node;
void create(node *);
void displayn(node *);
void displayr(node *);
node *search(node *);
node *inserteb(node *);
void insertel(node *);
void inserte(node *);
node *deletef(node *);
void deletee(node *);
void deleteb(node *);
int main()
{
int choice;
node *root,*record,*tag ;
record=(node *)malloc(sizeof(node));
root=record;
printf("\n ***********************MENU FOR LIST******************* \n");
printf("\n Press 1 to create:");
printf("\n Press 2 to display list in forward drection:");
printf("\n Press 3 to display list in reverse direction:");
printf("\n Press 4 to search in the list:");
printf("\n Press 5 to inserte at the begining of the list:");
printf("\n Press 6 to inserte at the end of the list:");
printf("\n Press 7 to inserte between first and last element:");
printf("\n Press 8 to delete the first element:");
printf("\n Press 9 to delete the last elemnt:");
printf("\n Press 10 to delete between the last and first elements:");
printf("\n Press 11 to exit:");
while(1)
{
printf("\n Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
create(root);
break;
case 2:
displayn(root);
break;
case 3:
displayr(root);
break;
case 4:
tag=search(root);
if(tag==NULL)
printf("\n Element not found:\n");
else
printf("\n The element is %d",tag->d);
break;
case 5:
root=inserteb(root);
break;
case 6:
insertel(root);
break;
case 7:
inserte(root);
break;
case 8:
root=deletef(root);
break;
case 9:
deletee(root);
break;
case 10:
deleteb(root);
   break;
case 11:
exit(0);
break;
default:
break;
}
}
return 0;
}
void create(node *record)
{
int i,n;
node *p;
printf("\n Enter the number of elements:");
scanf("%d",&n);
printf("\n Enter the elements:\n");
scanf("%d",&record->d);
record->prev=NULL;
for(i=1;i<=n-1;i++)
{
p=record;
record->next=(node *)malloc(sizeof(node));
record=record->next;
scanf("%d",&record->d);
record->prev=p;
}
record->next=NULL;
return;
}
void displayn(node *record)
{
 printf("\n");
while(record!=NULL)
{
printf("\t %d",record->d);
record=record->next;
}


}
void displayr(node *record)
{
printf("\n");
while(record->next!=NULL)
{
 // printf("\t %d",record->d);
record=record->next;
}
printf("\n");
while(record!=NULL)
{
printf("\t %d",record->d);
record=record->prev;
}
}
node *search(node *record)
{
int n,flag=0;
node *p;
printf("\n Enter the element to be searched:\n");
scanf("%d",&n);
while(record!=NULL)
{
if(record->d==n)
{
flag=1;
p=record;
break;
}
record=record->next;
}
if(flag==1)
return (p);
else
return (NULL);
}
node *inserteb(node *record)
{
node *p;
p=(node *)malloc(sizeof(node));
printf("\n Enter the element to be inserted:");
scanf("%d",&p->d);
p->next=record;
record->prev=p;
p->prev=NULL;
return (p);
}
void insertel(node *record)
{
node *p;
p=(node *)malloc(sizeof(node));
printf("\n Enter the element to be inserted:");
scanf("%d",&p->d);
while(record->next!=NULL)
{
record=record->next;
}
record->next=p;
p->next=NULL;
p->prev=record;
}
void inserte( node *record)
{
node *p,*tag,*pv;
displayn(record);
printf("\n Enter the element after which insertion has to be done:");
tag=search(record);
if(tag==NULL)
{
printf("\n Cannot be inserted");
}
else
{
pv=tag;
p=(node *)malloc(sizeof(node));
printf("\n Enter the element to be inserted:");
scanf("%d",&p->d);
p->next=tag->next;
tag->next=p;
p->prev=pv;
p->next->prev=p;

}
}
node *deletef(node *record)
{
node *p;
p=record->next;
p->prev=NULL;
free(record);
return(p);
}
void deletee(node *record)
{
 node *p;
while(record->next!=NULL)
{
record=record->next;
}
p=record->prev;
p->next=NULL;
p->prev=record->prev->prev;
   free(record);
}
void deleteb(node *record)
{
 node *p,*tag;
 displayn(record);
 printf("\n Item to be deleted, should be searched:\n");
 tag=search(record);
 if(tag==NULL)
printf("\n Element not found, try again");
 else
{
p=tag->prev;
p->next=tag->next;
p->next->prev=p;
free(tag);
}
}

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;
}

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;
}