Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I improve my performance with this singly linked list struct within my program?
    primarykey
    data
    text
    <p>Hey guys, I have a program that does operations of sets of strings. I have to implement functions such as addition and subtraction of two sets of strings. I need to get it down to the point where performance if of O(N+M), where N,M are sets of strings. Right now, I believe my performance is at O(N*M), since I for each element of N, I go through every element of M. I'm particularly focused on getting the subtraction to the proper performance, as if I can get that down to proper performance, I believe I can carry that knowledge over to the rest of things I have to implement.</p> <p>The '-' operator is suppose to work like this, for example.</p> <p>Declare set1 to be an empty set.<br> Declare set2 to be a set with { a b c } elements<br> Declare set3 to be a set with ( b c d } elements </p> <p>set1 = set2 - set3 </p> <p>And now set1 is suppose to equal { a }. So basically, just remove any element from set3, that is also in set2.</p> <p>For the addition implementation (overloaded '+' operator), I also do the sorting of the strings (since we have to). </p> <p>All the functions work right now btw.</p> <p>So I was wondering if anyone could </p> <p>a) Confirm that currently I'm doing O(N*M) performance<br> b) Give me some ideas/implementations on how to improve the performance to O(N+M) </p> <p>Note: I cannot add any member variables or functions to the class strSet or to the node structure.</p> <p>The implementation of the main program isn't very important, but I will post the code for my class definition and the implementation of the member functions:</p> <p><strong>strSet2.h (Implementation of my class and struct)</strong></p> <pre><code>// Class to implement sets of strings // Implements operators for union, intersection, subtraction, // etc. for sets of strings // V1.1 15 Feb 2011 Added guard (#ifndef), deleted using namespace RCH #ifndef _STRSET_ #define _STRSET_ #include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;string&gt; // Deleted: using namespace std; 15 Feb 2011 RCH struct node { std::string s1; node * next; }; class strSet { private: node * first; public: strSet (); // Create empty set strSet (std::string s); // Create singleton set strSet (const strSet &amp;copy); // Copy constructor ~strSet (); // Destructor int SIZE() const; bool isMember (std::string s) const; strSet operator + (const strSet&amp; rtSide); // Union strSet operator - (const strSet&amp; rtSide); // Set subtraction strSet&amp; operator = (const strSet&amp; rtSide); // Assignment }; // End of strSet class #endif // _STRSET_ </code></pre> <p><strong>strSet2.cpp (implementation of member functions)</strong></p> <pre><code>#include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;string&gt; #include "strset2.h" using namespace std; strSet::strSet() { first = NULL; } strSet::strSet(string s) { node *temp; temp = new node; temp-&gt;s1 = s; temp-&gt;next = NULL; first = temp; } strSet::strSet(const strSet&amp; copy) { if(copy.first == NULL) { first = NULL; } else { node *n = copy.first; node *prev = NULL; while (n) { node *newNode = new node; newNode-&gt;s1 = n-&gt;s1; newNode-&gt;next = NULL; if (prev) { prev-&gt;next = newNode; } else { first = newNode; } prev = newNode; n = n-&gt;next; } } } strSet::~strSet() { if(first != NULL) { while(first-&gt;next != NULL) { node *nextNode = first-&gt;next; first-&gt;next = nextNode-&gt;next; delete nextNode; } } } int strSet::SIZE() const { int size = 0; node *temp = first; while(temp!=NULL) { size++; temp=temp-&gt;next; } return size; } bool strSet::isMember(string s) const { node *temp = first; while(temp != NULL) { if(temp-&gt;s1 == s) { return true; } temp = temp-&gt;next; } return false; } strSet strSet::operator + (const strSet&amp; rtSide) { strSet newSet; newSet = *this; node *temp = rtSide.first; while(temp != NULL) { string newEle = temp-&gt;s1; if(!isMember(newEle)) { if(newSet.first==NULL) { node *newNode; newNode = new node; newNode-&gt;s1 = newEle; newNode-&gt;next = NULL; newSet.first = newNode; } else if(newSet.SIZE() == 1) { if(newEle &lt; newSet.first-&gt;s1) { node *tempNext = newSet.first; node *newNode; newNode = new node; newNode-&gt;s1 = newEle; newNode-&gt;next = tempNext; newSet.first = newNode; } else { node *newNode; newNode = new node; newNode-&gt;s1 = newEle; newNode-&gt;next = NULL; newSet.first-&gt;next = newNode; } } else { node *prev = NULL; node *curr = newSet.first; while(curr != NULL) { if(newEle &lt; curr-&gt;s1) { if(prev == NULL) { node *newNode; newNode = new node; newNode-&gt;s1 = newEle; newNode-&gt;next = curr; newSet.first = newNode; break; } else { node *newNode; newNode = new node; newNode-&gt;s1 = newEle; newNode-&gt;next = curr; prev-&gt;next = newNode; break; } } if(curr-&gt;next == NULL) { node *newNode; newNode = new node; newNode-&gt;s1 = newEle; newNode-&gt;next = NULL; curr-&gt;next = newNode; break; } prev = curr; curr = curr-&gt;next; } } } temp = temp-&gt;next; } return newSet; } strSet strSet::operator - (const strSet&amp; rtSide) { strSet newSet; newSet = *this; node *temp = rtSide.first; while(temp != NULL) { string element = temp-&gt;s1; node *prev = NULL; node *curr = newSet.first; while(curr != NULL) { if( element &lt; curr-&gt;s1 ) break; if( curr-&gt;s1 == element ) { if( prev == NULL) { node *duplicate = curr; newSet.first = newSet.first-&gt;next; delete duplicate; break; } else { node *duplicate = curr; prev-&gt;next = curr-&gt;next; delete duplicate; break; } } prev = curr; curr = curr-&gt;next; } temp = temp-&gt;next; } return newSet; } strSet&amp; strSet::operator = (const strSet&amp; rtSide) { if(this != &amp;rtSide) { if(first != NULL) { while(first-&gt;next != NULL) { node *nextNode = first-&gt;next; first-&gt;next = nextNode-&gt;next; delete nextNode; } } if(rtSide.first == NULL) { first = NULL; } else { node *n = rtSide.first; node *prev = NULL; while (n) { node *newNode = new node; newNode-&gt;s1 = n-&gt;s1; newNode-&gt;next = NULL; if (prev) { prev-&gt;next = newNode; } else { first = newNode; } prev = newNode; n = n-&gt;next; } } } return *this; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload