I have a project to create a pointer based linked list. I am having a problem with the contains() indexOf() and lastIndexOf() methods. I am not able to compare iter->item!=o. This is my ArrayList.cpp. I will follow it with ArrayList.h and Country.h (the ArrayList uses a typedef of the Country class for generics), and main.cpp. I assume the problem is there is no operator != in the Country.h file. (However, our instructor told us there would be no problems with missing operators in the header files.) Is there anything Else I can do?. I know some of the functions have yet to be written, and any feedback on how to do them would be great. Also, let me know how my code looks in general, I am fairly new to c++. Also, in main.cpp, the #include gives me an error "Cannot open header file". Any help with any of these issues would be great! thanks!
#include <string>
using namespace std;
#include "Country.h"
typedef Country Object;
class ArrayList {
private:
class Node; // forward declaration since used before defined
Node* head;
Node* tail;
int numberOfItems;
/* Private class representing a node.
*/
class Node {
public:
Object item;
Node* next;
Node(Object item, Node* next) { this->item = item; this->next = next; }
};
void ArrayList::copyList(const ArrayList& copyThis)
{
Node* copyIter = copyThis.head;
Node* tail;
numberOfItems = copyThis.numberOfItems;
tail = head = CreateNode(copyIter->item, NULL);
copyIter = copyIter->next;
while ( copyIter != NULL )
{
tail = tail->next = CreateNode(copyIter->item, NULL);
copyIter = copyIter->next;
}
}
void ArrayList::releaseList()
{
Node* del = head;
while (head != NULL)
{
head = head->next;
del->next = NULL;
delete del;
del = head;
}
}
Node* CreateNode(Object item, Node* next)
{
return new Node(Object(item), next);
}
public:
ArrayList::ArrayList()
{
head = NULL;
tail = NULL;
numberOfItems = 0;
}
ArrayList::ArrayList(const ArrayList& copyThis)
{
copyList(copyThis);
}
ArrayList::~ArrayList()
{
releaseList();
}
ArrayList ArrayList::operator =(const ArrayList& assignThis)
{
if (this != &assignThis)
{
releaseList();
copyList(assignThis);
}
return (*this);
}
void ArrayList::add(Object o)
{
tail = tail->next = CreateNode( o , NULL);
numberOfItems++;
}
void ArrayList::add(Object o, int index)
{
Node* before;
if (index == 0)
{
before = NULL;
head = CreateNode(o, head);
}
else
{
before = head;
for(int i = 1; i <index - 1; i++)
{
before = before->next;
}
before->next = CreateNode(o, before->next);
}
numberOfItems++;
}
void ArrayList::clear()
{
releaseList();
}
bool ArrayList::contains(Object o) const
{
Node* iter = head;
while(iter!=NULL && iter->item != o)
{
iter = iter->next;
}
if(iter == NULL)
{
return false
}
else return true;
}
Object ArrayList::objectAt(int index) const
{
Node* iter;
if (index==0)
{
iter = NULL;
}
else
{
iter = head;
for (int i = 1; i < index; i++)
{
iter = iter->next;
}
}
return iter->item;
}
int ArrayList::indexOf(Object o) const
{
}
int ArrayList::lastIndexOf(Object o) const
{
}
bool ArrayList::isEmpty() const
{
if (head->next = NULL)
return true;
else return false;
}
void ArrayList::remove(Object o)
{
}
void ArrayList::remove(int index)
{
Node* before = head;
Node* del;
if (index == 1)
{
del = head;
head = head->next;
}
else
{
for (int count = 1 ; count < index ; index++)
{
before = before->next;
}
del = before->next;
before->next = del ->next;
}
del->next = NULL;
delete del;
del = NULL;
before = NULL;
numberOfItems--;
}
int ArrayList::size() const
{
return numberOfItems;
}
void ArrayList::set(Object o, int index)
{
Node* before = head;
Node* del;
if (index == 0)
{
del = head;
head = CreateNode(o , head->next);
}
else
{
for (int count = 1 ; count < index ; index++)
{
before = before->next;
}
del = before->next;
before->next = CreateNode(o, before->next);
}
del->next = NULL;
delete del;
del = NULL;
before = NULL;
}
string ArrayList::toString()
{
}
/* File: ArrayList.h
*
* Author: Socratis Tornaritis
* Modified: 9/12/2012
*/
#ifndef ARRAY_LIST_H
#define ARRAY_LIST_H
#include <string>
using namespace std;
#include "Country.h"
typedef Country Object;
class ArrayList {
private:
class Node; // forward declaration since used before defined
Node* head;
Node* tail;
int numberOfItems;
/* Private class representing a node.
*/
class Node {
public:
Object item;
Node* next;
Node(Object item, Node* next)
{
this->item = item; this->next = next;
}
};
// Makes a deep copy of the parameter list.
void copyList(const ArrayList& copyThis);
// Releases all the memory allocated to the list; resets the object to its
// default values.
void releaseList();
// Allocates a new Node and populates it with the parameter values. Returns
// this new memory.
Node* CreateNode(Object item, Node* next)
{
return new Node(Object(item), next);
}
public:
// Initializes object to default values (NULL, NULL, 0).
ArrayList();
// Performs a deep copy of the parameter list. Calls copyList() to do the actual work.
ArrayList(const ArrayList& copyThis);
// Destroys the object. Calls releaseList() to do the actual work.
~ArrayList();
// Assigns a deep copy of the parameter list. Calls releaseList() and
// copyList() to do the actual work.
ArrayList operator =(const ArrayList& assignThis);
// Appends the object o to the end of the list. This resizes the list by 1.
void add(Object o);
// Adds the object o and the specified index. This resizes the list by 1.
void add(Object o, int index);
// Removes all the items from the list. Resets object to its default state.
void clear();
// Returns true if the list contains the object o.
bool contains(Object o) const;
// Returns the object at the specified index. Assumes index is in range.
Object objectAt(int index) const;
// Returns the index of the first matching object o. Assumes the object is in the list.
int indexOf(Object o) const;
// Returns the index of the last matching object. Assumes the object is in the list.
int lastIndexOf(Object o) const;
// Returns true if list is empty.
bool isEmpty() const;
// Removes the object o from the list. List is resized by 1. Assumes object is present.
void remove(Object o);
// Removes the object o at the specified index. List is resized by 1. Assumes index is in range.
void remove(int index);
// Returns the number of elements in the list.
int size() const;
// Sets the element at the specified index.
void set(Object o, int index);
// Returns a string containing all the items comma separated and surrounded
// by curly ({) braces, i.e. {item1, item2, ...}
string toString();
};
#endif
/* File: Country.h
*
* Author: Socratis Tornaritis
* Modified: 9/12/2012
*/
#ifndef COUNTRY_H
#define COUNTRY_H
#include <string>
using namespace std;
class Country {
private:
string name;
public:
Country() { name = "na"; }
Country(string name) { this->name = name; }
string getName() { return name; }
void setName(string name) { this->name = name; }
string toString() { return "{ name = " + name + " }"; }
};
#endif
#include "ArrayList.h"
#include <iostream>
using namespace std;
int main() {
ArrayList northAmerica;
Country canada("Canada");
Country usa("USA");
Country mexico("Mexico");
northAmerica.add( canada );
northAmerica.add( mexico );
northAmerica.add( usa, 1 );
cout << "North America "
<< "(" << northAmerica.size() << " head): "
<< endl
<< "\t";
// << northAmerica.toString();
//cout << endl
// << endl
// << "Does North America contain Panama? "
// << (northAmerica.contains( Country("Panama") )? "Yes" : "No")
// << endl
// << "Does North America contain Mexico? "
// << (northAmerica.contains( mexico )? "Yes" : "No")
// << endl;
//
//cout << endl
// << "Object at index 0: "
// << northAmerica.objectAt(0).toString()
// << endl;
//cout << endl
// << "Index of first occurence of 'USA': "
// << northAmerica.indexOf( usa )
// << endl
// << "Index of last occurence of 'USA': "
// << northAmerica.lastIndexOf( usa )
// << endl;
//cout << endl
// << "Removing Mexico from list:";
//northAmerica.remove( mexico );
//cout << endl
// << "North America:"
// << "(" << northAmerica.size() << " items): "
// << endl
// << "\t"
// << northAmerica.toString();
//cout << endl
// << endl
// << "Removing item 0 from list:";
//northAmerica.remove( (int) 0);
//cout << endl
// << "North America "
// << "(" << northAmerica.size() << " items): "
// << endl
// << "\t"
// << northAmerica.toString();
//
//cout << endl
// << endl
// << "Change 'USA' to 'United States':";
//northAmerica.set( Country("United States"), 0 );
//cout << endl
// << "North America "
// << "(" << northAmerica.size() << " items): "
// << endl
// << "\t"
// << northAmerica.toString();
cout << endl
<< endl
<< "Clear the list:";
northAmerica.clear();
cout << endl
<< "North America "
<< "(" << northAmerica.size() << " items): "
<< endl
<< "\t";
//<< northAmerica.toString();
cout << endl << endl;
return 0;
} // end main()