Here's my problem, I'm not sure how to write this whole linked list:
The linked list should be relatively object-oriented, using two classes at least: one for list itself and one for each element of the list. Suggested names are List and ListElement.
Remember that each ListElement should have a pointer to the next ListElement(probably called next).
Remember also that you must find some way to end the list: either by having the last ListElement's next pointer point to null or itself.
The list should hold integers and support three basic operations: adding integers, removing integers, and finding integers.
Please remember that linked lists require the use of new, and that every use of new should have a corresponding use of delete.
Assuming your class is called list, you may use the following sample main:
Code:
int main()
{
List list;
cout << list.Count(9) << endl; // should print 0
list.Add(7);
list.Add(12);
if(list.Contains(12))
cout << "found 12" << endl; // this should print
else
cout << "error" << endl; // shouldn't print
if(!list.Contains(15))
cout << "Did not find 15" << endl; // this should print
else
cout << "error" << endl;
for(int x = 0; x < 10; x++)
list.Add(x);
cout << list.Count(7) << endl; // this should pring 2, since
// there's (2) 7's in the list
list.RemoveAll(7);
cout << list.Count(7) << endl; // should print 0
return 0;
}
A few questions, would I need to use some sort of array or vector to store the nodes.
how would I write the node, such as:
Code:
struct Node
{
int x;
Node *next;
}
class List
{
public:
List() : head(NULL){}
void Add(int y)
{
if(head == NULL)
{
head = new Node;
head->x = y;
head->next = (what would I put here- NULL, new Node?)
}
else
arrow->Add(x)
private:
Node *head;
List *Arrow;
};
and for the RemoveAll function, would I write:
[code]RemoveAll(int y)
{
if(Arrow != NULL)
delete Arrow;
}[code]
And for list.Count(int), would I need to use an array or vector to count the number of times that a number was inserted int list.Add, or is there some other better way to do this?
thanks