Main can't be altered, functions aren't correct

I have this General List program from my instructor. I can't alter the main code. I have all the functions and overloads written, but when I compile the code in Visual Studio I get a bunch of errors in the main code. Could someone look it over? I'm uncertain how to correct said issues. Below is the full code and the spec sheet.


Code will be pasted in comments. Too large for the window
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
51
52
53
54
55
56
57
58
59
60
61
General List Program



You are to implement a 'List' class to handle a list with general operations.  
That means you can insert and delete any element anywhere in the list.  
The list has no order, except for the order you insert or delete.   
The methods you are to implement are as given:

Constructor methods (two, default and copy constructor, 
a list to a newly defined list, ie 'List listA(listB)' )

empty returns true or false if list is empty or not.

front makes current position at the beginning of the list

end makes current position at the end of a list.

prev places current position at the previous element in the list

next places current position at the next element in the list

getPos returns current position or where you are in the list

setPos(int) places current position in a certain position in the list

insertBefore  inserts a new element before the current position

insertAfter inserts a new element after the current position

get Element returns the one element that current position is pointing to

size returns the size of the list (number of elements in list)

replace(int) replace the current element with a new value

erase deletes the current element

clear makes the list an empty list

reverse   reverse elements in a list

swap  swaps all the elements of one list with the another list.

overload the operators:  (at least)    <<  output,  == ,  + ,  = (assignment)

You are to implement the List class using an array.  
For now the array can be 20 in size.
You will need to use the 'ElementType' for 'typing' your data.
You will need to use CAPACITY constant for the size of the array, 
in case we need to change the array size.

Invariants:  The elements in the list should be left justified.  
‘pos’ should point to the element just inserted. 
 ‘pos’ should never be outside of element list.

I will supply you a main program to test your code with. 
 I will supply you a main program at a later time.  
Do not wait for my code to test your program, but test your code with your own main program.

Last edited on
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include <iostream>
#include<iomanip>

using namespace std;

const int CAPACITY = 20;
typedef int ElementType;
typedef size_t size_type;

class List
{
private:
     ElementType myAry[CAPACITY];
     size_type used;
     int pos;

public:
     List()
     {
         clear();
         pos=0;
     }

     List(List& aryList);
     List operator = (List& aryListB);
     bool empty();
     void first(); 
     void last();
     void prev();
     void next();   
     int getPos();
     void setPos(int);     
     void insertBefore();
     void insertAfter();
     int getElement();
     int size();
     void replace(int); 
     void erase();
     void clear();
};

ostream& operator << (ostream& outs, List& aryList);
List operator + (List& ListA, List& ListB);  
bool operator == (List aryList1, List aryList2);

bool List::empty() 
{
     if (size() == 0)
          return true;

     else
          return false;
}

void List::first() //makes current position at the beginning of the list
{  
      pos = 0; 
}

void List::last()  //makes current position at the end of the list
{
     pos=used-1;
}

void List::prev()   //places current position at the previous element in the list 
{
    if (pos>0)
     pos--; 
}

void List::next()  //places current position at the next element of the list 
{
     if (pos < (used - 1))
         pos++;
}

int List::getPos()   //returns current position or where you are in the list 
{
   return pos;
}

void List::setPos(int newPos)  //places current position in a certain position in the list 
{
    if (newPos >= 0 && newPos < used)
        pos = newPos;
}

void List::insertBefore()  //inserts a new element before the current position
{
     int a;
   
   if (pos >= CAPACITY)
        cout << "Cannot insert a new element, capacity reached.\n";

    else 
    {
       for (int i=pos; i<CAPACITY; i++) 
        {
          myAry[pos]=a;
        }
          if (pos == 0) 
          {
             myAry[pos] = a;
          }
          else 
          {
               myAry[pos - 1]= a;
           }
         pos++;
         used++;
       }
}

void List::insertAfter()  //inserts a new element after the current position
{
    int a;
    if (pos >= CAPACITY)
        cout << "Cannot insert a new element, capacity reached.\n";

    else 
    {
       for (int i=pos; i<CAPACITY; i++) 
        {
          myAry[pos+1]=a;
  
        }
          if (used == 0) 
          {
             myAry[pos+1]= a;
          }
          else 
          {
             myAry[pos + 1] = a;
             pos++;
           }
        used++;
       }
}

int List::getElement()  //returns the one element that current position is pointing to 
{
    if (size() <= 0)
        return 0;

    else
        return myAry[pos];
}

int List::size()  //returns the size of the list (number of elements in the list)
{
   return used;
}

void List::replace(int a)  //replace the current element with a new (x, y) value
{
   myAry[pos] = a;
}

void List::erase()  //deletes the current element 
{
   if (used>0)
    {
         myAry[pos] = 0;
    } 
}

void List::clear()   //makes the list an empty list
{
   used = 0;
   pos = 0;

  for (int i = 0; i < CAPACITY; i++)
   {
     myAry[i] = 0;
   }
}

List::List(List& aryListB)
{
    pos=0;
    this->clear();
    int orig_pos = aryListB.getPos();
    aryListB.first();
    
for (int i = 0; i < aryListB.size(); aryListB.next(), i++) 
{
   this->myAry[i] = aryListB.getElement();
   this->used++;
}
this->pos = orig_pos;
aryListB.setPos(orig_pos);
}

ostream& operator <<(ostream& outs, List& aryList)  //to print out in ( , )
{
   aryList.first();
   if(aryList.empty())
   {
       cout<<"---------";
   }
   
   else
   {
    for (int i = 0; i < aryList.size(); aryList.next(), i++) 
    {
       outs<<"("<<aryList.getElement() << "," << ")";
    }
   } 
    return outs;
}

List operator+(List& ListA, List& ListB)  //adding two lists together 
{
    List ListC(ListA);
    int orig_pos = ListB.getPos();
    ListC.first();
    ListB.first();

  for (int i = 0; i < ListB.size(); ListB.next(), ListC.next(), i++) 
  {
    ListC.replace(ListB.getElement() + ListC.getElement());
  }
    ListB.setPos(orig_pos);
    return ListC;
}

bool operator == (List aryList1, List aryList2) //comparing two lists if they are equal 
{
  for (int i = 0; i < aryList1.size(); i++) 
{     
   if (aryList1.getElement() == aryList2.getElement()) 
        return true;
   
   else
       return false;
 }
}
  
bool operator != (List aryList1, List aryList2)  //comparing two lists if they are not equal 
{
   if (aryList1 == aryList2)
       return false;
   
   else
       return true;
}

