Functions not working?

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
/*My instructor says:
My driver does not test all the class functions.
My program test data should not be hard-coded in my program. 
My test data should be input from the keyboard or from an input file.*/
//------------------------------------------------------------------------
//driver.cpp (for some tests)
// ListDemo.cpp : Defines the entry point for the console application.
//driver.cpp
//#include "stdafx.h"
#include "List.h"
#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
   List Test;
   Test.insert("AAA", 0);
   Test.insert("BBB", 1);
   Test.insert("CCC", 2);
   cout<<"The first list is: "<<endl;
   Test.display(cout);
   cout << Test.getSize() << endl;
   List Test_Second;
   Test_Second = Test;
   cout<<"The Second list is: "<<endl;
   Test_Second.display(cout);
   cout << Test_Second.getSize() << endl;
   Test_Second.insert("DDD", 3);
   List Test_Third(Test_Second);
   cout<<"The Third list is: "<<endl;
   Test_Third.display(cout);
   cout<<"\n" << Test_Third.getSize() << endl;
   Test_Third.insert("EEE",4);
   cout<<"The Third list after inserting another element is: "<<endl;
   Test_Third.display(cout);
   cout << Test_Third.getSize() << endl;
   cout<<"Deleted an element whose index value is 2: "<<endl;
   Test_Third.erase(2);
   cout<<endl;
   cout<<"Therefore, the elements present in the Third list are: "<<endl;
   Test_Third.display(cout);
   cout<<"\n"<<"The value AAA is found at: "<<Test_Third.find("AAA");
   cout<<endl;
   cout <<"The size of the Third list is :" <<Test_Third.getSize() << endl;
system ("pause");
   return 0;
}
//------------------------------------------------------------------------------
//List.cpp
//#include "stdafx.h"
#include "List.h"
//Construct empty List
List::List()
{
   first = NULL;
   mySize = 0;
}
//Construct copy of source List
List::List( const List &source )
{
   first = NULL;
   mySize = 0;         //check the code, the value of mySize is
   //changed
   int index = 0;
   NodePointer current = source.first;
   while(current != NULL)
   {
       insert(current->data, index++);
       current = current->next;
   }
}
List::~List( )
{
   for(int i = 0; i <= mySize; i++)
       erase(i);
}
const List & List::operator=( const List &rightSide )
{
   int index = 0;
   NodePointer current = rightSide.first;
   while(current != NULL)
   {
       insert(current->data, index++);
       current = current->next;
   }
   return *this;
}
int List::getSize( ) const
{
   return mySize;
}
bool List::empty( ) const
{
   if(mySize == -1)
       return true;
   return false;
}
void List::insert( ElementType dataVal, int index )
{
   if(index == 0)
   {
       NodePointer NewNode = new Node;
       NewNode->data = dataVal;
       NewNode->next = first;
       first = NewNode;
       mySize++;
   }
   else if(index > 0 && index <= (mySize + 1))
   {
       NodePointer current = first;
       for(int i = 0; i < index - 1; i++)
           current = current->next;
       NodePointer NewNode = new Node;
       NewNode->data = dataVal;
       NewNode->next = current->next;
       current->next = NewNode;
       mySize++;
   }
}
void List::erase( int index )
{
   if(first != NULL)
   {
       if(index == 0)
       {
           NodePointer temp = first;
           first = temp->next;
           delete temp;
           mySize--;
       }
       else if(index > 0 && index <= (mySize + 1))
       {
           NodePointer current = first;
           for(int i = 0; i < index -1; i++)
               current = current->next;
             NodePointer temp = current->next;
            if(temp!=NULL)   //check the condition that has been modified
                     current->next = temp->next;
           delete temp;
           mySize--;
       }
   }
}
void List::display( ostream &out ) const
{
   NodePointer current = first;
   while(current != NULL)
   {
       out << current->data << endl;
       current = current->next;
   }
}
int List::find( ElementType value) const
{   NodePointer current = first;
   int index = 0;
   while(current != NULL)
   {
       if(current->data == value)
           return index;
       else
       {
             index++;
           current = current->next;         
       }
   }
   return -1;
}
//--------------------------------------------------------------------------------------------
// List.h
#ifndef LIST
#define LIST
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
typedef string ElementType;
class List
{
   private:
       class Node
       {
           public:
               ElementType data;
               Node * next;
               Node( ): data( ElementType( ) ), next( NULL )
               { }
               Node( ElementType initData ): data( initData ), next( NULL )
               { }
       }; // end of Node class
   typedef Node * NodePointer;
   public:
       List( );
       /* Construct a List object
       Precondition: none.
       Postcondition: An empty List object has been constructed.
       */
       List( const List &source );
       /* Construct a copy of a List object.
       Precondition: None.
       Postcondition: A copy of source has been constructed.
       */
       ~List( );
       /* Destroys a List object.
       Precondition: None.
       Postcondition: Any memory allocated to the List object has been freed.
       */
       const List & operator=( const List &rightSide );
       /* Assign a copy of a List object to the current object.
       Precondition: none
       Postcondition: A copy of rightside has been assigned to this
       object. A constant reference to this list is returned.
       */
       int getSize( ) const;
       /* Returns the size of the list (number of items in the list)
       Precondition: none
       Postcondition: The return value is the number of items in the list.
       */
       bool empty( ) const;
       /* Check if this List is empty
       Precondition: none
       Postcondition: The return value is true if this List object is empty;
       otherwise the return value is false.
       */
       void insert( ElementType dataVal, int index );
       /* Insert a value into this List at a given index
       Precondition: The index is valid (0 <= index <= the list size).
       The first position is index 0, the second position is index 1, etc.
       Postcondition: dataval has been inserted into the list at the position
       determined by index (provided there is room and index is a legal
       position).
       */
       void erase( int index );
       /* Remove the value from this List at a given index.
       Precondition: The list is not empty and index is valid
       (0 <= index < the list size).
       Postcondition: the element at position index has been
       removed (provided index is a legal position).
       */
       void display( ostream &out ) const;
       /* Display the contents of this List
       Precondition: ostream out is open
       Postcondition: the items in this List have been output to stream out
       */
       int find( ElementType value) const;
       /* Find the first occurrence of a value in this List
       Preconditions: None
       Postconditions: The return value is the index of the first List item
       that matches value. The first list item has index 0, the second has
       index 1, etc. The return value is -1 if value is not found in the list.
       */
   private:
       NodePointer first;
      int mySize;
}; // end of List class
#endif 
Last edited on
Please try to remove the double spacing from your future code posts.

As for the driver, write a separate program that reads commands from cin. The commands should correspond 1:1 with your class. In other words, you'd have commands for:
getsize
empty
insert <value> <index>
erase<index>
display
find <value>

Then you can create a text file that is full of input for the test driver. For example:
empty
insert tom
insert dick
insert harry
display
find tom
find dave
insert sue
find sue
erase 12
display
erase 2
display
empty


This may all seem pointless but it's actually one of the most useful and important things you're going to learn. In the real world you'd create a test driver like this, run it and save the results. If you have to change the code later on, you can rerun the test and compare the output to the known good output. This is likely to expose any bugs.
Topic archived. No new replies allowed.