Question: Write a program to read IP addresses from a file and produce a list of distinct addresses and a count of how many times each

Write a program to read IP addresses from a file and produce a list of distinct addresses and a count of how many times each address appeared in the file. The addresses and counts are stored in a linked list. Your linked list should hold objects of type AddressItem. You should use the templated version of the linked list class we reviewed in lecture. Hint: You can add a replace function to the Linked List class to overwrite a node. :

#include #include #include "ListPointer.h"

using namespacestd;

class AddressItem{

public:

AddressItem ( ) {count = 0;}

friend istream & operator>>(istream & in, AddressItem & a); //inputs one ip address into a

friend ostream & operator<<(ostream & out, const AddressItem & a); void Tally( ); // add one to addressitems count string getAddress()const;

int getCount()const;

bool operator==(constAddressItem & addr2)const; //compare address values only

private:

string address;

int count;

};

/** @ templated file ListPointer.h - includes implementation*/

#ifndef LISTPOINTER_H

#define LISTPOINTER_H

#include // for NULL

#include //assert

#include

#include

using namespace std;

/** @class List

* ADT list - Pointer-based implementation. */

template

class List

{

public:

// Constructors and destructor:

/** Default constructor. */

List();

/** Copy constructor.

* @param aList The list to copy. */

// List(const List& aList);

/** Destructor. */

~List();

// List operations:

bool isEmpty() const;

int getLength() const;

void insert(int index, const T& newItem);

void remove(int index);

void retrieve(int index, T& dataItem) const;

void display ();

//void reverse ();

//void exchange(int index);

private:

/** A node on the list. */

struct ListNode

{

/** A data item on the list. */

T item;

/** Pointer to next node. */

ListNode *next;

}; // end ListNode

/** Number of items in list. */

int size;

/** Pointer to linked list of items. */

ListNode *head;

/** Locates a specified node in a linked list.

* @pre index is the number of the desired node.

* @post None.

* @param index The index of the node to locate.

* @return A pointer to the index-th node. If index < 1

* or index > the number of nodes in the list,

* returns NULL. */

void find(int index, ListNode *&) const;

}; // end List

// definitions of methods follow:

template

List::List(){

head = NULL;

size = 0;

}

/*

template

List::List(const List& aList)

{

size = aList.size;

//cout << "size " << size << endl;

if (aList.head == NULL)

head = NULL; // original list is empty

else

{ // copy first node

head = new ListNode;

head->item = aList.head->item;

// copy rest of list

ListNode *newPtr = head; // new list pointer

// newPtr points to last node in new list

// origPtr points to nodes in original list

for (ListNode *origPtr = aList.head->next; origPtr != NULL; origPtr = origPtr->next)

{ newPtr->next = new ListNode;

newPtr = newPtr->next;

newPtr->item = origPtr->item;

} // end for

newPtr->next = NULL;

} // end if

} // end copy constructor

*/

template

List::~List()

{

while (!isEmpty())

remove(1);

} // end destructor

template

bool List::isEmpty() const

{

return size == 0;

} // end isEmpty

template

int List::getLength() const

{

return size;

} // end getLength

template

void List::find(int index, ListNode *& p ) const

{

if ( (index < 1) || (index > getLength()) )

p = NULL;

else // count from the beginning of the list.

{ ListNode *cur = head;

for (int skip = 1; skip < index; skip++)

cur = cur->next;

//return cur;

p=cur;

} // end if

} // end find

template

void List::retrieve(int index, T& dataItem) const

{

assert ( (index >= 1) && (index <= getLength()) );

// get pointer to node, then data in node

ListNode *cur;

find(index, cur);

dataItem = cur->item;

} // end retrieve

template

void List::insert(int index, const T& newItem)

{

int newLength = getLength() + 1;

assert ( (index >= 1) && (index <= getLength()+1) );

ListNode *newPtr = new ListNode;

size = newLength;

newPtr->item = newItem;

// attach new node to list

if (index == 1)

{ // insert new node at beginning of list

newPtr->next = head;

head = newPtr;

}

else

{

ListNode *prev;

find(index-1, prev);

// insert new node after node to which prev points

newPtr->next = prev->next;

prev->next = newPtr;

} // end if

} // end insert

template

void List::remove(int index)

{

ListNode *cur;

assert ( (index >= 1) && (index <= getLength()) );

--size;

if (index == 1)

{ // delete the first node from the list

cur = head; // save pointer to node

head = head->next;

}

else

{

ListNode *prev;

find(index-1, prev);

// delete the node after the node to which prev points

cur = prev->next; // save pointer to node

prev->next = cur->next;

} // end if

// return node to system

cur->next = NULL;

delete cur;

cur = NULL;

} // end remove

template

void List::display ()

{

ListNode *cur;

if (size != 0)

for (cur = head; cur != NULL; cur = cur ->next)

cout << cur->item << endl;

}

#endif;

// End of header file.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!