this is my current code
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
#include "IPlist.h"
IPlist::IPlist()
// default constructor
// creates a new empty list
{
list=NULL;
}
IPlist::~IPlist()
// destructor - reallocates all dynamic memory of list
{
delete [] list;
}
IPlist::IPlist (const IPlist & other)
// copy constructor to create a deep copy of other IPlist
// object
{
if (other.list==NULL)
{
list = NULL;
}
else
{
// copy the first node
node *current = other.list;
list = new node;
list->IPaddress = current->IPaddress;
list->link = NULL;
current = current->link;
node *pt = list;
// copy the rest of list
while (current != NULL)
{
pt->link = new node;
pt = pt->link;
pt->IPaddress = current->IPaddress;
pt->link = NULL;
current = current->link;
}
}
}
bool IPlist::empty() const
// precondition : none
// postcondition : returns true if list is empty
// and otherwise false
{
return list==NULL;
}
void IPlist::insertInOrder(string address)
// precondition : list is ordered and address is not in list
// postcondition : list is ordered and contains address
{
while(list!=NULL)
{
node* current=new node;
//temp->IPaddress=address;
//node *smaller=list;
if (list->IPaddress < address )
{
current->IPaddress =address;
current->link =list->link;
list->link = current;
list=current->link;
}
else
{
list=list->link;
}
}
}
bool IPlist::isPresent(string address) const
// precondition : none
// postcondition : returns true if a node containing address
// is present in list and otherwise false
{
node *current=list;
while(current!=NULL)
{
if (current->IPaddress==address)
{
return true;
}
else
{
current=current->link;
}
}
return false;
}
void IPlist::updateCount(string address)
// precondition : address is in list
// postcondition : the count of node with address has been incremented
{
int count=0;
node *current=list;
while (current!=NULL)
{
if (current->IPaddress==address)
{
current->count=count+1;
}
else
{
current=current->link;
}
}
}
node *IPlist::find (string address) const
// precondition : none
// postcondition : returns a pointer to node containing address or
// NULL if not present
{
node *current=list;
while (current!=NULL)
{
if(current->IPaddress == address)
{//checks 1st node
return current;
}
else
{//increments to the next node
current=current->link;
}
}
return NULL;
}
void IPlist::display() const
// precondition : none
// postcondition : the list has been displayed
// one record per line, tab-separated
// with heading IPaddress Count
{
node *current=list;
while(current!=NULL)
{
cout<<"IPaddress Count "<<endl;
cout<<current<<" "<<current->count<<endl;
current=current->link;
}
}
and my main...
#include <iostream>
#include <cstdlib>
using namespace std;
#include "IPlist.h"
void testCopyConstructor(IPlist x);
int main()
{
IPlist ip;
if (ip.empty())
{
cout << "empty works" << endl;
}
string data[]={"a","b","c","a","b","c","a","d"};
// some strings to insert into IPlist for testing
// they don't really need to be IP addresses
for (int i = 0; i < 8; i++)
{
if (!ip.isPresent(data[i]))
ip.insertInOrder(data[i]);
else
ip.updateCount(data[i]);
}
ip.display();
cout << endl;
node *current = ip.find("d");
if (current != NULL)
cout << "found "<< current->IPaddress << endl;
system("pause");
return EXIT_SUCCESS;
}
void testCopyConstructor(IPlist x)
{
// copy constructor will be automatically called when pass by value
// if we have written a correct copy constructor to make a deep copy
// then changing x will not change the corresponding argument
// (in this case ip in the main program)
x.insertInOrder("w");
x.insertInOrder("x");
}
im getting no output when i compile,its just giving me an empty list what error am i making?