Write a program to read IP addresses from a file and produce a list of distinct
ID: 3837399 • Letter: W
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 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 <iostream>
#include <string>
#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 addressitem’s count
string getAddress()const;
int getCount()const;
bool operator==(constAddressItem & addr2)const; //compare address values only
private:
string address;
int count;
};
please it should be matching this implement
/** @ templated file ListPointer.h - includes implementation*/
#ifndef LISTPOINTER_H
#define LISTPOINTER_H
#include <cstddef> // for NULL
#include <cassert> //assert
#include <iostream>
#include <string>
using namespace std;
/** @class List
* ADT list - Pointer-based implementation. */
template<class T>
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<class T>
List<T>::List(){
head = NULL;
size = 0;
}
/*
template<class T>
List<T>::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<class T>
List<T>::~List()
{
while (!isEmpty())
remove(1);
} // end destructor
template<class T>
bool List<T>::isEmpty() const
{
return size == 0;
} // end isEmpty
template<class T>
int List<T>::getLength() const
{
return size;
} // end getLength
template<class T>
void List<T>::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<class T>
void List<T>::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<class T>
void List<T>::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<class T>
void List<T>::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<class T>
void List<T>::display ()
{
ListNode *cur;
if (size != 0)
for (cur = head; cur != NULL; cur = cur ->next)
cout << cur->item << endl;
}
#endif;
// End of header file.
#include <iostream>
//#include <string>// don't have to include string because ListPointer does
#include "ListPointer.h"
using namespace std;
void main( )
{
List<string> l;
l.insert(1,"word1");
string s;
l.retrieve(1,s);
cout << s << endl;
l.insert(1,"word2");
l.insert(2,"word3");
l.display( );
}
/*
word1
word2
word3
word1
Press any key to continue . . .
*/
we use c++
Explanation / Answer
Executable code:
#include <cassert>
#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <list>
#include <algorithm>
using namespace std;
class MyAddressCounter
{
public:
void myRead(istream & fIn)
{
fIn >> ipAddress;
counter = 0;
}
void myPrint(ostream & fOut) const
{
fOut << setw(15) << left << ipAddress
<< " occurs " << counter << " times ";
}
void myTally()
{
counter++;
}
friend bool operator==(const MyAddressCounter& address1,const MyAddressCounter& address2);
private:
string ipAddress;
int counter;
};
inline bool operator==(const MyAddressCounter& address1,const MyAddressCounter& address2)
{
return address1.ipAddress == address2.ipAddress;
}
typedef list<MyAddressCounter> IPList;
int main()
{
string fName;
IPList listOfIPAddr;
ifstream inputStream;
cout << "Enter name of file having IP addresses: ";
cin >> fName;
inputStream.open(fName.data());
assert(inputStream.is_open());
MyAddressCounter data;
for (;;)
{
data.myRead(inputStream);
if (inputStream.eof())
break;
IPList::iterator iter = find(listOfIPAddr.begin(), listOfIPAddr.end(), data);
if (iter != listOfIPAddr.end())
(*iter).myTally();
else
listOfIPAddr.push_back(data);
}
cout <<endl<<"Addresses and Counts: "<<endl<<endl;
for (IPList::iterator iter = listOfIPAddr.begin();
iter != listOfIPAddr.end(); iter++)
(*iter).myPrint(cout);
cin.get();
cin.get();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.