List List::operator = (List& aryListB)  //assignment operator 
{
   int orig_pos = aryListB.getPos();
   aryListB.first();
   used = 0;

  for (int i = 0; i < aryListB.size(); aryListB.next(), this->next(), i++) 
  {
    this->myAry[i] = aryListB.getElement();
  }
     pos = orig_pos;
     aryListB.setPos(orig_pos);
     return aryListB;
}

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
int main()
{
     List a, b;  int endit;

     for (int i = 1; i <= 20; i++)
          a.insertAfter(i * 2);
     cout << "List a : " << endl;
     cout << "  " << a << endl;
     cout << "Number of elements in a - " << a.size() << endl;

     for (int i = 1; i <= 10; i++)
          b.insertBefore(i * 3);
     cout << "List b : " << endl;
     cout << "  " << b << endl;
     cout << "Number of elements in b - " << b.size() << endl;

     if (a == b)
          cout << "List a & b are equal" << endl;
     else
          cout << "List a & b are Not equal" << endl;

     a.first();
     b.first();
     cout << "First elmenet in list a & b: " << a.getElement() << ", "
          << b.getElement() << endl;
     a.last();
     b.last();
     cout << "Last elmenet in list a & b: " << a.getElement() << ", "
          << b.getElement() << endl;

     cout << endl << endl << " Start of new stuff" << endl;

     b.clear();
     cout << "Empty List b:  " << b << endl;

     if (a == b)
          cout << "List a & b are equal" << endl;
     else
          cout << "List a & b are Not equal" << endl;

     for (int i = 1; i <= 10; i++)
          b.insertBefore(i * 5);
     cout << "List b : " << endl << b << endl;

     a.setPos(5);
     b.first();
     for (int i = 1; i < 4; i++)
     {
          a.erase();
          b.replace(i);
          b.next();
     }

     cout << "Modified Object 'a' (shorter) " << endl;
     cout << "List a: " << a << endl;
     cout << "Modified Object 'b' " << endl;
     cout << "List b: " << b << endl;

     List c(b);
     cout << "Copy Constructor c(b)" << endl;
     cout << "List b : " << b << endl;
     cout << "List c : " << c << endl;

     if (c == b)
          cout << "List c & b are equal" << endl;
     else
          cout << "List c & b are Not equal" << endl;


     List e;
     e = c;
     cout << "Object 'c' assigned to Object 'e':" << endl;
     cout << "List c : " << c << endl;
     cout << "List e : " << e << endl;

     List d;
     d = a;

     d.first();
     endit = d.size();
     for (int i = 1; i < endit; d.next(), i++)
     {
          d.insertBefore(d.getElement() * (-2));
          d.next();
     }
     cout << "Results after some Replaces on d " << endl;
     cout << "List d : " << d << endl;

     a.first();
     endit = a.size();
     for (int i = 1; i < endit; a.next(), i++)
     {
          a.replace(a.getPos() + a.getElement());
          a.next();
     }
     cout << "Results after some weird stuff on list a" << endl;
     cout << "List a : " << a << endl;

     List alist(b);
     alist.clear();
     for (int i = 1; i <= 10; i++)
          alist.insertAfter(i);
     alist.first();
     cout << "New List alist with positions above: " << endl;
     for (int i = 1; i <= 10; i++) {
          cout << setw(5) << alist.getPos();
          alist.next();
     }
     cout << endl;
     alist.first();
     for (int i = 1; i <= 10; i++) {
          cout << setw(5) << alist.getElement();
          alist.next();
     }
     cout << endl;

     cout << endl << "  check out boundary conditions" << endl;
     List sq;
     cout << "number of elements in empty sq list = " << sq.size() << endl;
     sq.first();
     sq.erase();
     sq.setPos(5);
     cout << "empty sq values " << sq << endl;
     sq.insertBefore(777);
     cout << "sq values " << sq << endl;
     sq.next(); sq.next();
     cout << "sq.getElement() = " << sq.getElement() << endl;
     cout << "sq list = " << sq << endl;
     return 0;
}
Last edited on

void insertAfter(); // This function/method is telling me that it does not take any parameters.

However, here, a.insertAfter(i * 2); you are placing the parameters (i * 2), but you do not have a function in the code to take those parameters. Hence, the compiler will complain.

The same situation with void insertBefore(); as I just mentioned about the parameters issue.

I just play with it and it could give you an idea to at least compile it so you can keep debugging:

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
void List::insertAfter(int a)  //inserts a new element after the current position
{
    //int a;
    if (pos >= CAPACITY)
        cout << "Cannot insert a new element, capacity reached.\n";

    else
    {
        for (int i = pos; i < CAPACITY; i++)
        {
            myAry[pos + 1] = a;

        }
        if (used == 0)
        {
            myAry[pos + 1] = a;
        }
        else
        {
            myAry[pos + 1] = a;
            pos++;
        }
        used++;
    }
}


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
void List::insertBefore(int a)  //inserts a new element before the current position
{
    //int a;

    if (pos >= CAPACITY)
        cout << "Cannot insert a new element, capacity reached.\n";

    else
    {
        for (int i = pos; i < CAPACITY; i++)
        {
            myAry[pos] = a;
        }
        if (pos == 0)
        {
            myAry[pos] = a;
        }
        else
        {
            myAry[pos - 1] = a;
        }
        pos++;
        used++;
    }
}
Last edited on
Thank you!!!! I'm always to the point when it comes to the spec sheet. His instructions didn't have arguments passed into those specific functions but his main code does. I should of seen that. I do appreciate your time!!
1
2
3
4
5
6
7
8
bool List::empty() 
{
     if (size() == 0)
          return true;

     else
          return false;
}


can be simplified as

1
2
3
4
bool List::empty()
{
    return size() == 0;
}


Also, there seems to be an issue with the insertBefore() and insertAfter(). Consider an existing list of 2 3 4 5 6 with pos at 4. If use insertAfter(1) then the expected list would be 2 3 4 1 5 6. ie the elements 5 and 6 are 'shuffled down' the list to allow 1 to be inserted after the 4 in the position originally occupied by 5. Hence there needs to be some code to 'shuffle down' the list. In this case 6 would be moved to the first unused space then 5 to the position previously occupied by 6.
With some assistance from a classmate. I've come up with the following code. I just have 5 errors, all 5 are the same.
"1>C:\Users\Owner\OneDrive\Fall 2020\CS 372 - Data Structures\3rd Program - List Class\General List\General List.cpp(285,47): error C2064: term does not evaluate to a function taking 0 arguments"

Before I moved ElementType size; from private to public, I was getting a lot more errors. So I moved it into public and getting the error above. If I do size(); I get a lot more errors.

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include<iostream>
#include<iomanip>

using namespace std;

typedef int ElementType;

class List
{
private:
     static const ElementType CAPACITY = 20;
     ElementType ary[CAPACITY];
     ElementType pos;

public:
     ElementType size;

     // Constructors.
     List();
     List(List&);

     // Modifier methods.
     void setPos(ElementType);
     void inc();
     void first();
     void last();
     void prev();
     void next();
     void insertBefore(ElementType);
     void insertAfter(ElementType);
     void replace(ElementType);
     void erase();
     void clear();

     // Accessor methods.
     ElementType getCapacity() const;
     ElementType getSize() const;
     ElementType getPos() const;
     ElementType getElement() const;
     bool empty() const;
     bool operator==(List&) const;
     bool operator!=(List&) const;
     void operator+(List&);
     void operator=(List&);
     void operator+=(List&);
     friend ostream& operator<<(std::ostream&, List&);
};

// Constructors.
List::List()
{
     clear();
}

List::List(List& l)
{
     clear();
     ElementType temp_size = l.getSize();
     l.first();
     for (ElementType i = 0; i < temp_size; i++)
     {
          insertAfter(l.getElement());
          next();
          l.next();
     }
     size = temp_size;
}

// Modifier methods.
void List::setPos(ElementType i)
{
     ElementType temp_size = getSize();
     if (size == 0)
          i = 0;
     else if (temp_size > 0 && i > temp_size - 1)
          i = temp_size - 1;
     else if (i < 0)
          i = 0;
     else
          pos = i;
}

void List::inc()
{
     size++;
}

void List::first()
{
     if (size > 0)
          pos = 0;
}

void List::last()
{
     if (size > 0)
          pos = size - 1;
}

void List::prev()
{
     if (size > 1)
          if (pos == 0)
               return;
     pos--;
}

void List::next()
{
     if (pos < (size - 1))
          pos++;
}

void List::insertBefore(ElementType val)
{
     if (size == 0)
     {
          pos = 0;
          ary[pos] = val;
          size++;
     }
     else if (size < CAPACITY)
     {
          for (ElementType i = size; i > pos - 1; i--)
               ary[i] = ary[i - 1];
          ary[pos] = val;
          size++;
     }
}

void List::insertAfter(ElementType val)
{
     if (size == 0)
     {
          pos = 0;
          ary[pos] = val;
          size++;
     }
     else if (size < CAPACITY)
     {
          for (ElementType i = size; i > pos; i--)
               ary[i] = ary[i - 1];
          ary[pos + 1] = val;
          size++;
          pos++;
     }
}

void List::replace(ElementType val)
{
     ary[pos] = val;
}

void List::erase()
{
     ary[pos] = 0;
     for (ElementType i = pos; i < size - 1; i++)
          ary[i] = ary[i + 1];
     size--;
}

