Advertisement
sanpai

Intersection of linked list

Aug 31st, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. #include<malloc.h>
  4.  
  5.  
  6.  
  7. struct node
  8. {
  9.  
  10.  
  11.  
  12. int num;
  13. struct node *ptr;
  14.  
  15. };
  16.  
  17.  
  18.  
  19. struct node * Addelement(struct node *,int);
  20.  
  21. struct node * uni(struct node *,struct node *);
  22.  
  23. void display(struct node *);
  24.  
  25. int main()
  26.  
  27. {
  28.  
  29.  
  30.  
  31. int num1,num2,i,n;
  32.  
  33. struct node *p1,*p2,*p3;
  34.  
  35. p1=NULL;
  36.  
  37. p2=NULL;
  38. p3=NULL;
  39.  
  40. printf(" \n The number of elements to be added to the 1st list ");
  41.  
  42. scanf("%d",&num1);
  43.  
  44.  
  45. for(i=1;i<=num1;i++)
  46.  
  47. {
  48.  
  49. printf("\n Enter the Elements of 1st list\n ");
  50.  
  51. scanf("%d",&n);
  52.  
  53. p1=Addelement(p1,n);
  54.  
  55. }
  56.  
  57.  
  58.  
  59. printf(" \n The number of elements to be added to the 2nd list ");
  60.  
  61. scanf("%d",&num2);
  62.  
  63.  
  64.  
  65. for(i=1;i<=num2;i++)
  66.  
  67. {
  68.  
  69. printf("\n Enter the Elements of 2nd list \n ");
  70.  
  71. scanf("%d",&n);
  72.  
  73. p2=Addelement(p2,n);
  74.  
  75. }
  76.  
  77. printf("\n The elements of first list are ");
  78.  
  79. display(p1);
  80.  
  81. printf("\n The elements of second list are ");
  82.  
  83. display(p2);
  84. p3=uni(p1,p2);
  85.  
  86. printf("\n The elements after the intersection are : ");
  87. display(p3);
  88.  
  89. }
  90.  
  91. struct node * Addelement(struct node *head,int num)
  92.  
  93. {
  94. struct node *newnode;
  95.  
  96. newnode=(struct node *)malloc(sizeof(struct node));
  97.  
  98. newnode->num=num;
  99.  
  100. if(head==NULL)
  101.  
  102. {
  103. newnode->ptr=NULL;
  104.  
  105. return newnode;
  106.  
  107. }
  108.  
  109.  
  110.  
  111. else
  112.  
  113. {
  114.  
  115. newnode->ptr=head;
  116.  
  117. return newnode;
  118.  
  119. }
  120.  
  121. }
  122.  
  123.  
  124. struct node * uni(struct node *p1,struct node *p2)
  125. {
  126.  
  127. if(p1==NULL&&p2==NULL)
  128. {
  129. printf("\n The list is empty ");
  130. }
  131. struct node *temp,*p3,*temp2;
  132. p3=NULL;
  133. int value;
  134. temp2=p2;
  135. while(p1!=NULL)
  136. {
  137. value=p1->num;
  138. temp=(struct node *)malloc(sizeof(struct node));
  139. if(p3==NULL)
  140.  
  141. {
  142. temp->ptr=NULL;
  143. p3= temp;
  144. }
  145. else
  146. {
  147. temp->ptr=p3;
  148. p3=temp;
  149.  
  150. }
  151. p2=temp2;
  152. while(p2!=NULL)
  153. {
  154. if(value==p2->num)
  155. {
  156. temp->num=value;
  157. p2=p2->ptr;
  158. break;
  159. }
  160. else
  161. {
  162. p2=p2->ptr;
  163. }
  164. }
  165. p1=p1->ptr;
  166. }
  167. return p3;
  168. }
  169.  
  170.  
  171. void display(struct node *p3)
  172.  
  173. {
  174.  
  175. if(p3==NULL)
  176.  
  177.  
  178. {
  179.  
  180.  
  181. printf("\n The list is empty \n");
  182.  
  183.  
  184. }
  185.  
  186. while(p3!=NULL)
  187.  
  188.  
  189. {
  190.  
  191. printf("%d \t",p3->num);
  192.  
  193.  
  194. p3=p3->ptr;
  195.  
  196.  
  197.  
  198. }
  199.  
  200. } 199,1 97%
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement