Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<malloc.h>
- struct node
- {
- int num;
- struct node *ptr;
- };
- struct node * Addelement(struct node *,int);
- struct node * uni(struct node *,struct node *);
- void display(struct node *);
- int main()
- {
- int num1,num2,i,n;
- struct node *p1,*p2,*p3;
- p1=NULL;
- p2=NULL;
- p3=NULL;
- printf(" \n The number of elements to be added to the 1st list ");
- scanf("%d",&num1);
- for(i=1;i<=num1;i++)
- {
- printf("\n Enter the Elements of 1st list\n ");
- scanf("%d",&n);
- p1=Addelement(p1,n);
- }
- printf(" \n The number of elements to be added to the 2nd list ");
- scanf("%d",&num2);
- for(i=1;i<=num2;i++)
- {
- printf("\n Enter the Elements of 2nd list \n ");
- scanf("%d",&n);
- p2=Addelement(p2,n);
- }
- printf("\n The elements of first list are ");
- display(p1);
- printf("\n The elements of second list are ");
- display(p2);
- p3=uni(p1,p2);
- printf("\n The elements after the intersection are : ");
- display(p3);
- }
- struct node * Addelement(struct node *head,int num)
- {
- struct node *newnode;
- newnode=(struct node *)malloc(sizeof(struct node));
- newnode->num=num;
- if(head==NULL)
- {
- newnode->ptr=NULL;
- return newnode;
- }
- else
- {
- newnode->ptr=head;
- return newnode;
- }
- }
- struct node * uni(struct node *p1,struct node *p2)
- {
- if(p1==NULL&&p2==NULL)
- {
- printf("\n The list is empty ");
- }
- struct node *temp,*p3,*temp2;
- p3=NULL;
- int value;
- temp2=p2;
- while(p1!=NULL)
- {
- value=p1->num;
- temp=(struct node *)malloc(sizeof(struct node));
- if(p3==NULL)
- {
- temp->ptr=NULL;
- p3= temp;
- }
- else
- {
- temp->ptr=p3;
- p3=temp;
- }
- p2=temp2;
- while(p2!=NULL)
- {
- if(value==p2->num)
- {
- temp->num=value;
- p2=p2->ptr;
- break;
- }
- else
- {
- p2=p2->ptr;
- }
- }
- p1=p1->ptr;
- }
- return p3;
- }
- void display(struct node *p3)
- {
- if(p3==NULL)
- {
- printf("\n The list is empty \n");
- }
- while(p3!=NULL)
- {
- printf("%d \t",p3->num);
- p3=p3->ptr;
- }
- } 199,1 97%
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement