Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#C++ Hi, I don't really know how to solve this question. If you are free please help me with it. Thank you! Heres the contents

#C++

Hi,

I don't really know how to solve this question. If you are free please help me with it. Thank you!

Heres the contents of a file called example.cpp:

// example.cpp #include "LinkedList.h" #include  #include  using namespace std; int main() { cout << "Please enter some words (ctrl-d to stop): "; LinkedList lst; int count = 0; string s; while (cin >> s) { count++; lst.add(remove_non_letters(s)); } // while cout << " " << count << " total words read in "; cout << lst.size() << " unique words read in "; lst.print(); } 

If you try to compile this youll get an error saying the header file LinkedList.h doesnt exist. Your task for this assignment is to implement LinkedList.h so that this example program and any other program that uses it works correctly.

Here are the parts of the program you need to implement:

LinkedList is a class that implements a linked list. Implement this as singly-linked list, doubly-linked list, or circular list (you decide what is best).

LinkedList has a default constructor, i.e. a constructor that takes no inputs, that creates a new empty LinkedList object.

LinkedList has a destructor that deletes all the nodes on the list. This is necessary to make sure LinkedList has no memory leaks.

Assuming lst is an object of type LinkedList, then it has at least the following methods:

lst.size() returns the number of nodes currently on the linked list.

lst.add(s) adds the string s to the front of the linked list if s is not already somewhere on the list. If s is already somewhere on the list, then it does nothing.

Thus, lst.add(s) ensures that only one copy of each string occurs in lst.

lst.print() prints to cout a numbered list of all the strings in lst. The strings should be printed in the order they occur on the list. Note that because new strings are always added to the front of the list, the order of the strings should always be the same.

Please print your list neatly in the format shown in the example runs below.

The function remove_non_letters(s) takes a string s as input and returns a new string that is the same as s, except any non-letters have been removed. For example, remove_non_letters("M-55a3cfg.1") returns the string "Macfg", and remove_non_letters("555-5390).returns "".

Note that you can add any helper code you need to implement these functions. Please dont use code from any other sources except the C++ standard library, and implement the linked list using basic C++ (e.g. dont just use the STL linked list!).

Example Runs

Suppose the file test1.txt contains this text:

this is a test, this is only a test! 

Then you can build and run example.cpp like this:

$ make example g++ -std=c++14 -Wall -Wextra -Werror -Wfatal-errors -Wno-sign-compare -Wnon-virtual-dtor -g example.cpp -o example $ ./example  

The command ./example

You also need to check that your program has no memory leaks, so run it with valgrind like this:

valgrind ./example  

valgrind reports that there are no memory leaks. It says that 18,944 bytes are still in use at exit, but thats okay because the program is done and all memory is being returned to the operating system anyways.

Be sure to test your program on more than just the data in test1.txt, and also on simple code besides the code in example.cpp. Do enough testing to be certain your program works correctly in all cases.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Administration The Complete Guide To Dba Practices And Procedures

Authors: Craig S. Mullins

2nd Edition

0321822943, 978-0321822949

More Books

Students also viewed these Databases questions

Question

=+j Explain the essential nature of repatriation.

Answered: 1 week ago