Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to read IP addresses from a file and produce a list of distinct

ID: 3838465 • 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
#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 addressitem’s count
string getAddress()const;

int getCount()const;

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

private:

string address;

int count;

};

i found the answer for this question. but its not the way my proffessor needed. they needed using an impletation file. we need to use headers and pointers. we cant use the edit method. this is the impletation

/** @ 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.

Explanation / Answer

I have this solutions, hope this helps:

#include <iostream>
#include <map> // we will use map to count the words.
#include <fstream> // this Will be used to read from a file.
#include <string> // This is for storing map's key value.
#include <arpa/inet.h> // this is used in our isValidIpAddress() to check is the text is valid ip address
using namespace std;


//Here we used this to print the map later.
template <class KTy, class Ty>
void PrintMap(map<KTy, Ty> map)
{
typedef std::map<KTy, Ty>::iterator iterator;
for (iterator p = map.begin(); p != map.end(); p++)
cout << p->first << ": " << p->second << endl;
}
bool isValidIpAddress(char *ipAddress)
{
struct sockaddr_in sa;
int result = inet_pton(AF_INET, ipAddress, &(sa.sin_addr));
return result != 0;
}

int main(void)
{
static const char* fileName = "C:\YourFile.txt";

// Will store the word and count.
map<string, unsigned int> wordsCount;


{
// Begin reading from file:
ifstream fileStream(fileName);

// Check if we've opened the file (as we should have).
if (fileStream.is_open())
while (fileStream.good())
{
// Store the next word/IP in the file in a local variable.
string word;
fileStream >> word;
               if(isValidIpAddress(word) // checking if the current text is ip or not.
               {
//Look if it's already there.
if (wordsCount.find(word) == wordsCount.end()) // Then we've encountered the word for a first time.
wordsCount[word] = 1; // Initialize it to 1.
else // Then we've already seen it before..
wordsCount[word]++; // Just increment it.
               }
}
else // We couldn't open the file. Report the error in the error stream.
{
cerr << "Couldn't open the file." << endl;
return EXIT_FAILURE;
}

// Print the words map.
PrintMap(wordsCount);
}

return EXIT_SUCCESS;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote