Guest User

Untitled

a guest
Apr 25th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. x = a+c
  2. y = b+c
  3.  
  4. x = a+c
  5. y = b+c
  6. z = a+b
  7.  
  8. a = (+x-y+z)/2
  9. b = (-x+y+z)/2
  10. c = (+x+y-z)/2
  11.  
  12. lenA = count(listA) //iterates list A
  13. lenB = count(listB) //iterates list B
  14.  
  15. ptrA = listA
  16. ptrB = listB
  17.  
  18. //now we adjust either ptrA or ptrB so that they are equally far from the end
  19. while(lenA > lenB):
  20. ptrA = ptrA->next
  21. lenA--
  22. while(lenB > lenA):
  23. prtB = ptrB->next
  24. lenB--
  25.  
  26. while(ptrA != NULL):
  27. if (ptrA == ptrB):
  28. return ptrA //found merge point
  29. ptrA = ptrA->next
  30. ptrB = ptrB->next
  31.  
  32. A-->B-->C
  33. |
  34. V
  35. 1-->2-->3-->4-->5
  36.  
  37. A B C
  38.  
  39. 1-->2-->3 4 5
  40.  
  41. //pseudocode
  42. //for the first list
  43. p1=list1;
  44. unsigned int addr[];//to store addresses
  45. i=0;
  46. while(p1!=null){
  47. addr[i]=&p1;
  48. p1=p1->next;
  49. }
  50. int len=sizeof(addr)/sizeof(int);//calculates length of array addr
  51. //for the second list
  52. p2=list2;
  53. while(p2!=null){
  54. if(search(addr[],len,&p2)==1)//match found
  55. {
  56. //this is the merging node
  57. return (p2);
  58. }
  59. p2=p2->next;
  60. }
  61.  
  62. int search(addr,len,p2){
  63. i=0;
  64. while(i<len){
  65. if(addr[i]==p2)
  66. return 1;
  67. i++;
  68. }
  69. return 0;
  70. }
  71.  
  72. -> A - Contains reference in Pointers? No, move on
  73. -> B - Contains reference in Pointers? No, move on
  74. -> C - Contains reference in Pointers? No, move on
  75. -> D - Contains reference in Pointers? Yes, merge point found, break.
  76.  
  77. for each item in list a
  78. push pointer to item onto stack_a
  79.  
  80. for each item in list b
  81. push pointer to item onto stack_b
  82.  
  83. while (stack_a top == stack_b top) // where top is the item to be popped next
  84. pop stack_a
  85. pop stack_b
  86.  
  87. // values at the top of each stack are the items prior to the merged item
  88.  
  89. Head A 0x7fffb2f3c4b0
  90. 0x214f010
  91. 0x214f030
  92. 0x214f050
  93. 0x214f070
  94. 0x214f090
  95. 0x214f0f0
  96. 0x214f110
  97. 0x214f130
  98. 0x214f150
  99. 0x214f170
  100.  
  101.  
  102. Head B 0x7fffb2f3c4a0
  103. 0x214f0b0
  104. 0x214f0d0
  105. 0x214f0f0
  106. 0x214f110
  107. 0x214f130
  108. 0x214f150
  109. 0x214f170
  110.  
  111. next_node = node->next;
  112. node = (struct node*)((unsigned long)node | 0x1UL);
  113.  
  114. real_node = (struct node*)((unsigned long)node) & ~0x1UL);
  115. real_node = real_node->next;
  116. node = real_node;
Add Comment
Please, Sign In to add comment