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
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
#include
#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
head = NULL;
size = 0;
}
/*
template
List
{
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
{
while (!isEmpty())
remove(1);
} // end destructor
template
bool List
{
return size == 0;
} // end isEmpty
template
int List
{
return size;
} // end getLength
template
void List
{
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
{
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
{
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
{
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
{
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
Get step-by-step solutions from verified subject matter experts
