Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. #include <cstdlib> // To allow NULL if no other includes
  2. #include <iostream>
  3. using namespace std;
  4.  
  5.  
  6. struct Node {
  7. Node(int data = 0, Node* link = NULL) : data(data), link(link) {}
  8. int data;
  9. Node* link;
  10. };
  11.  
  12. void listInsertHead(int entry, Node*& headPtr) {
  13. headPtr = new Node(entry, headPtr);
  14. }
  15.  
  16.  
  17. void splice(Node* headptr2, Node* target) {
  18. //target is a pointer to 7 Node
  19. Node *temp = target->link; //store a temporary pointer for the target's pointer
  20. target->link = headptr2; //change the target pointer to L2's first Node
  21. while (headptr2->link != nullptr) //while there is a next pointer after current,
  22. headptr2 = headptr2->link; //current gets next pointer
  23. if (headptr2 != nullptr) //if L2 is not empty,
  24. headptr2->link = temp; //the pointer to the last element of L2 gets the temporary pointer stored from earlier
  25. }
  26.  
  27.  
  28. Node* isSubList(Node*headptr1, Node* headptr2) {
  29. while (headptr1->data != headptr2->data) { //data in L1 doesn't match data in L2,
  30. headptr1 = headptr1->link; // increment headptr1 until the data does match
  31. }
  32. Node *temp = headptr1;
  33. bool isSubList = true;
  34. while (isSubList && (headptr2->link != nullptr)) { //while the data matches, increment both pointers
  35. headptr1 = headptr1->link;
  36. headptr2 = headptr2->link;
  37. if (headptr1->data == headptr2->data)
  38. isSubList = true;
  39. else
  40. isSubList = false;
  41. }
  42.  
  43. if (isSubList)
  44. return temp;
  45. else {
  46. cout << "Failed to match" << endl;
  47. return nullptr;
  48. }
  49. }
  50.  
  51.  
  52. void printList(Node* head) {
  53. //if (head == nullptr) { cout << "nullptr"; }
  54. while (head != nullptr) { //while temp->link != nullptr is not the right code
  55. cout << head->data << " ";
  56. head = head->link;
  57. }
  58. cout << endl;
  59. }
  60.  
  61.  
  62. int main() {
  63. Node *head1 = nullptr; //head1 points to the first Node of L1
  64. listInsertHead(1, head1);
  65. listInsertHead(9, head1);
  66. listInsertHead(7, head1);
  67. listInsertHead(5, head1); // 5 7 9 1
  68.  
  69. Node *head2 = nullptr; //head2 points to the first Node of L2
  70. listInsertHead(2, head2);
  71. listInsertHead(3, head2);
  72. listInsertHead(6, head2); //6 3 2
  73.  
  74. cout << "Part One:" << endl << endl;
  75. cout << "L1: ";
  76. printList(head1);
  77. cout << "L2: ";
  78. printList(head2);
  79. Node *target = head1->link;
  80. cout << "Target: ";
  81. printList(target);
  82. cout << "Splicing L2 at target in L1" << endl;
  83. splice(head2, target);
  84. cout << "L1: ";
  85. printList(head1);
  86. cout << "L2: ";
  87. printList(target->link);
  88.  
  89.  
  90.  
  91. cout << "=====================" << endl << endl;
  92. cout << "Part Two:" << endl << endl;
  93.  
  94. Node *head3 = nullptr; //head3 points to the first Node of the target List
  95. listInsertHead(6, head3);
  96. listInsertHead(5, head3);
  97. listInsertHead(4, head3);
  98. listInsertHead(2, head3);
  99. listInsertHead(3, head3);
  100. listInsertHead(2, head3);
  101. listInsertHead(3, head3);
  102. listInsertHead(2, head3);
  103. listInsertHead(1, head3); //1 2 3 2 3 2 4 5 6
  104. cout << "Target: ";
  105. printList(head3);
  106. cout << endl;
  107.  
  108. Node *sublist1 = nullptr;
  109. listInsertHead(1, sublist1); //1
  110. cout << "Attempt match: ";
  111. printList(sublist1);
  112. if (isSubList(head3, sublist1) != nullptr)
  113. printList(isSubList(head3, sublist1));
  114.  
  115. Node *sublist2 = nullptr;
  116. listInsertHead(9, sublist2);
  117. listInsertHead(3, sublist2); //3 9
  118. cout << "Attempt match: ";
  119. printList(sublist2);
  120. if (isSubList(head3, sublist2) != nullptr)
  121. printList(isSubList(head3, sublist2));
  122.  
  123. Node *sublist3 = nullptr;
  124. listInsertHead(3, sublist3);
  125. listInsertHead(2, sublist3); //2 3
  126. cout << "Attempt match: ";
  127. printList(sublist3);
  128. if (isSubList(head3, sublist3) != nullptr)
  129. printList(isSubList(head3, sublist3));
  130.  
  131. Node *sublist4 = nullptr;
  132. listInsertHead(6, sublist4);
  133. listInsertHead(5, sublist4);
  134. listInsertHead(4, sublist4);
  135. listInsertHead(2, sublist4); //2 4 5 6
  136. cout << "Attempt match: ";
  137. printList(sublist4);
  138. if (isSubList(head3, sublist4) != nullptr)
  139. printList(isSubList(head3, sublist4)); //multiple instances of 2
  140.  
  141. Node *sublist5 = nullptr;
  142. listInsertHead(4, sublist5);
  143. listInsertHead(2, sublist5);
  144. listInsertHead(3, sublist5);
  145. listInsertHead(2, sublist5); //2 3 2 4
  146. cout << "Attempt match: ";
  147. printList(sublist5);
  148. if (isSubList(head3, sublist5) != nullptr)
  149. printList(isSubList(head3, sublist5)); //multiple instances of 2
  150.  
  151. Node *sublist6 = nullptr;
  152. listInsertHead(7, sublist6);
  153. listInsertHead(6, sublist6);
  154. listInsertHead(5, sublist6); //5 6 7
  155. cout << "Attempt match: ";
  156. printList(sublist6);
  157. //if (isSubList(head3, sublist6) != nullptr)
  158. // printList(isSubList(head3, sublist6)); //beyond scope of L1
  159.  
  160. system("pause");
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement