473,387 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Problem with a list operation

35
Hello,

I'm having issues dealing with lists, in this case sets of numbers.
I'm trying to get the difference between two lists, say S1 and S2.
So "S3 = S1 - S2" in my main file will produce a new set with the difference between the two. The "=" and "-" operator have been written by me.
The problem is this: S1 before the operation and S1 just after the operation are not the same. So I'm messing something up. Here is my "-" operator:

Expand|Select|Wrap|Line Numbers
  1. Set Set::operator- (const Set& b)
  2. {
  3.     Set tempSet;
  4.     Set a = *this;
  5.     Node* a_temp = a.header;
  6.     Node* b_temp = b.header;
  7.     Node* this_temp = tempSet.header;
  8.  
  9.  
  10.     while(a_temp!=0 && b_temp!=0)
  11.     {
  12.                   if (a_temp->value == b_temp->value)
  13.                   {
  14.                      a_temp = a_temp->next;
  15.                      b_temp = b_temp->next;
  16.                   }
  17.                   else if (a_temp->value < b_temp->value)
  18.                   {
  19.                        if (tempSet.header==0)
  20.                        {
  21.                              tempSet.header = new Node(a_temp->value, tempSet.header);
  22.                              this_temp = tempSet.header;
  23.                              a_temp = a_temp->next;
  24.                        }
  25.                        else
  26.                        {
  27.                              this_temp->next = new Node(a_temp->value, this_temp->next);
  28.                              this_temp = this_temp->next;
  29.                              a_temp = a_temp->next;
  30.                        }
  31.                   }
  32.                   else if (a_temp->value > b_temp->value)
  33.                   {
  34.                        b_temp = b_temp->next;
  35.                   }
  36.     }
  37.  
  38.     while (a_temp!=0)
  39.     {
  40.           this_temp->next = new Node(a_temp->value, this_temp->next);
  41.           this_temp = this_temp->next;
  42.           a_temp = a_temp->next;
  43.     }
  44.  
  45.     return tempSet;
  46. }
Expand|Select|Wrap|Line Numbers
  1. And the "=" operator in case that should be the issue:
  2. Set& Set::operator=(const Set& b)
  3. {
  4.      if ((this == &b) || (b.header==0))
  5.      {
  6.               return *this;
  7.      }
  8.      Node* temp = header;
  9.      while (temp!=0)
  10.      {
  11.            header = temp -> next;
  12.            delete temp;
  13.            temp = header;
  14.      }
  15.      Node* b_temp = b.header;
  16.      temp = header;
  17.      header = new Node(b.header->value, header);
  18.      b_temp = b_temp->next;
  19.      while(b_temp != 0)
  20.      {
  21.                   insert(b_temp->value); 
  22.                   b_temp = b_temp -> next;
  23.      }
  24.      return *this;
  25. }
I should mention that if I check what the "this"-set is at the end of the subtraction function, it looks just fine, but as soon as I'm back in the main file it's messed up.

Big thanks in advance if anyone is bored enough to help me :)
May 3 '07 #1
1 1130
JosAH
11,448 Expert 8TB
Note that this line:
Expand|Select|Wrap|Line Numbers
  1. Set a = *this;
does not involve the operator= function. I think your code suffers a bit from
incorrect value sematics. The operator- in A-B should result in a new object
containing the difference of A and B while A and B themselves should stay
untouched. For the first line (see above) you need a copy constructor that
does a deep copy, i.e. all relevant members of the class should be a new copy
of the original one too.

kind regards,

Jos
May 3 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Brett L. Moore | last post by:
Hi, I have had trouble determining whether the STL list.size() operation is O(1) or O(n). I know the list is a doubly-linked list, so if the size() operation begins at the head, then counts to...
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a...
6
by: Wescotte | last post by:
I'm writing a tiny php app that will log into our bank of america account and retrieve a file containing a list of checks that cleared the previous day. The problem I'm running into is when I...
1
by: Rohit Raghuwanshi | last post by:
Hello all, we are running a delphi application with DB2 V8.01 which is causing deadlocks when rows are being inserted into a table. Attaching the Event Monitor Log (DEADLOCKS WITH DETAILS) here....
3
by: Larry | last post by:
I've set up a list of image filesnames in a listview control(lvSrcFileList), I'm using the selectedIndexChange event to select which file is being displayed in a picturebox control(pbImagePreview)....
1
by: Parag Mahajan | last post by:
Hello, I am getting exception when my .Net client tries to deserialize the response received. The excpetion thrown is: Unhandled Exception: System.InvalidOperationException: There is an error...
1
by: Matthew | last post by:
Using .NET 1.1 I trying to consume a web service that returns an integer and an array of type string which I declare and use line this Dim Results() As String Me.txtMain.Text =...
0
by: NiveditaB06 | last post by:
Can anyone please help me? I tried this problem , it is giving a runtime error in the below mentioned line. _observers->push_back(o); Anyone have any explanation for the reason & if anyone knows...
10
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
19
by: Juha Nieminen | last post by:
If I'm not completely mistaken, the only reason why std::list::size() may be (and usually is) a linear-time operation is because they want std::list::splice() to be a constant-time operation, and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.