Typically the idea behind this type of program is to make sure you understand how objects are created, how object pointers work, and a basic list. Your object is already created, you will need to make some minor modifications to it, but you're on the right track. In your constructors, ctors, make sure that you're setting next = NULL. This will make it seem like your object is the last one regardless of where it's at. Now, there is an easy way to change this without needing a "getter" but more on that later.
Since your class is fine for the moment, we'll move on to the next step, getting the list designed. As Felicia said, and as I believe you already implemented, you need a Course* head. This will always point at your very first Course object (the great thing about lists is that our variables don't need names). Also, you need another Course* which you declared as c. This will allow you to move through your array as needed.
Now, let's work on how this should work. head and c should both equal NULL, they don't have objects yet. You can now create a new object and return the pointer to c. You will have to work out how exactly you want to create objects, but it's typically best to put it in some style of loop asking the user for inputs. After c has a value, which it will when you
c = new Course(...);
, you should check to see if head has a value as well. If head == NULL, it's empty and should equal c. After all, if head is NULL, c contains your first object.
Once you understand the concept of the pointers, you need to move on to linking your list together. This is where the Course* next comes in. This should point to the next Course in the list. You have two options, you can make this public, which is easiest, but also possibly the worst thing you can do, or you can create a new member function called something like AddAfterMe that accepts a Course pointer. This will effectively allow you to link your list together.
Now, on to how to complete everything. c should, more or less, be your temp pointer that holds the new courses. You should create a new Course pointer and call it something like prev, p, whatever. This will allow you to know what the most recently added element to your list was since the list itself doesn't know what was prior. Some basic pseudo code:
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
|
Course *head = NULL, *c = head, *prev = head; // Initialize all three as NULL
// Define local variables for use
courseName, courseTerm, courseUnit, courseGrade, again;
do {
What is the course name?
input courseName;
What is the course term?
input courseTerm;
What is the course Unit?
input courseUnit;
What is the course Grade?
input courseGrade;
c = new Course with above values
if (!head) // head is still NULL
head = c;
if (prev) // prev is pointing at something
prev->AddAfterMe(c);
prev = c; // Work is done, store the next item.
Would you like to enter another course?
input again;
} while (again == yes);
|
At this point, head points to your first item, and c and prev both point to your last. You can do a bunch of things with all of your pointers, but I strongly urge against changing head
EVER unless it's NULL, otherwise you will lose elements in your list.
I hope all of this has helped you understand what you're doing it, why I wrote the code I did, and what all you can do with this.
Also, you may want to create a member function called NextCourse() it should return the pointer. You can test against this pointer returned and if it equals NULL, you've gone to the end of the list.
Hint: You'll probably be using code like this quite often in this program:
1 2
|
if (p->NextCourse())
p = p->NextCourse();
|
Good luck and sorry about the long winded reply.