Advertisement
gpric001

CS 12 SI w7_2

Feb 24th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.07 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct IntNode {
  5.     int data;
  6.     IntNode* next;
  7.     IntNode(int data) : data(data), next(0) {}
  8. };
  9.  
  10.  
  11. //Problem 1
  12. //---------------------------------------------------------------------------
  13. //Define a function with the following declaration that takes as input an array
  14. //populates a linked list with those numbers, and returns the head of the list
  15. IntNode* populateList(int a[], int size);
  16.  
  17. //Problem 2
  18. //---------------------------------------------------------------------------
  19. //Define a function with the following declaration that creates a new linked
  20. //list that is the union of list1 and list2.
  21. //Union is defined as the elements that are in list1 or list2 or both.
  22. //Example:
  23. //  list1 = 1->2->3->4
  24. //  list2 = 2->4->6->8
  25. //  myUnion(list1, list2) = 1->2->3->4->6->8
  26. //
  27. //Note that elements are not added twice
  28. IntNode* myUnion(IntNode* list1, IntNode* list2);
  29.  
  30.  
  31. //Problem 3
  32. //---------------------------------------------------------------------------
  33. //Define a function with the following declaration that creates a new linked
  34. //list that is the intersection of list1 and list2.
  35. //Intersection is defined as the elements that are in list1 and in list2.
  36. //Example:
  37. //  list1 = 1->2->3->4
  38. //  list2 = 2->4->6->8
  39. //  myIntersection (list1, list2) = 2->4
  40. IntNode* myIntersection(IntNode* list1, IntNode* list2);
  41.  
  42.  
  43. //Problem 4
  44. //---------------------------------------------------------------------------
  45. //Define a function with the following declaration that reverses a linked list
  46. //Example:
  47. //  list1 = 1->2->3->4
  48. //  myReverse(list1)
  49. //  list1 = 4->3->2->1
  50. void myReverse(IntNode* list);
  51.  
  52.  
  53. //Problem 5
  54. //---------------------------------------------------------------------------
  55. //Create a function based on the following declaration that returns a
  56. //pointer to the middle element of a list.
  57. IntNode* findMid(IntNode *list);
  58.  
  59. //Bonus: The above problem is actually a common interview question.
  60. //You likely wrote that function by looping through the list multiple
  61. //times. Is it possible to accomplish this same functionality
  62. //by only looking at each node ONCE?
  63.  
  64. int main() {
  65.     const int SIZE_OF_A = 9;
  66.     const int SIZE_OF_B = 9;
  67.     int a[] = {5,6,3,4,9,8,2,3,0};
  68.     int b[] = {3,8,9,0,5,4,3,10,15};
  69.     IntNode* list1 = populateList(a, SIZE_OF_A);
  70.     IntNode* list2 = populateList(b, SIZE_OF_B);
  71.     IntNode* list3 = myUnion(list1, list2);
  72.     IntNode* list4 = myIntersection(list1, list2);
  73.     myReverse(list4);
  74.     IntNode* mid = findMid(list3);
  75.    
  76.     return 0;
  77. }
  78.  
  79. //Stub for Problem 1
  80. IntNode* populateList(int a[], int size){
  81.     IntNode* head = new IntNode(0);
  82.     return head;
  83. }
  84.  
  85. //Stub for Problem 2
  86. IntNode* myUnion(IntNode* list1, IntNode* list2){
  87.     IntNode* head = new IntNode(0);
  88.     return head;
  89. }
  90.  
  91. //Stub for Problem 3
  92. IntNode* myIntersection(IntNode* list1, IntNode* list2){
  93.     IntNode* head = new IntNode(0);
  94.     return head;
  95. }
  96.  
  97. //Stub for Problem 4
  98. void myReverse(IntNode* list1){
  99.     return;
  100. }
  101.  
  102. //Stub for Problem 5
  103. IntNode* findMid(IntNode*){
  104.     IntNode* head = new IntNode(0);
  105.     return head;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement