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

CSCE 2100, Spring 2017 Programming Assignment 02 Full Points: 100 Submission Due

ID: 3792590 • Letter: C

Question

CSCE 2100, Spring 2017 Programming Assignment 02 Full Points: 100 Submission Due 02/22/2017 (Wednesday), 11:59 pm Create two classes word and linked list in C++ 1. Class word' contains the following properties a) A member string called 'word to hold a string or word b) A member integer called frequency to count the frequency of the word c) A pointer to point to the next element of the same class Add appropriate member-functions such as, a constructor b) function (s to print the word and it's frequency c) function to update/increase the frequency of the word 2. Class linked list contains a pointer of type word Add appropriate functions to a) initialize the linked list b) grow the linked list when a new word appears c) print the linked list Then write a separate file program 02. cpp" where you will read an integer 'n' and then using a repetitive structure read n strings (n words Every time you read a word, store it in your linked list If the word already exists increment the frequency of the word otherwise, create a new word at the end of the linked list. After reading n words from a file/console, print the word and its frequency. Finally, print a message like the following The input set contains m unique words out of total n words.

Explanation / Answer

Please find the code attached.

Word.h

/*

* Word.h

*

* Created on: 18-Feb-2017

* Author: yourname

*/

#include<string>

#include<iostream>

#ifndef WORD_H_

#define WORD_H_

class Word {

   friend class LinkedList;

private:

   std::string word;

   int frequency;

   Word *next;

public:

   void print();

   void increaseFrequency();

   Word();

   Word(std::string word);

   virtual ~Word();

};

#endif /* WORD_H_ */

-------------------------------------------------------------------------------------------------------------------------------------------------------------

LinkedList.h

/*
* LinkedList.h
*
* Created on: 18-Feb-2017
* Author: yourname
*/

#include "Word.h"
#ifndef LINKEDLIST_H_
#define LINKEDLIST_H_

class LinkedList {
private:
   Word *head;
public:
   LinkedList();
   virtual ~LinkedList();
   void insertWord(std::string word);
   void print();
};

#endif /* LINKEDLIST_H_ */

-------------------------------------------------------------------------------------------------------------------------------------------------------------

Word.cpp

/*
* Word.cpp
*
* Created on: 18-Feb-2017
* Author: yourname
*/

#include "Word.h"
Word::Word() {
   word = "";
   frequency = 0;
   next = NULL;
}

Word::Word(std::string word) {
   this->word = word;
   frequency = 1;
   next = NULL;
}

void Word::increaseFrequency() {
   frequency += 1;
}

void Word::print() {
   std::cout << word << ": " << frequency << std::endl;
}

Word::~Word() {
}

-------------------------------------------------------------------------------------------------------------------------------------------------------------

LinkedList.cpp

/*

* LinkedList.cpp

*

* Created on: 18-Feb-2017

* Author: yourname

*/

#include "LinkedList.h"

LinkedList::LinkedList() {

   head = NULL;

}

LinkedList::~LinkedList() {

}

void LinkedList::insertWord(std::string word) {

   if (head == NULL) {

       head = new Word(word);

       return;

   }

   Word *first = head;

   bool inserted = false;

   while (first->next != NULL) {

       if (first->word == word) {

           first->increaseFrequency();

           inserted = true;

           break;

       }

       first = first->next;

   }

   if (!inserted && first->word == word) {

       first->increaseFrequency();

       inserted = true;

   }

   if (!inserted) {

       first->next = new Word(word);

   }

}

void LinkedList::print() {

   Word *first = head;

   while (first != NULL) {

       first->print();

       first = first->next;

   }

}

-------------------------------------------------------------------------------------------------------------------------------------------------------------

program_02.cpp

#include "LinkedList.h"

using namespace std;

int main() {

   int n;

   LinkedList l;

   std::string word;

   cin >> n;

   for (int i = 0; i < n; ++i) {

       cin >> word;

       l.insertWord(word);

   }

   l.print();

   return 0;

}