void List::clear()
{
     ElementType* ptr = ary;
     for (ElementType i = 0; i < CAPACITY; i++)
     {
          *ptr = 0;
          ptr++;
     }
     size = 0;
     pos = -1;
}

// Accessor methods.
ElementType List::getCapacity() const
{
     return CAPACITY;
}

ElementType List::getSize() const
{
     return size;
}

ElementType List::getPos() const
{
     return pos;
}

ElementType List::getElement() const
{
     return ary[pos];
}

bool List::empty() const
{
     if (size == 0)
          return true;
     return false;
}

bool List::operator==(List& l) const
{
     l.first();
     for (ElementType i = 0; i < size; i++)
     {
          if (ary[i] != l.getElement())
               return false;
          l.next();
     }
     return true;
}

bool List::operator!=(List& l) const
{
     l.first();
     for (ElementType i = 0; i < size; i++)
     {
          if (ary[i] != l.getElement())
               return true;
          l.next();
     }
     return false;
}

void List::operator+(List& l)
{
     ElementType temp_size = l.getSize();
     last();
     l.first();
     for (ElementType i = size; size < temp_size; i++)
     {
          insertAfter(l.getElement());
          next();
          l.next();
     }
}

void List::operator=(List& l)
{
     ElementType temp_size = l.getSize();
     l.first();
     for (ElementType i = 0; i < temp_size; i++)
     {
          insertAfter(l.getElement());
          next();
          l.next();
     }
     size = temp_size;
}

void List::operator+=(List& l)
{
     List temp;
     temp + l;
     operator=(temp);
}

std::ostream& operator<<(std::ostream& out, List& l)
{
     ElementType temp_size = l.getSize();
     for (ElementType i = 0; i < temp_size; i++)
     {
          l.setPos(i);
          out << l.getElement() << ' ';
     }
     return out;
}
Last edited on
If I keep the size as a private this error occurs.
"1> error C2248: 'List::size': cannot access private member declared in class 'List'
1>: message : see declaration of 'List::size'
1> message : see declaration of 'List'
1>error C2064: term does not evaluate to a function taking 0 arguments"
¿what line numbers?

