In this C program on linked list, two different linked lists will be created with different number of nodes. A third linked list will be created from those two linked lists taking the nodes with common values.
Step by step analysis of the linked list program
Two linked lists are created separately with varying number of nodes. User enters the number of nodes of each linked list while creating them. The linked lists are displayed after creating. Function ‘void make(struct list *)’ is used to create linked list and another function ‘void show(struct list *)’ is defined in this program to display linked list. Function ‘struct list *com(struct list *,struct list *,struct list *)’ is defined in this program to do the final job, searching the common value nodes from each of the linked lists and creating the third linked list.
Codes of this linked list program
#include< stdio.h>
#include< stdlib.h>
struct list
{
int info;
struct list *next;
};
int i=0;
void make(struct list *);
void show(struct list *);
struct list *com(struct list *,struct list *,struct list *);
void main()
{
struct list *temp,*head,*in=NULL;
clrscr();
temp=(struct list *)malloc(sizeof(struct list));
make(temp);
printf("\ndisplaying FIRST linked list\n");
show(temp);
head=(struct list *)malloc(sizeof(struct list));
make(head);
printf("\ndisplaying SECOND linked list\n");
show(head);
in=com(temp,head,in);
printf("\nDisplaying the final linked list\n");
show(in);
getch();
}
void make(struct list *temp)
{
int n,i;
printf("\n\n enter the no of nodes you want to create\n");
scanf("%d",&n);
for(i=1;i< n;i++)
{
printf("entering value in %d node\n",i);
scanf("%d",&temp->info);
temp->next=(struct list *)malloc(sizeof(struct list));
temp=temp->next;
}
printf("Entering value in %d node\n",i);
scanf("%d",&temp->info);
temp->next=NULL;
}
void show(struct list *temp)
{
while(temp!=NULL)
{
printf("%d\t",temp->info);
temp=temp->next;
}
}
struct list *com(struct list *temp,struct list *h,struct list *in)
{
struct list *head,*head1;
while(temp!=NULL)
{
head=h;
while(head!=NULL)
{
if(temp->info==head->info)
{
in->next=(struct list *)malloc(sizeof(struct list));
in=in->next;
in->info=temp->info;
in->next=NULL;
if(i==0)
head1=in;
i++;
}
head=head->next;
}
temp=temp->next;
}
return head1;
}
char name[15];
int i=0;
clrscr();
get(start);
display(start);
while(i==0)
{
printf("\nEnter the name whose record is to be changed :");
scanf("%s",name);
i=search(name,start);
}
display(start);
getch();
}
Program to display a linked list from front side and reverse side
#include
#include
struct tag
{
int i;
struct tag *next;
};
struct tag *start=NULL,*node;
int x=1,flag=0,flag1;
void create()
{
char ch;
node=start;
puts("Want to create nodes(y/n):-");
scanf("%c",&ch);
while(ch!='n')
{
node->next=(struct tag *)malloc(sizeof(struct tag));
node=node->next;
node->next=NULL;
printf("\nEnter value::");
scanf("%d",&node->i);
flag++;
puts("Want to create nodes(y/n):-");
ch=getche();
}
flag1=flag;
}
void dis()
{
node=start->next;
while(node)
{
printf("%d\n",node->i);
node=node->next;
}
}
void dis1()
{
while(x<=flag1)
{
flag=flag1-x;
node=start->next;
while(flag)
{
node=node->next;
flag--;
}
printf("%d\n",node->i);
x++;
}
}
void main()
{
clrscr();
create();
printf("\nValues from front side are as follows.\n");
dis();
printf("\nLinked List displayed from reverse\n");
dis1();
getch();
}
#include
struct tag
{
int i;
struct tag *next;
};
struct tag *start=NULL,*node;
int x=1,flag=0,flag1;
void create()
{
char ch;
node=start;
puts("Want to create nodes(y/n):-");
scanf("%c",&ch);
while(ch!='n')
{
node->next=(struct tag *)malloc(sizeof(struct tag));
node=node->next;
node->next=NULL;
printf("\nEnter value::");
scanf("%d",&node->i);
flag++;
puts("Want to create nodes(y/n):-");
ch=getche();
}
flag1=flag;
}
void dis()
{
node=start->next;
while(node)
{
printf("%d\n",node->i);
node=node->next;
}
}
void dis1()
{
while(x<=flag1)
{
flag=flag1-x;
node=start->next;
while(flag)
{
node=node->next;
flag--;
}
printf("%d\n",node->i);
x++;
}
}
void main()
{
clrscr();
create();
printf("\nValues from front side are as follows.\n");
dis();
printf("\nLinked List displayed from reverse\n");
dis1();
getch();
}
No comments:
Post a Comment