Important programs in c language-VI

/*linked list*/

 
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
void main()
{
struct node
{
int num;
struct node *ptr;
};
typedef struct node NODE;
NODE *head, *first, *temp;
int count=0;
int choice=1;
first=NULL;
while(choice)
{
	head=(NODE *)malloc(sizeof(NODE));
	printf("Enter the data item\n");
	scanf("%d",&head->num);
	  if(first!=NULL)
		{
		  temp->ptr=head;
		  temp=head;
		}
else
		{
		  first=temp=head;
		}
fflush(stdin);
printf("Do you want to continue(type 0 or 1)?\n");
scanf("%d",&choice);
}
 
temp->ptr=NULL;
temp=first;
printf("Status of the linked list is\n");
while(temp!=NULL)
	{
		printf("%d",temp->num);
		count++;
		temp=temp->ptr;
	}
printf("NULL");
printf("NO of nodes in the list =%d\n",count);
getch();
}

/*insertion in tree*/

#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 countnode(struct rec *tree);
void countleave(struct rec *tree);
struct rec *temp;
int node=1,total;
 main()
{
	struct rec *tree=NULL;
	int choice;
	long digit;
	do
	  {
	    choice=select();
	    switch(choice)
		{
		  case 1: puts("Enter integers:To quit enter 0");
			  scanf("%ld",&digit);
			  while(digit!=0)
			       {
				 tree=insert(tree,digit);
				 scanf("%ld",&digit);
				}continue;
		  case 2: countnode(tree);
			  printf("Nuber of nodes=%d\n",node);
			  node=1;continue;
		  case 3: countleave(tree);
			  printf("Nuber of leaves=%d\n",total);
			  total=0;
			  continue;
		  case 4: puts("END");exit(0);
		 }
	  }while(choice!=4);
}
   int select()
      {
       int selection;
       do
	 {
	   puts("Enter 1: Insert a node");
	   puts("Enter 2: Display number of nodes");
	   puts("Enter 3: Display number of leave");
	   puts("Enter 4: End");
	   puts("Enter your choice");
	   scanf("%d",&selection);
	   if((selection<1)||(selection>4))
	     {
	       puts("Wrong choice: Try again");
	       getchar();
	     }
	 }while((selection<1)||(selection>4));
		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 countnode(struct rec *tree)
{
    if(tree!=NULL)
      {
      if(tree->left!=NULL)
	{
	  node++;
	  countnode(tree->left);
	 }
      if(tree->right!=NULL)
	{
	  node++;
	  countnode(tree->right);
	}
       }
}
void countleave(struct rec *tree)
{
   if(tree!=NULL)
     {
      if((tree->left==NULL)&&(tree->right==NULL))
	 total++;
 else
   countleave(tree->left);
   countleave(tree->right);
     }
}

/*insertion in linked list*/

#include
#include
int select();
struct rec
{
char name[80];
struct rec *next;
};
struct rec *rear;
struct rec *create(struct rec *list);
struct rec *insert1(struct rec *node);
struct rec *insert2(struct rec *node);
struct rec *insert3(struct rec *node);
struct rec *insert4(struct rec *node);
struct rec *delete(struct rec *node);
void *display(struct rec *list);
int nodes;
main()
{
struct rec *first=NULL;
int choice;
clrscr();
do
{
choice=select();
switch(choice)
{
case 1: first=create(first);continue;
case 2: first=insert1(first);continue;
case 3: first=insert2(first);continue;
case 4: first=insert3(first);continue;
case 5: first=insert4(first);continue;
case 6: first=delete(first);continue;
case 7: display(first);continue;
case 8: puts(”END”);exit(0);
}
}while(choice!=8);
}
int select()
{
int selection;
do
{
puts(”Enter 1: create the list”);
puts(”Enter 2: insert in the beginnig of the list”);
puts(”Enter 3: insert after a node in the list”);
puts(”Enter 4: insert before a node in the list”);
puts(”Enter 5: insert in the end of the list”);
puts(”Enter 6: delete the list”);
puts(”Enter 7: display the list”);
puts(”Enter 8: END”);
puts(”Enter your choice”);
scanf(”%d”,&selection);
}while(selection<1||selection>8);
return selection;
}
struct rec *create(struct rec *first)
{
struct rec *element;
first=(struct rec*)malloc(sizeof(struct rec));
puts(”Enter/name/word/text: To quit enter*”);
scanf(” %[^\n]“,first->name);
first->next=first;
rear=first;
rear->next=first;for(;;)
{
element=(struct rec*)malloc(sizeof(struct rec));
scanf(” %[^\n]“,element->name);
if(strcmp(element->name,”*”)==0)break;
element->next=first;
rear->next=element;
rear= element;
}
return(first);
}
struct rec *insert1(struct rec *first)
{
struct rec *node;
node=(struct rec*)malloc(sizeof(struct rec));
puts(”Enter node/name to be inserted”);
scanf(” %[^\n]“,node->name);
if(first==NULL)
{
node->next=first;
rear=first;
}
else
{
node->next=first;
first=node;
rear->next=first;
}
return(first);
}
struct rec *insert2(struct rec *first)
{
struct rec *current,*prior,*x;
struct rec *node;current=first;
node=(struct rec*)malloc(sizeof(struct rec));
puts(”Enter node/name after which new node to be inserted”);
scanf(” %[^\n]\n”,node->name);
x=(struct rec*)malloc(sizeof(struct rec));
puts(”Enter node/name to be inserted”);
scanf(” %[^\n]“,x->name);
while(current!=rear && current!=NULL)
{
if(strcmp(current->name,node->name)==0)
{
x->next=current->next;
current->next=x;
return(first);
}
else current=current->next;
}
if(strcmp(current->name,node->name)==0)
{
x->next=first;
rear->next=x;
rear=x;
return(first);
}
puts(”Node does not exist in the list”);
return(first);
}
struct rec *insert3(struct rec *first)
{
struct rec *node,*current,*x,*prior;
current=first;
node=(struct rec*)malloc(sizeof(struct rec));
puts(”Enter node/name before which new node to be inserted”);
scanf(” %[^\n]“,node->name);
x=(struct rec*)malloc(sizeof(struct rec));
puts(”Enter node/name to be inserted”);
scanf(” %[^\n]“,x->name);
if(strcmp(current->name,node->name)==0)
{
x->next=first;
first=x;
return(first);
}
while(current!=NULL)
{
prior=current;
current=current->next;
if(strcmp(current->name,node->name)==0)
{
x->next=current;
prior->next=x;
return(first);
}
}
puts(”Node does not exist in the list”);
return(first);
}
struct rec *insert4(struct rec *first)
{
struct rec *element;
element=(struct rec*)malloc(sizeof(struct rec));
puts(”Enter node/name to be inserted at the end of list”);
scanf(” %[^\n]“,element->name);
element->next=first;
rear->next=element;
rear=element;
return(first);
}
struct rec *delete(struct rec *first)
{
struct rec *current,*prior,*node;
current=first;
node=(struct rec*)malloc(sizeof(struct rec));
puts(”Enter node/name to be delete”);
scanf(” %[^\n]“,node->name);
if(strcmp(current->name,node->name)==0)
{
first=current->next;
rear->next=first;
free(current);
return(first);
}
while(current!=rear && current!=NULL)
{
prior=current;
current=current->next;
if(strcmp(current->name,node->name)==0)
{
prior->next=current->next;
free(current);
return(first);
}
}
if(strcmp(current->name,node->name)==0)
{
prior->next=current->next;
prior->next=first;
rear=prior;
free(current);
return(first);
}
puts(”Node does not exist in the list”);
return(first);
}
void *display(struct rec *first)
{
int node=0;
do
{
node++;
printf(”%s\n”,first->name);
first=first->next;
}
while((first!=rear->next)&&(first!=NULL));
printf(”Nuber of nodes= %d\n”,node);
}

Leave a Reply

You must be logged in to post a comment.

Close