read your assignment
> The methods you are to implement are as given:
> size returns the size of the list (number of elements in list)
the name of the member function should be `size()', not `getSize()'
I see that you have a `size' member variable, change its name

in main() you have the usage of your List, implement those methods so they can be used as it's shown
You seem to be confusing when to you ElementType and when to use int. ElementType is the type of the elements that the list stores. int is the number of items in the list, or a for loop index. When writing the code, it's helpful to think "what if ElementType was string? If you do that, then code like this will stand out like a sore thumb:
for (ElementType i = 0; i < size; i++)

An incredibly useful technique for doing assignments is to copy the full text of the assignment into the code as a comment. Then move the comments around to the places that implement them. For example, the assignment says:
You will need to use the 'ElementType' for 'typing' your data.

So you can put that before the declaration of your data:
1
2
    // You will need to use the 'ElementType' for 'typing' your data.
    ElementType ary[CAPACITY];


If you do this, you're practically guaranteed to take care of every requirement and get every method to do exactly what it's supposed to do. For example, when I add the comment to your setPos method I get:
1
2
3
// setPos(int) places current position in a certain position in the list
void List::setPos(ElementType i)
{

Looking at this, it's clear that the parameter should be int.

More importantly, is this:
1
2
// size returns the size of the list (number of elements in list)
ElementType getSize() const;

Not only does getSize() return the wrong type but it has the wrong name. You have a size member, but "size" should be the name of the method. This required a ton of search/replace.

After doing all these changes, your class is below. Note the comments at the bottom! Those are methods that you haven't implemented yet. I haven't checked if the code you have is correct. I just got it to compile.
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include<iostream>
#include<iomanip>

using namespace std;

typedef int ElementType;

class List
{
private:
    // For now the array can be 20 in size.
     static const int CAPACITY = 20;

    // You will need to use the 'ElementType' for 'typing' your data.
    // You are to implement the List class using an array.  
    // You will need to use CAPACITY constant for the size of the array, 
    // in case we need to change the array size.
    ElementType ary[CAPACITY];
    int sz;

    // ‘pos’ should point to the element just inserted. 
    //  ‘pos’ should never be outside of element list.
    int pos;

public:

    // Constructor methods (two, default and copy constructor, 
    // a list to a newly defined list, ie 'List listA(listB)' )
     List();
     List(List&);

     // Modifier methods.
    void setPos(int);
     void inc();
     void first();
     void last();
     void prev();
     void next();
     void insertBefore(ElementType);
     void insertAfter(ElementType);
     void replace(ElementType);
     void erase();
     void clear();

     // Accessor methods.
     int getCapacity() const;
     int size() const;
     int getPos() const;
     ElementType getElement() const;
    // empty returns true or false if list is empty or not.
     bool empty() const;
     bool operator==(List&) const;
     bool operator!=(List&) const;
     void operator+(List&);
     void operator=(List&);
     void operator+=(List&);
     friend ostream& operator<<(std::ostream&, List&);
};

// Constructors.
List::List()
{
     clear();
}

List::List(List& l)
{
     clear();
     int temp_size = l.size();
     l.first();
     for (int i = 0; i < temp_size; i++)
     {
          insertAfter(l.getElement());
          next();
          l.next();
     }
     sz = temp_size;
}

// Modifier methods.
// setPos(int) places current position in a certain position in the list
void List::setPos(int i)
{
    int temp_size = size();
     if (sz == 0)
          i = 0;
     else if (temp_size > 0 && i > temp_size - 1)
          i = temp_size - 1;
     else if (i < 0)
          i = 0;
     else
          pos = i;
}

void List::inc()
{
     sz++;
}

void List::first()
{
     if (sz > 0)
          pos = 0;
}

void List::last()
{
     if (sz > 0)
          pos = sz - 1;
}

// prev places current position at the previous element in the list
void List::prev()
{
     if (sz > 1)
          if (pos == 0)
               return;
     pos--;
}

// next places current position at the next element in the list
void List::next()
{
     if (pos < (sz - 1))
          pos++;
}

// insertBefore  inserts a new element before the current position
void List::insertBefore(ElementType val)
{
     if (sz == 0)
     {
          pos = 0;
          ary[pos] = val;
          sz++;
     }
     else if (sz < CAPACITY)
     {
          for (int i = sz; i > pos - 1; i--)
               ary[i] = ary[i - 1];
          ary[pos] = val;
          sz++;
     }
}

// insertAfter inserts a new element after the current position
void List::insertAfter(ElementType val)
{
     if (sz == 0)
     {
          pos = 0;
          ary[pos] = val;
          sz++;
     }
     else if (sz < CAPACITY)
     {
          for (int i = sz; i > pos; i--)
               ary[i] = ary[i - 1];
          ary[pos + 1] = val;
          sz++;
          pos++;
     }
}

// replace(int) replace the current element with a new value
void List::replace(ElementType val)
{
     ary[pos] = val;
}

// erase deletes the current element
void List::erase()
{
     ary[pos] = 0;
     for (int i = pos; i < sz - 1; i++)
          ary[i] = ary[i + 1];
     sz--;
}

// clear makes the list an empty list
void List::clear()
{
     ElementType* ptr = ary;
     for (int i = 0; i < CAPACITY; i++)
     {
          *ptr = 0;
          ptr++;
     }
     sz = 0;
     pos = -1;
}

// Accessor methods.
int List::getCapacity() const
{
     return CAPACITY;
}

// size returns the size of the list (number of elements in list)
// !!! Wrong name
int List::size() const
{
     return sz;
}

// getPos returns current position or where you are in the list
int List::getPos() const
{
     return pos;
}

// get Element returns the one element that current position is pointing to
ElementType List::getElement() const
{
     return ary[pos];
}

// empty returns true or false if list is empty or not.
bool List::empty() const
{
     if (sz == 0)
          return true;
     return false;
}

// Overloaded operator ==
bool List::operator==(List& l) const
{
     l.first();
     for (int i = 0; i < sz; i++)
     {
          if (ary[i] != l.getElement())
               return false;
          l.next();
     }
     return true;
}

bool List::operator!=(List& l) const
{
     l.first();
     for (int i = 0; i < sz; i++)
     {
          if (ary[i] != l.getElement())
               return true;
          l.next();
     }
     return false;
}

// Overloaded operator +
void List::operator+(List& l)
{
     int temp_size = l.size();
     last();
     l.first();
     for (int i = sz; sz < temp_size; i++)
     {
          insertAfter(l.getElement());
          next();
          l.next();
     }
}

// Overloaded operator =
void List::operator=(List& l)
{
     int temp_size = l.size();
     l.first();
     for (int i = 0; i < temp_size; i++)
     {
          insertAfter(l.getElement());
          next();
          l.next();
     }
     sz = temp_size;
}

void List::operator+=(List& l)
{
     List temp;
     temp + l;
     operator=(temp);
}

// Overloaded operator <<
std::ostream& operator<<(std::ostream& out, List& l)
{
     int temp_size = l.size();
     for (int i = 0; i < temp_size; i++)
     {
          l.setPos(i);
	  // Invariants:  The elements in the list should be left justified.
	  // !!!
          out << l.getElement() << ' ';
     }
     return out;
}

// front makes current position at the beginning of the list
// end makes current position at the end of a list.
// reverse   reverse elements in a list
// swap  swaps all the elements of one list with the another list. 



Couple of comments:

- copy constructor would normally have its ref argument const. Also your constructor code is very inefficient using insertAfter() and next(). Also, as first(), next() etc alters pos, the argument can't be const. Consider:

1
2
3
4
5
6
7
8
List::List(const List& l)
{
	sz = l.sz;
	pos = l.pos;	// May want 0??

	for (int i = 0; i < CAPACITY; ++i)
		ary[i] = l.ary[i];
}


- setPos() can be simplified. Consider:

1
2
3
4
5
6
7
void List::setPos(int i)
{
	if (sz == 0 || i < 0)
		pos = 0;
	else
		pos = (sz > 0 && i > sz - 1) ? sz - 1 : i;
}


- inc() should test for end of list

1
2
3
4
5
void List::inc()
{
	if (sz < CAPACITY)
		++sz;
}


- the insertBefore()/insertAfter() functions can simplified:

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
// insertBefore  inserts a new element before the current position
void List::insertBefore(ElementType val)
{
	if (sz == 0)
		pos = 0;
	else
		if (sz < CAPACITY)
			for (int i = sz; i > pos - 1; i--)
				ary[i] = ary[i - 1];

	ary[pos] = val;
	++sz;
}

// insertAfter inserts a new element after the current position
void List::insertAfter(ElementType val)
{
	if (sz == 0)
		pos = 0;
	else
		if (sz < CAPACITY)
		{
			for (int i = sz; i > pos; i--)
				ary[i] = ary[i - 1];

			pos++;
		}

	ary[pos] = val;
	++sz;
}


- why does clear() use pointers?

1
2
3
4
5
6
7
8
void List::clear()
{
	for (int i = 0; i < CAPACITY; ++i)
		ary[i] = 0;

	sz = 0;
	pos = -1;
}


- empty - as per previous comment can be simplified:

1
2
3
4
5
// empty returns true or false if list is empty or not.
bool List::empty() const
{
	return sz == 0;
}


- operator==(), if the sizes of the two lists aren't equal then the result is false without any further tests. The same for operator!=

1
2
3
4
5
6
7
8
9
10
11
12
13
bool List::operator==(List& l) const
{
	if (l.sz != sz)
		return false;

	l.first();

	for (int i = 0; i < sz; ++i, l.next())
		if (ary[i] != l.getElement())
			return false;

	return true;
}


-also for operator!=, this is the inverse of operator==, so is simply:

1
2
3
4
bool List::operator!=(List& l) const
{
	return !operator==(l);
}


- I'm not sure that the operator+() is correct. In the original post the declaration is
List operator + (List& ListA, List& ListB);

which is what I would be expecting (except the args should be const). operator() wouldn't expect to change the contents of the current list, but create a new list with the specified argument list appended to the current list and returned.

- operator=() this is the same as copy constructor - re same comment

Last edited on
Regarding seeplus's suggestions:

In List::List(), there's no need to copy the entire array. You can just copy the occupied elements:
1
2
	for (int i = 0; i < l.sz; ++i)
		ary[i] = l.ary[i];

You could also consider just using your assignment operator. That way there's just one copy of code that copies Lists:
List::List(const List& l) { *this = l; }

By the way, you should never use l as a variable name because it's too easy to confuse with 1.

In setPos(), I'd leave pos unchanged if i is invalid:
1
2
3
4
5
6
void List::setPos(int i)
{
	if (i >= 0 && i < sz) {
		pos = i;
	}
}


inc() isn't needed or used so I'd get rid of it.

InsertBefore() and insertAfter() shouldn't do anything if the list is full
1
2
3
4
5
6
7
8
9
10
11
12
// insertBefore  inserts a new element before the current position.
void List::insertBefore(ElementType val)
{
	if (sz >= CAPACITY) return;
	if (sz > 0) {
		for (int i = sz; i > pos; --i) {
			arr[i] = arr[i-1];
		}
	}
	arr[pos] = val;
	++sz;
}


clear() doesn't need to change arr.

Operator== shouldn't change l (by calling first() and next()). It's best to make the parameter const:
1
2
3
4
5
6
7
8
9
10
11
bool List::operator==(List& rhs) const
{
	if (rhs.sz != sz) return false;

	for (int i = 0; i < sz; ++i) {
		if (ary[i] != rhs.arr[i]) {
			return false;
		}
	}
	return true;
}
 
bool List::operator==(const List& rhs) const


1
2
3
4
bool List::operator!=(const List& l) const
{
	return !operator==(l);
}


The same with operator<<

1
2
3
4
5
6
7
std::ostream& operator<<(std::ostream& out, const List& l)
{
     for (int i = 0; i < sz; ++i)
          out << ary[i] << ' ';

     return out;
}


also operator+=, operator= which should also return a ref to the list

 
List& List::operator=(const List& l)


 
List& List::operator+=(const List& l)


There's also an argument that first(), last(), next() and prev() should be const as they don't actually change the stored data in the list. In that case pos should be mutable.
Last edited on
Alright! Thank you all for the assistance with the methods and functions. I've incorporated the shorter more efficient ways. Now when I compile I get this debug error.


"Debug Error!
Run-Time Check Failure #2 - Stack around the variable 'sq' was corrupted.
Press Retry to debug the application."

Its a window that populates on screen after the output to the console generates.
Last edited on
Please post your latest code.
I've done something to the code, I have to revert to a previous copy. I did copy paste your alteration dhayden, that's when I first encountered the error, when i compiled the code of yours with the main posted above.
> You could also consider just using your assignment operator. That way there's
> just one copy of code that copies Lists:
> List::List(const List& l) { *this = l; }
eek, your member variables are not initialised, ¿are you sure that operator= will not fuck it up?
quite error prone, perhaps there is no problem in this case, but consider dynamic memory allocation

checkout copy-swap idiom
Now is a good time to brush up on your debugging skills. Every programmer needs to acquire good debugging skills. The earlier they're learnt the better IMO. Use the debugger, step through the code to se what's happening and that it is as expected. When you find the area of code with the issue, then if you can't work out what's happening then post the code. Even very experienced programmers make heavy use of the debugger to find issues in code. Apart from 'trivial' programs, most will have at least one problem when coded which the debugger is used to find and fix.
ne555 wrote:
eek, your member variables are not initialised, [...] perhaps there is no problem in this case
Indeed, there is no problem in this case, but I agree that I should have mentioned that this is an uncommon case and I should have mentioned it.
I did copy paste your alteration dhayden, that's when I first encountered the error,
I should have mentioned that your code still has bugs. My goal was just to get it to a state where it would compile. Sorry for not being clear.

In any case, we can't help you unless your post your current code.
Topic archived. No new replies allowed.