Important programs in c language part-IV

/*insert subtree*/

#include<stdio.h>
#include<conio.h>
struct rec
{
long num;
struct rec *left;
struct rec *right;
};
struct rec *tree=NULL;
struct rec *insert(struct rec *tree,long num);
void *exchange(struct rec *tree);
struct rec *temp;
void main()
{
 struct rec *tree=NULL;
 int choice;
 long digit;
 do
  {
    choice=select();
    switch(choice)
    {
      case 1: puts("Enter integer: To quit enter 0");
	      scanf("%ld",&digit);
	      while(digit!=0)
	      {
		tree=insert(tree,digit);
		scanf("%ld",&digit);
		}continue;
      case 2: printf("%5d\n",tree->num);exchange(tree);continue;
      case 3: puts("END");exit(0);
      }
      }while(choice!=3);
      }
      int select()
      {
      int selection;
      do
      {
      puts("Enter 1: Insert a node");
      puts("Enter 2: Exchange subtrees");
      puts("Enter 3: End");
      puts("Enter your choice");
      scanf("%d",&selection);
      if((selection<1)||(selection>3))
      {puts("Wrong choice: Try again");
       getchar();
       }
       }while((selection<1)||(selection>3));
       return selection;
       }
       struct rec *insert(struct rec *tree,long digit)
       {
       if(tree==NULL)
       {
       tree=(struct rec *)malloc(sizeof(struct rec));
       tree->left=tree->right=NULL;
       tree->num=digit;
       }
       else
       if(digit<tree->num)
       tree->left=insert(tree->left,digit);
       else if(digit>tree->num)
       tree->right=insert(tree->right,digit);
       else if(digit==tree->num)
       {puts("Duplicates Nodes: Program Exited");exit(0);
       }
       return(tree);
       }
       void *exchange(struct rec *tree)
       {
       if((tree->left->num!=0)&&(tree->right->num!=0))
       {
       temp=tree->left;
       tree->left=tree->right;
       tree->right=temp;
       printf("%5ld\n",tree->left->num);
       printf("%5ld\n",tree->right->num);
       exchange(tree->left);
       exchange(tree->right);
       }
       }

/*insertion and deletion at places*/

#include<stdio.h>
#include<conio.h>
struct node
{
int info;
struct node *left,*right;
};
typedef struct node NODE;
NODE *head;
insert_cir(NODE *head)
{
NODE *ptr,*temp;
int item;
ptr=(NODE *)malloc(sizeof(NODE));
printf("Enter the no\n");
scanf("%d",&item);
ptr->info=item;
temp=head->right;
head->right=ptr;
ptr->left=head;
ptr->right=temp;
temp->left=ptr;
return (head);
}
insert_cir_end(NODE *head)
{
NODE *ptr,*temp;
int item;
ptr=(NODE *)malloc(sizeof(NODE));
printf("Enter the no\n");
scanf("%d",&item);
ptr->info=item;
temp=head->left;
temp=head->left;
temp->right=ptr;
ptr->left=temp;
ptr->right=head;
head->left=ptr;
return (head);
}
dele_beg(NODE *head)
{
NODE *temp;
if(head->right==head)
{
printf("Empty list\n");
return;
}
else
{
temp=head->right;
printf("deleted element is =%d\n",temp->info);
head->right=temp->right;
temp->right->left=head;
free(temp);
return (head);
}
}
dele_end(NODE *head)
{
NODE *temp;
if(head->right==head)
{
printf("empty list\n");
return;
}
else
{
temp=head->left;
printf("deleted elementis =%d\n",temp->info);
head->left=temp->left;
free(temp);
return (head);
}
}
void main()
{
int left,right,choice;
clrscr();
do
{
printf("Enter 1: insert at the begining of the link list\n");
printf("Enter 2: insert at the end of the link list\n");
printf("Enter 3: delete at the begining of the link list\n");
printf("Enter 4: delete at the end of the link list\n");
printf("Enter 5: end\n");
printf("Enter your choice\n" );
scanf("%d",&choice);
switch(choice)
{
case 1: insert_cir(head);
	break;
case 2: insert_cir_end(head);
	break;
case 3: dele_beg(head);
	break;
case 4: dele_end(head);
	break;
case 5: return;
}
}while(choice!=5);
getch();
}

If you have questions, please ask below

Leave a Reply

If you have any questions headover to our forums

You can use these XHTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <blockquote cite=""> <code> <em> <strong>