in Education by
Tm trying to get the difference of 2 sets in linked list Example: Input: Set A: 1 - 2 - 3 - 5 Set B: 2 - 4 - 5 Output: Union: 5 - 4 - 2 - 5 - 3 - 2 - 1 Intersection: 5 - 2 Difference: 3 - 4 - 1 (Problem here) Mine outputs Difference: 5 - 3 - 2 - 1 This code is under "LinkedList.h" #include "Node.h" #include using namespace std; class LinkedList { public: Node *head; LinkedList() { head = NULL; } bool find(Node *value) { int cnt(0); Node *iterator = head; while(iterator != NULL) { if(value->data == iterator->data) return 1; iterator = iterator->next; } return cnt; } void insertBeginning(int value) { Node *newNode = new Node; newNode->data = value; if(find(newNode) != 1) { Node *temp = head; head = newNode; newNode->next = temp; } } void deleteBeginning() { if(head != NULL) { Node *temp = head->next; head = temp; } } void display() { Node *iterator = head; while (iterator != NULL) { cout<<iterator->data<<" ->"; iterator = iterator->next; } cout<<" end"; } bool isEmpty() { int c; head == NULL ? c = 1: c = 0; return c; } }; class Set:public LinkedList { public: void Union(Set& myListA,Set& myListB) { for(Node* iterator1 = myListA.head; iterator1 != NULL; iterator1 = iterator1->next) { LinkedList::insertBeginning(iterator1->data); } for(Node* iterator2 = myListB.head; iterator2 != NULL; iterator2 = iterator2->next) { LinkedList::insertBeginning(iterator2->data); } } void Intersection(Set& myListA, Set& myListB) { for(Node* iterator3 = myListA.head; iterator3 != NULL; iterator3 = iterator3->next) { //if(LinkedList::find(iterator3) != 1) for(Node* iterator4 = myListB.head; iterator4 != NULL; iterator4 = iterator4->next) { //if(LinkedList::find(iterator4) != 1) if( iterator3->data == iterator4->data ) { LinkedList::insertBeginning(iterator4->data); } } } } void Difference(Set& myListA, Set& myListB) { for(Node* iterator5 = myListA.head; iterator5 != NULL; iterator5 = iterator5->next) { for(Node* iterator6 = myListB.head; iterator6 != NULL; iterator6 = iterator6->next) { if (iterator6->data != iterator5->data && LinkedList::find(iterator5) != 1 && LinkedList::find(iterator6) != 1) { LinkedList::insertBeginning(iterator5->data); } else { continue; } } } } }; and this is under "Node.h" class Node { public: int data; Node *next; }; JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Your implementation of Difference is far too complicated: since your lists are sorted, all you need to do is to find mismatching elements. This requires one loop where in each iteration you move both iterators if the element is in both sets, i.e. it isn't part of the difference the iterator for the first list in which case you have an element which is in the first set but not in the second set the iterator for the second list in which case you have an element which is in the second set but not in the first set Your implementation of Intersection is likewise too complicated and also needs just one loop: it would just store the common value in the cases where no element is stored for Difference(). Finally, Union() is, again, too complicated: it would store an element in every iteration, either the common one or the one skipped depending on which branch is taken. This would also yield a correct result. Obviously, what you really want to use is std::set_intersection(s0.begin(), s0.end(), s1.begin(), s1.end(), std::back_inserter(result_intersection)); std::set_union(s0.begin(), s0.end(), s1.begin(), s1.end(), std::back_inserter(result_union)); std::set_symmetric_difference(s0.begin(), s0.end(), s1.begin(), s1.end(), std::back_inserter(result_difference)); assuming you have given your lists and iterators a standard interface.

Related questions

0 votes
    Closed. This question needs debugging details. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    Closed. This question needs debugging details. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    A person tosses a coin 3 times. Find the probability of occurrence of exactly one head. Select the correct answer from above options...
asked Nov 25, 2021 in Education by JackTerrance
0 votes
    If A and B be two events and 2P(B) = P(A) = 6/13 and P(A/B) = 1/3 then find P(A ∪ B). Select the correct answer from above options...
asked Nov 25, 2021 in Education by JackTerrance
0 votes
    Which of these standard collection classes implements a linked list data structure? (a) AbstractList (b) LinkedList ... of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    which linked list is used in managing memory in data structure? Select the correct answer from above options...
asked Dec 10, 2021 in Education by JackTerrance
0 votes
    Which of these standard collection classes implements a linked list data structure? (a) AbstractList (b) ... questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    I am implementing a display algorithm where we can have multiple layers of windows based on their z-order i ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 28, 2022 in Education by JackTerrance
0 votes
    I am implementing a display algorithm where we can have multiple layers of windows based on their z-order i ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 27, 2022 in Education by JackTerrance
0 votes
    I am implementing a display algorithm where we can have multiple layers of windows based on their z-order i ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 27, 2022 in Education by JackTerrance
0 votes
    As far as I know, in gcc you can write something like: #define DBGPRINT(fmt...) printf(fmt); Is ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    I'm working on a fairly complex project, a custom encryption routine if you will (just for fun) ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I'm working on a fairly complex project, a custom encryption routine if you will (just for fun) ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I'm working on a fairly complex project, a custom encryption routine if you will (just for fun) ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I'm working on a fairly complex project, a custom encryption routine if you will (just for fun) ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 18, 2022 in Education by JackTerrance
...