Yes - This is homework related. I'm not looking for a direct answer, just a pointer (ha ha - pun intended).
This pertains to linked lists.
We have a class such as:
CODE
class item
{ public:
item();
item(string,int);
~item();
void insert(item *p);
void list();
void forwards();
void backwards();
void put(ostream &out);
private:
string name;
int count;
item *next;
};
Assume at this point that this list populates correctly.
So this seemingly simple problem goes:
QUOTE
Create a loop at the end of the main() function, before the items are deleted, that will traverse the linked list and display the contents of each item.
So, INHCNN says:
CODE
p = head;
while(p != NULL)
{
p->put(cout);
p = p->next;
}
...but of course,
next is private, so I can't roll like that. I considered making a public getNext function, but that seems like it defeats the purpose, or whatever goal, is being sought after here.
Help? Just a little?
Here's the driver... If you need Item.h or Item.cpp, holler.
CODE
/**********************************************
* Lab 01
* Your name goes here
**********************************************/
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
#include "Item.h"
/**********************************************
* main
**********************************************/
void main()
{ int count;
string name;
fstream infile;
item *head,*p;
head = NULL;
// Open file
cout << "Enter file name: ";
cin >> name;
infile.open(name.data(),ios::in);
// Loop through file
while(!infile.eof())
{ infile >> name >> count;
// Process valid input
if(infile.good())
{
p = new item(name,count);
if(!head)
head = p;
else
head->insert(p);
};
};
// Close file
infile.close();
// Loop through contents
p = head;
while(p != NULL)
{
p->put(cout);
p = p->next;
}
cout << "Finished!" << endl;
if(head) delete head;
system("pause");
}
And as an aside (hopefully to prove that I'm not completely lost here), the other part of this was to create the forward and backward member functions, which I have wrote (in the implementation file):
CODE
void item::forwards()
{
this->put(cout);
if(this->next != NULL)
this->next->forwards();
}
void item::backwards()
{
if(this->next != NULL)
this->next->backwards();
this->put(cout);
}