Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct IntNode {
- int data;
- IntNode* next;
- IntNode(int data) : data(data), next(0) {}
- };
- //Problem 1
- //---------------------------------------------------------------------------
- //Define a function with the following declaration that takes as input an array
- //populates a linked list with those numbers, and returns the head of the list
- IntNode* populateList(int a[], int size);
- //Problem 2
- //---------------------------------------------------------------------------
- //Define a function with the following declaration that creates a new linked
- //list that is the union of list1 and list2.
- //Union is defined as the elements that are in list1 or list2 or both.
- //Example:
- // list1 = 1->2->3->4
- // list2 = 2->4->6->8
- // myUnion(list1, list2) = 1->2->3->4->6->8
- //
- //Note that elements are not added twice
- IntNode* myUnion(IntNode* list1, IntNode* list2);
- //Problem 3
- //---------------------------------------------------------------------------
- //Define a function with the following declaration that creates a new linked
- //list that is the intersection of list1 and list2.
- //Intersection is defined as the elements that are in list1 and in list2.
- //Example:
- // list1 = 1->2->3->4
- // list2 = 2->4->6->8
- // myIntersection (list1, list2) = 2->4
- IntNode* myIntersection(IntNode* list1, IntNode* list2);
- //Problem 4
- //---------------------------------------------------------------------------
- //Define a function with the following declaration that reverses a linked list
- //Example:
- // list1 = 1->2->3->4
- // myReverse(list1)
- // list1 = 4->3->2->1
- void myReverse(IntNode* list);
- //Problem 5
- //---------------------------------------------------------------------------
- //Create a function based on the following declaration that returns a
- //pointer to the middle element of a list.
- IntNode* findMid(IntNode *list);
- //Bonus: The above problem is actually a common interview question.
- //You likely wrote that function by looping through the list multiple
- //times. Is it possible to accomplish this same functionality
- //by only looking at each node ONCE?
- int main() {
- const int SIZE_OF_A = 9;
- const int SIZE_OF_B = 9;
- int a[] = {5,6,3,4,9,8,2,3,0};
- int b[] = {3,8,9,0,5,4,3,10,15};
- IntNode* list1 = populateList(a, SIZE_OF_A);
- IntNode* list2 = populateList(b, SIZE_OF_B);
- IntNode* list3 = myUnion(list1, list2);
- IntNode* list4 = myIntersection(list1, list2);
- myReverse(list4);
- IntNode* mid = findMid(list3);
- return 0;
- }
- //Stub for Problem 1
- IntNode* populateList(int a[], int size){
- IntNode* head = new IntNode(0);
- return head;
- }
- //Stub for Problem 2
- IntNode* myUnion(IntNode* list1, IntNode* list2){
- IntNode* head = new IntNode(0);
- return head;
- }
- //Stub for Problem 3
- IntNode* myIntersection(IntNode* list1, IntNode* list2){
- IntNode* head = new IntNode(0);
- return head;
- }
- //Stub for Problem 4
- void myReverse(IntNode* list1){
- return;
- }
- //Stub for Problem 5
- IntNode* findMid(IntNode*){
- IntNode* head = new IntNode(0);
- return head;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement