In: Computer Science
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
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
==========================================================================