hi

  this is linked list program




    #include<stdio.h>
   #include<conio.h>
   typedef struct  node
   {
 int info;struct node *next;
 }node;
 void del(node **head);
 void create_ll(node **start);
 void insertion_at_big(node **first,int item) ;
 void insertion_at_end(node **end,int item);
 void insertion_after_given_no(node **,int after,int item);
 void insertion_before_given_element(node **,int item,int before);
 node *search(node **,int after);
 void deletion_from_beg(node **);
 void deletion_from_end(node **);
 void cir_display(node **head) ;
 void deletion_from_end_in_cll(node **head);
 void merged_2sorted_list(node **head1,node **head2);
 void insertion_at_big_in_2ll(node **first,int item);
 void inserion_in_sorted(node **head,int item);
 void display(node **start);
 void insertion_at_end_in_cirll(node **head,int item);
 void insertion_at_beg_in_circular(node **head,int item);
 void sort(node **head);
 void bubble_sort(node **head);
 void copy_of_ll(node **head);
 void dletion_of_same_data(node **head);
 void deletion_after_element_in_cll(node **head);
 void main()
 {
  int item,ans,after,before; node *head,*head1;
  create_ll(&head);
  create_ll(&head1);
  do
  {
 clrscr();
 printf("1:insertion at begning\t2:insertion at end\n3:traverse\t4:exit\n");
 printf("5:insertion after a given list\t");
 printf("6:insertion before a given list \n");
 printf("7:insertion after a given list in sorted list\t8:Deletion from 
begning\n");
 printf("9:deletion from end\t10:deletion after a particular 
element\n11:deletion before a element\n");
 printf("12 insertion at beg in cir ll\t13:display of cir \n14:insertion in end 
in cll\n");
 printf("15:sorting\n16:insertion in sorted array\n17:bubble sorting\n18:copy 
of ll\n");
 printf("19:deletion of same data\n20:insertion in second 
ll\n21:merging\n22:dele from end in ccll\n");
 printf("\n23: deletion after in cll\tEnter ur choice");
 scanf("%d",&ans);
 switch(ans)
  {
  case 1:
   printf("enter item for insertion");
   scanf("%d",&item);
   insertion_at_big(&head,item);
   break;
  case 2:
    printf("enter item");
    scanf("%d",&item);
    insertion_at_end(&head,item);
    break;
    case 3:
  display(&head);
  break;
    case 4:
    exit(0);
    case 5:
  printf("enter item for insertion");
  scanf("%d",&item);
  insertion_after_given_no(&head,after,item);
  break;
    case 6:
  printf("enter item for insertion");
  scanf("%d",&item);
  insertion_before_given_element(&head,item,before);
   break;
    case 8:
    deletion_from_beg(&head);break;
    case 9:
    deletion_from_end(&head);break;
    case 12:
    printf("enter item for insertion");
  scanf("%d",&item);
  insertion_at_beg_in_circular(&head,item);
  break;
  case 13:
  cir_display(&head);
  break;
  case 14:
  printf("enter item for insertion");
  scanf("%d",&item);
  insertion_at_end_in_cirll(&head,item);
  break;
  case 15:
  sort(&head);
  break;
    case 16:
    printf("enter item");
    scanf("%d",&item);
  inserion_in_sorted(&head,item);
  break;
  case 17:
   bubble_sort(&head) ;
   break;
  case 18:
   copy_of_ll(&head);
   break;
    case 19:
    dletion_of_same_data(&head);
    break;
    case 20:
    printf("enter item");
    scanf("%d",&item);
    insertion_at_big_in_2ll(&head1,item);
    break;
    case 21:
    merged_2sorted_list(&head,&head1) ;
    break;
   case 22:
   deletion_from_end_in_cll(&head);
   case 23:
    deletion_after_element_in_cll(&head);
    break;
    case 25:
  del(&head);
  break;
    default:
  printf("wrong choice");
  break;
  }
  }while(ans!=4);
  getch();
}
 void create_ll(node **start)
  {
   *start =NULL;
  }
 void insertion_at_big(node **first,int item)
{
 node *ptr;
 ptr= (node*)malloc(sizeof(node));
 ptr->info=item;
 ptr->next=NULL;
 if(first==NULL)
 *first=ptr;
 else
 {
 ptr->next=*first;
 *first=ptr;
   }
 }
 void insertion_at_end(node **end,int item)
 {
  node *ptr,*first;
 ptr= (node*)malloc(sizeof(node));
 ptr->info=item;
 ptr->next=NULL;
 if(*end==NULL)
 *end=ptr;
 else
 {
 first = *end;
 while(first->next!=NULL)
  first=first->next;
   first->next=ptr;
  }
 }

  void display(node **start)
  {
  node *p;
 p=*start;
 clrscr();
  while(p!=NULL)
  {
   printf("%d\n",p->info);
   p=p->next;
 }
 getch();
}
void insertion_after_given_no(node **head,int after,int item)
{
  node *ptr,*loc;
  ptr = (node*)malloc(sizeof(node));
  printf("enter after's value");
  scanf("%d",&after);
  loc = search(head,after);
  if(loc==NULL)
 printf("no. not present");
 else
 {
 ptr->next=loc->next;
 loc->next=ptr;
 ptr->info=item;
 }
}
void insertion_before_given_element(node **head,int item,int before)
{
 node *ptr,*loc;
  printf("enter before's value");
  scanf("%d",&before);
  ptr = (node*)malloc(sizeof(node));
  loc=search(head,before);
  if(loc==NULL)
 printf("no. not present");
 else
 {
 ptr->next=loc;
 loc= *head;
 while((loc->next)->next != ptr->next)
 loc=loc->next;
 (loc->next)->next=ptr;
   //loc->next->next=ptr;
   ptr->info=item;
 }
}
node* search(node **head,int after)
{
 node *loc;
 loc= *head;
 while(loc->info!=after&&loc!=NULL)
  loc=loc->next;
 return loc;
}
void deletion_from_beg(node **head)
{
 node *ptr; int item;
 if(*head==NULL)
 printf("no element");
 else if(((*head)->next)==NULL)
 {
  ptr= *head;
  printf("deleted item is %d",ptr->info);
  head=NULL;
  free(ptr);
  }
 else
  {
  ptr=*head;
  *head=ptr->next;
  printf("deleted item is %d",ptr->info);
    free(ptr);
  }
  printf("%d",sizeof(ptr));
  ptr=NULL;
   printf("\n%d",sizeof(ptr));
   printf("%d",ptr->info);
  getch();
 }
void deletion_from_end(node **end)
 {
  node *ptr,*last;
  ptr= *end;
  //if( ptr->next==NULL)
  while(ptr->next->next!=NULL)
 ptr=ptr->next;
 last=ptr;
 ptr=ptr->next;
 last->next=NULL;
 printf("deleted element is %d",ptr->info);
 free(ptr);
 getch();
  }
 void insertion_at_beg_in_circular(node **head,int item)
 {
  node *ptr,*loc;
  ptr=(node*)malloc(sizeof(node));
  loc=*head;
  ptr->info=item;
  if(loc==NULL)
  {
   *head=ptr;
   ptr->next=ptr;
  }
 else
 {
  loc=*head;
  while(loc->next!= *head)
  loc=loc->next;
  ptr->next=*head;
  *head=ptr;
  loc->next=ptr;
 }
}
void cir_display(node **head)
{
 node *ptr;
 ptr=*head;
   //while(ptr!=*head)
   do
 {
 printf("%d ",ptr->info);
 ptr=ptr->next;
 }
 while(ptr!=*head);
  getch();
}
void insertion_at_end_in_cirll(node **head,int item)
{
 node *ptr,*lastn;
 ptr=(node*)malloc(sizeof(node));
  ptr->info=item;
  if(*head==NULL)
  {
   *head=ptr;
   ptr->next=ptr;
  }
  else
  {
   lastn=*head;
   while(lastn->next!= *head)
   lastn=lastn->next;
   ptr->next=*head;
   lastn->next=ptr;
   }
  }
void sort(node **head)
{
 node *ptr,*ptr1;
 int temp;
 ptr = *head;
 while(ptr->next!=NULL)
 {
  ptr1=ptr->next;
  while(ptr1!=NULL)
  {
   if(ptr->info > ptr1->info)
   {
 temp=ptr->info;
 ptr->info=ptr1->info;
 ptr1->info=temp;
 }
 ptr1=ptr1->next;
   }
  ptr=ptr->next;
  }
}
 void inserion_in_sorted(node **head,int item)
 {
  node *ptr,*ptr1,*loc;
  ptr=(node*)malloc(sizeof(node));
  ptr->info=item;
  ptr->next=NULL;
  if(*head==NULL)
  *head=ptr;
  else
   {
 ptr1= *head;
 while(ptr1->info < item && ptr1->next!=NULL)
 { loc=ptr1;
 ptr1=ptr1->next;
 }
 ptr->next=loc->next;
 loc->next=ptr;
 }
  }
 void bubble_sort(node **head)
 {
  node *ptr,*ptr1;
  int item;
  ptr=(*head)->next;
  while(ptr!=NULL)
  {
   ptr1=*head;
   while(ptr1->next!=NULL)
   {
 if(ptr1->info > (ptr1->next)->info)
 {
  item=ptr1->info;
  ptr1->info=(ptr1->next)->info;
 (ptr1->next)->info=item;
 }
   ptr1=ptr1->next;
  }
  ptr=ptr->next;
 }
}
void copy_of_ll(node **head)
{
 node *ptr,*head1,*ptr1,*head2;
 head1= (*head)->next;
 ptr1->info=(*head)->info;
 ptr1->next=NULL;
 head2=ptr1;
 clrscr();
 while(head1!=NULL)
 {
 ptr=(node*)malloc(sizeof(node));
 ptr->info=head1->info;
 ptr1->next=ptr;
 ptr1=ptr1->next;
 ptr->next=NULL;
  head1=head1->next;
  }
 head1=head2;
 while(head1!=NULL)
 {
  printf(" %d",head1->info) ;
  head1=head1->next;
 }
 getch();
}
 void dletion_of_same_data(node **head)
 {
  node *ptr,*ptr2;int item;
  for(ptr=*head;ptr!=NULL;ptr=ptr->next)
  {
   item=ptr->info;
   for(ptr2=ptr;ptr2->next!=NULL;ptr2=ptr2->next)
   {
 if(ptr2->next->info==item)
   ptr2->next=ptr2->next->next;
   }
  }
 }
 void merged_2sorted_list(node **head1,node **head2)
 {
  node *head,*ptr,*ptr1,*ptr2,*loc;   int f;
   ptr1=*head1;
   ptr2=*head2;
   create_ll(&head);
  while(ptr1!= NULL || ptr2!=NULL)
   {
 ptr= (node*)malloc(sizeof(node));
 if(ptr1->info > ptr2->info)
  {
  ptr->info=ptr2->info;
  ptr->next=NULL;
  ptr2=ptr2->next;
  f=0;
  }
   else
 {
  ptr->info=ptr1->info;
  ptr->next=NULL;
  ptr1=ptr->next;
  f=0;
 }
 if(f==0)
 {
  if(head==NULL)
   head=ptr;
 else
   {
   loc=head;
   while(loc->next!=NULL)
   loc=loc->next;
   loc->next=ptr;
   }
 }
 if(ptr1!=NULL &&ptr2==NULL)
 {
  while(ptr1!=NULL)
   {
 ptr->info=ptr1->info;
 ptr->next=NULL;
   if(head==NULL)
   head=ptr;
 else
  {
    loc=head;
  while(loc->next!=NULL)
   loc=loc->next;
    loc->next=ptr;
   }
   ptr1=ptr1->next;
   }
  }
  if(ptr1==NULL && ptr2!=NULL)
   {
 while(ptr2!=NULL)
 {
 ptr->info=ptr2->info;
   if(head==NULL)
   head=ptr;
 else
   {
   loc=head;
   while(loc->next!=NULL)
  loc=loc->next;
    loc->next=ptr;
 }
   ptr2=ptr2->next;
 }
 }
 }
 loc=head;
 clrscr();
 while(loc!=NULL)
 {
 printf("%d ",loc->info);
 loc=loc->next;
}
getch();
}
 void insertion_at_big_in_2ll(node **first,int item)
{
 node *ptr;
 ptr= (node*)malloc(sizeof(node));
 ptr->info=item;
 ptr->next=NULL;
 if(first==NULL)
 *first=ptr;
 else
 {
 ptr->next=*first;
 *first=ptr;
   }
 }
void deletion_from_end_in_cll(node **head)
{
 node *ptr,*loc;
 ptr=*head;
 while(ptr->next!=*head)
 {
  loc=ptr;
  ptr=ptr->next;
 }
 loc->next=*head;
 free(ptr);
 }
 void deletion_after_element_in_cll(node **head)
 {
 node *ptr,*loc;int after,item;
 loc=*head;
 while(loc->next!=*head&&loc->info!=after)
 loc=loc->next;
 if(*head==NULL)
  printf("list is empty");
 else
  ptr=loc;
  ptr->next=loc->next->next;
}
void del(node **head)
{
 node *ptr,*loc;
 int i;
 ptr=*head;
 for(i=0;i<4;i++)
 ptr=ptr->next;
 loc=ptr;
 while(loc->next->next!=NULL)
 {
 // *head= (*head)->next;
 loc->info=loc->next->info;
 loc=loc->next;
}
loc->next=NULL;
//loc->next=NULL;
//free(loc);
//(*head)->next=loc->next;
//loc->next
}









 


Send instant messages to your online friends http://uk.messenger.yahoo.com 

Reply via email to