SINGLY
LINKED LIST
1.Write a C program to accept 2 singly linked lists
& print the elements which are common in both the lists.
//program starts here
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct
node *next;
};
typedef struct node snode;
snode *f=NULL,*l=NULL,*new,*p;
snode *l1=NULL,*l2=NULL;
snode *insert(snode *f,int x)
{
new=malloc(sizeof(snode));
new->data=x;
new->next=NULL;
if(f==NULL)
{
f=new;
f->next
=NULL;
}
else
{
p=f;
while(p->next!=NULL)
p=p->next;
p->next=new;
}
return f;
}
void display(snode *f)
{
snode
*temp;
temp=f;
printf("elements\n");
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->next;
}
}
void find(snode *l1,snode *l2)
{
printf("common
elements\n");
snode
*tmp;
while(l1!=NULL)
{
tmp=l2;
while(tmp!=NULL)
{
if(l1->data==tmp->data)
printf("%d\t",tmp->data);
tmp=tmp->next;
}
l1=l1->next;
}
}
int main()
{
int
c,d,ch,i,j,n;
ch=0;
d=1;
while(ch!=2)
{
printf("1.insert\t\t2.break\n
enter ur choice\n");
scanf("%d",&ch);
switch(ch)
{
case
1:while(d!=0)
{
printf("\nenter data\n");
scanf("%d",&n);
l1=insert(l1,n);
printf("enter 0r
exit[0/1]:\t");
scanf("%d",&d);
}
printf("ele of list 1
are:\n");
display(l1);
d=1;
while(d!=0)
{
printf("\nenter data\n");
scanf("%d",&n);
l2=insert(l2,n);
printf("enter 0r
exit[0/1]:\t");
scanf("%d",&d);
}
printf("ele of list 2
are:\n");
display(l2);
break;
case
2 :break;
}
}
find(l1,l2);
}//END OF 1st PROGRAM
2. Consider a linked list with n integers. Each node
of the list is numbered from 1 to n. Develop a program using 'C' language to
split this list into 4 lists so that:
a. First list contains nodes numbered 1,5,9,13,_,_,_
b. Second list contains nodes numbered 2,6,10,14,_,_
c. Third list contains nodes numbered 3,7,11,15,_,_
d.Fourth list contains nodes numbered 4,8,12,16,_
//program starts here
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct
node *next;
};
typedef struct node snode;
snode *f=NULL,*l=NULL,*p,*new;
snode *l1=NULL;
snode *insert(snode *f,int x)
{
new=malloc(sizeof(snode));
new->data=x;
new->next=NULL;
if(f==NULL)
{
f=new;
f->next=NULL;
}
else
{
p=f;
while(p->next!=NULL)
p=p->next;
p->next=new;
}
return f;
}
void display(snode *f){
snode
*ptr;
ptr=f;
while(ptr!=NULL)
{
printf("%d\t",ptr->data);
ptr=ptr->next;
}
}
void split(snode *f)
{
snode
*temp;
snode
*f1,*f2,*f3,*f4;
f1=f2=f3=f4=NULL;
temp=f;
int i=0;
while(temp!=NULL)
{
if(i%4==0)
f1=insert(f1,temp->data);
if(i%4==1)
f2=insert(f2,temp->data);
if(i%4==2)
f3=insert(f3,temp->data);
if(i%4==3)
f4=insert(f4,temp->data);
i++;
temp=temp->next;
}
printf("elements
of 1st list:\n");
display(f1);
printf("\nelemets
of 2nd list:\n");
display(f2);
printf("elements
of 3rd list:\n");
display(f3);
printf("\nelemets
of 4th list:\n");
display(f4);
}
int main()
{
int
ch=0,n,i,d=1;
while(ch!=2)
{
printf("1.insert\t2.break\nenter
ur choice\n");
scanf("%d",&ch);
switch(ch)
{
case
1:while(d!=0)
{
printf("\nenter data:\t");
scanf("%d",&n);
l1=insert(l1,n);
printf("enter or
exit[0/1]\t");
scanf("%d",&d);
}
display(l1);
break;
case
2: break;
}
}
split(l1);
}//END OF 2nd PROGRAM
3. Write a program to multiply two polynomials using
linked list.
//PROGRAM STARTS HERE
#include<stdio.h>
#include<stdlib.h>
struct node{
int coeff,power;
struct node *next;
};
typedef struct node snode;
snode *f=NULL,*l=NULL,*new,*p;
snode *l1=NULL,*l2=NULL,*l3=NULL;
snode *insert(snode *f,int co,int po)
{
new=malloc(sizeof(snode));
new->coeff=co;
new->power=po;
new->next=NULL;
if(f==NULL)
{
f=new;
f->next
=NULL;
}
else
{
p=f;
while(p->next!=NULL)
p=p->next;
p->next=new;
}
return f;
}
void display(snode *f)
{
snode *temp;
temp=f;
printf("elements\n");
while(temp->next!=NULL)
{
printf("%dx%d
",temp->coeff,temp->power);
printf("+");
temp=temp->next;
}
printf("%dx%d
",temp->coeff,temp->power);
printf("\n");
}
void mul(snode *l1,snode *l2)
{
int co,po;
snode *t;
while(l1!=NULL)
{
t=l2;
while(t!=NULL)
{
co=l1->coeff*t->coeff;
po=l1->power
+ t->power;
l3=insert(l3,co,po);
t=t->next;
}
l1=l1->next;
}
display(l3);
}
int main()
{
int i,n1,n2,co,po;
printf("enter
the number of terms in 1st polynomial:\t");
scanf("%d",&n1);
for(i=1;i<=n1;i++)
{
printf("\n%d
term coefficient:\t",i);
scanf("%d",&co);
printf("\n%d
term power:\t",i);
scanf("%d",&po);
l1=insert(l1,co,po);
}
printf("\nfirst
polynomial is:\n");
display(l1);
printf("\nenter
the number of terms in 2nd
polynomial:\t");
scanf("%d",&n2);
for(i=1;i<=n2;i++)
{
printf("\n%d
term coefficient:\t",i);
scanf("%d",&co);
printf("\n%d
term power:\t",i);
scanf("%d",&po);
l2=insert(l2,co,po);
}
printf("\nsecond polynomial is:\n");
display(l2);
mul(l1,l2);
}//END OF 3rd PROGRAM
No comments:
Post a Comment