Question

In: Computer Science

C++ Code You will write a program to process the lines in a text file using...

C++ Code

You will write a program to process the lines in a text file using a linked list and shared pointers.

You will create a class “Node” with the following private data attributes:

• Line – line from a file (string)

• Next (shared pointer to a Node)

Put your class definition in a header file and the implementation of the methods in a .cpp file. The header file will be included in your project. If you follow the style they use in the book of having a "#include" for the implementation file at the bottom of the header file, then you do not include your implementation file in the project.

You will have the following public methods:

• Accessors and mutators for each attribute

• Constructor that initializes the attributes to nulls (empty string)

You will make the linked list a class and make the processes class methods.

Your program will open a text file , read each line and store it in a Node. Make sure the file opened correctly before you start processing it. You do not know how many lines are in the file so your program should read until it gets to the end of the file. It will display the contents of the list (the lines that were read from the file) with all the duplicates.

Then your program will edit the list, removing any duplicate entries and display the list again once the duplicates have been removed.

Your linked list class should include the following methods, possibly more:

1. addNode – adds a node to the list given the value of the line

2. removeDuplicates – go through the entire list and remove any duplicate lines

3. displayList – displays each entry in the list

. You will write all of the code for processing the linked list - do not use any predefined objects in C++. You do not need to offer a user interface – simply display the list before and after removing duplicate lines.

To test the program, use the Stuff1.txt below:

Stuff1.txt

The sky is blue.

Clovis Bagwell

Shake rattle and roll.

The quick brown fox jumped over the lazy dog.

One red bean stuck in the bottom of a tin bowl.

Rats live on no evil star.

The sky is blue.

Mark Knopfler

The sky is blue.

Archibald Beechcroft

Oliver Crangle

Workers of the world unite.

Oliver Crangle

A squid eating dough in a polyethylene bag is fast and bulbous.

Somerset Frisby

Mark Knopfler

Oliver Crangle

Data structures is really hard.

It's raining and boring outside.

It's raining and boring outside.

It's raining and boring outside.

Somerset Frisby

Henry Bemis

Mark Knopfler

Solutions

Expert Solution

Below is complete code. Let me know if you have any problem or doubts. Thank you.

==========================================================================

node.h

====================

#include <string>
using namespace std;

#ifndef NODE_H
#define NODE_H

class Node {
private:
   string line; // represent line from file
   Node* next; // pointer to nnext node
public:
   Node(); // default constructor
   // accessors
   string getLine();
   Node* getNext();
   // mutators
   void setLine(string line);
   void setNext(Node* next);
};

#endif // !NODE_H

====================

node.cpp

====================

#include "node.h"

Node::Node() {
   // initialize data to nulls
   line = "";
   next = NULL;
}

string Node::getLine() {
   return line;
}

Node* Node::getNext() {
   return next;
}

void Node::setLine(string line) {
   this->line = line;
}

void Node::setNext(Node* next) {
   this->next = next;
}

====================

linkedlist.h

====================

#include "node.h"

#ifndef LINKED_LIST_H
#define LINKED_LIST_H

class LinkedList {
private:
   Node* head; // head of linked list
public:
   LinkedList(); // constructor
   ~LinkedList(); // destructor
   // adds a node to the list given the value of the line
   void addNode(string line);
   // go through the entire listand remove any duplicate lines
   void removeDuplicates();
   // displays each entry in the list
   void displayList();
};

#endif // !LINKED_LIST_H

====================

linkedlist.cpp

====================

#include "linkedlist.h"
#include <iostream>

LinkedList::LinkedList() {
   // initialize head
   head = NULL;
}

LinkedList::~LinkedList() {
   // delete all nodes to free memory
   while (head != NULL) {
       Node* temp = head;
       head = head->getNext();
       delete temp;
   }
}

void LinkedList::addNode(string line) {
   // creat a new node to insert in list
   Node* node = new Node();
   // set line to new node
   node->setLine(line);
   // check for first node
   // if head is null then new node is the first node to insert
   if (head == NULL) {
       head = node;
   }
   else {
       // create a pointer to iterate over list
       Node* itr = head;
       // move to last node
       while (itr->getNext() != NULL) {
           itr = itr->getNext();
       }
       // add node at end of list
       itr->setNext(node);
   }
}

void LinkedList::removeDuplicates() {
   // create a pointers to iterate over list
   Node* itr1 = head;
   Node* itr2;
   // move iterator 1 till end of list to process the list
   while (itr1 != NULL) {
       // use iterater 2 to compare node's data in list
       // initialize iterator 2 to locatioin of iterator 1
       // since a node it self is not duplicate we will compare next node
       // after iterator 2
       itr2 = itr1;
       while (itr2 != NULL && itr2->getNext() != NULL) {
           // compare data in nodes to remove duplicate
           // use compare function to compare string data
           if (itr1->getLine().compare(itr2->getNext()->getLine()) == 0) {
               // remove next node from the node pointed by iterator 2
               // assign temparorary pointer to node to free its memory
               Node* temp = itr2->getNext();
               Node* nextNode = temp->getNext();
               // remove link of the node to be deleted
               itr2->setNext(nextNode);
               // delete the node with duplicate content
               delete temp;
           }
           else {
               // move pointer to next node
               itr2 = itr2->getNext();
           }
       }
       itr1 = itr1->getNext();
   }
}

void LinkedList::displayList() {
   // create a pointer to iterate over list
   Node* itr = head;
   // print each node's data
   while (itr != NULL) {
       cout << itr->getLine() << endl;
       // move to next node
       itr = itr->getNext();
   }
}

====================

main.cpp

====================

#include "linkedlist.h"
#include <iostream>
#include <fstream>

int main() {
   // create linkedlist to represent list data
   LinkedList list;
   string filename = "Stuff1.txt";
   // read from file Stuff1.txt
   ifstream file(filename);
   // check if file can be opened
   if (file.is_open()) {
       // read file line by line
       string line;
       while (!file.eof()) {
           getline(file, line);
           // add line to list
           list.addNode(line);
       }
       // print file content
       cout << "File " << filename << " contains data:" << endl;
       list.displayList();
       // remove duplicates from list
       list.removeDuplicates();
       // print final result
       cout << "\nList after removing duplicates:" << endl;
       list.displayList();
   }
   else {
       // print error message
       cout << "Could not open file: " << filename << endl;
   }

   return 0;
}

====================

Stuff1.txt

====================

The sky is blue.
Clovis Bagwell
Shake rattle and roll.
The quick brown fox jumped over the lazy dog.
One red bean stuck in the bottom of a tin bowl.
Rats live on no evil star.
The sky is blue.
Mark Knopfler
The sky is blue.
Archibald Beechcroft
Oliver Crangle
Workers of the world unite.
Oliver Crangle
A squid eating dough in a polyethylene bag is fast and bulbous.
Somerset Frisby
Mark Knopfler
Oliver Crangle
Data structures is really hard.
It's raining and boring outside.
It's raining and boring outside.
It's raining and boring outside.
Somerset Frisby
Henry Bemis
Mark Knopfler

==========================================================================


Related Solutions

Using C++, write a code that this program always stores text file output into a text...
Using C++, write a code that this program always stores text file output into a text file named "clean.txt". -The program should read one character at a time from "someNumbers.txt", and do the following. -If it is a letter, print that letter to the screen, AND also store it in the text file. All letters should be converted to lowercase beforehand. -If it is a number, print that number to screen, but do NOT store it in the text file....
Write a program that allows the user to navigate the lines of text in a file....
Write a program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the program...
Write a program that allows the user to navigate the lines of text in a file....
Write a program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the program...
Language: c++ using visual basic Write a program to open a text file that you created,...
Language: c++ using visual basic Write a program to open a text file that you created, read the file into arrays, sort the data by price (low to high), by box number (low to high), search for a price of a specific box number and create a reorder report. The reorder report should alert purchasing to order boxes whose inventory falls below 100. Sort the reorder report from high to low. Inventory data to input. Box number Number boxes in...
Language: c++ using visual basic Write a program to open a text file that you created,...
Language: c++ using visual basic Write a program to open a text file that you created, read the file into arrays, sort the data by price (low to high), by box number (low to high), search for a price of a specific box number and create a reorder report. The reorder report should alert purchasing to order boxes whose inventory falls below 100. Sort the reorder report from high to low. Inventory data to input. Box number Number boxes in...
Write a C++ program using produces Huffman code for a string of text entered by the...
Write a C++ program using produces Huffman code for a string of text entered by the user. Must accept all ASCII characters.
Java Code using Queue Write a program that opens a text file and reads its contents...
Java Code using Queue Write a program that opens a text file and reads its contents into a queue of characters, it should read character by character (including space/line change) and enqueue characters into a queue one by one. Dequeue characters, change their cases (upper case to lower case, lower case to upper case) and save them into a new text file (all the chars are in the same order as the original file, but with different upper/lower case) use...
Java Code using Stack Write a program that opens a text file and reads its contents...
Java Code using Stack Write a program that opens a text file and reads its contents into a stack of characters, it should read character by character (including space/line change) and push into stack one by one. The program should then pop the characters from the stack and save them in a second text file. The order of the characters saved in the second file should be the reverse of their order in the first file. Ex input file: Good...
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. In paragraph 1 Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. In pragraph 2 Replace "We" with v"i" This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT