Error with custom swap function

I am supposed to build my own swap function for my class. It runs, but it doesn't produce the correct output. I would greatly appreciate any help or advice on what I am doing wrong. Thanks in advance for your time!


List alist and newa before swap 
 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 
 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 
List alist and newa after swap 
 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 
 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
int main()
{
//code before...

    List newa;
    for (int i=1;i<=20;i++)
	   newa.insertAfter(i*3);

    cout << "List alist and newa before swap " << endl;
    cout << " " << alist << endl;
    cout << " " << newa << endl;

    //Swap the two lists
    alist.swap(newa);

    cout << "List alist and newa after swap " << endl;
    cout << " " << alist << endl;
    cout << " " << newa << endl;

//more code follows...

    //Exit program
    return EXIT_SUCCESS;
}

//Function to swap the two lists
void List::swap(List CurrentList)
{
    // Create temporary holder
    List temp;

    // Populate temp list with current list
    for (int i = 0; i < CAPACITY; i++)
    {
        temp.MyAry[i]= CurrentList.MyAry[i];
    }

    // Replace current list with other list
    for (int i = 0; i < CAPACITY; i++)
    {
        CurrentList.MyAry[i] = MyAry[i];
    }

    // Replace second list with temp list
    for (int i = 0; i < CAPACITY; i++)
    {
        MyAry[i] = temp.MyAry[i];
    }
}

I think that the problem is that you are using the same value of CAPACITY for two different lists..
Last edited on
Capacity is defined as:

 
static const ET CAPACITY = 20; //Size of the array 
Oh, I am sorry.

You should declare the function as

void List::swap(List &CurrentList);

that is you should pass a list by reference.
I figured it out. It was changing the variable, but when it left the function, it was gone because I didn't pass it properly.
Topic archived. No new replies allowed.