Question

In: Computer Science

For example, I have an input text file that reads: "Hello, how are you?" "You look...

For example, I have an input text file that reads:

"Hello, how are you?"

"You look good today."

"Today is a great day."

How can I write a function that inputs each line into a node in a linked list? Thank you. (Note: the input text files could have a different amount of lines, and some lines could be blank.)

This is the linked list code:

#include <iostream>

#include <cstdlib>

#include <fstream>

class Node

{

public:

Node* next;

int data;

};

using namespace std;

class LinkedList

{

public:

int length;

Node* head;

  

LinkedList();

~LinkedList();

void add(int data);

void print();

};

LinkedList::LinkedList(){

this->length = 0;

this->head = NULL;

}

LinkedList::~LinkedList(){

std::cout << "LIST DELETED";

}

void LinkedList::add(int data){

Node* node = new Node();

node->data = data;

node->next = this->head;

this->head = node;

this->length++;

}

void LinkedList::print(){

Node* head = this->head;

int i = 1;

while(head){

std::cout << i << ": " << head->data << std::endl;

head = head->next;

i++;

}

}

int main(int argc, char const *argv[])

{

LinkedList* list = new LinkedList();

list->print();

delete list;

return 0;

}

Solutions

Expert Solution

#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;
class Node {
        public:
        Node* next;
        string data;
};


class LinkedList {
        public:
        int length;
        Node* head;

        LinkedList();
        ~LinkedList();
        void add(string data);
        void print();
};

LinkedList::LinkedList(){
        this->length = 0;
        this->head = NULL;
}

LinkedList::~LinkedList(){
std::cout << "LIST DELETED";
}

void LinkedList::add(string data){
        Node* node = new Node();
        node->data = data;
        node->next = this->head;
        this->head = node;
        this->length++;
}

void LinkedList::print(){
        Node* head = this->head;
        int i = 1;
        while(head){
                std::cout << i << ": " << head->data << std::endl;
                head = head->next;
                i++;
        }
}

int main(int argc, char const *argv[]) {

        ifstream f("input.txt");
        LinkedList* list = new LinkedList();

        string line;

        while(getline(f, line)) {
                if(line != "") {
                        list->add(line);
                }
        }

        list->print();

        delete list;

        return 0;
}

input.txt:

Hello, how are you?

You look good today.

Today is a great day.

**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces another text file in Which blank lines are removed, multiple blanks are replaced with a single blank, and no lines are longer than some given length (let say 80). Put as many words as possible on the same line (as close as possible to 80 characters). You will have to break some lines of the given file, but do not break any words or...
I have a Python code that reads the text file, creates word list then calculates word...
I have a Python code that reads the text file, creates word list then calculates word frequency of each word. Please see below: #Open file f = open('example.txt', 'r') #list created with all words data=f.read().lower() list1=data.split() #empty dictionary d={} # Adding all elements of the list to a dictionary and assigning it's value as zero for i in set(list1):     d[i]=0 # checking and counting the values for i in list1:     for j in d.keys():        if i==j:           d[i]=d[i]+1 #Return all non-overlapping...
Part I The input to the program will be a text file containing the information for...
Part I The input to the program will be a text file containing the information for a tolerance table. An example follows using the values from the first lecture on tolerance analysis. These values will be stored in a text file. The data is comma delimited, which means that each data field is separated by a comma. If the first word is ‘PART’ the following values are the nominal size, +/- impact, tolerance, and fixed/variable. If the first word is...
Write a function in C++ that reads the line separates it with comma. Input: hello how...
Write a function in C++ that reads the line separates it with comma. Input: hello how are you. hello world hello_world I am there for you! Output: hello, how, are and you. hello and world hello_world I, am, there, for and you! just add a comma and (and) before the last word. CODE IN C++ ONLY.
You are given a text file containing a short text. Write a program that 1. Reads...
You are given a text file containing a short text. Write a program that 1. Reads a given text file : shortText.txt 2. Display the text as it is 3. Prints the number of lines 4. Prints the occurences of each letter that appears in the text. [uppercase and lowercase letter is treated the same]. 5. Prints the total number of special characters appear in the text. 6. Thedisplayofstep3,4and5aboveshouldbesaveinanoutputfile:occurencesText.txt write it in C++ programing Language
I just finished this program where the program reads a text file of full names ,...
I just finished this program where the program reads a text file of full names , first and last name, and a zip code. It reads the items then stores the first name and last name and zipcodes as an objects and prints the items.   However, when i use my text file i get this error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1 " I notice this issue only happens when the names are...
This is a python file Reads information from a text file into a list of sublists....
This is a python file Reads information from a text file into a list of sublists. Be sure to ask the user to enter the file name and end the program if the file doesn’t exist. Text file format will be as shown, where each item is separated by a comma and a space: ID, firstName, lastName, birthDate, hireDate, salary Store the information into a list of sublists called empRoster. EmpRoster will be a list of sublists, where each sublist...
Hello. I have a statement that I´m having difficulties to answer. Do you have any input...
Hello. I have a statement that I´m having difficulties to answer. Do you have any input on this? "The only objective companies need to follow to achieve stakeholder goals is shareholder wealth maximisation." Thank you
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
For c language. I want to read a text file called input.txt for example, the file...
For c language. I want to read a text file called input.txt for example, the file has the form. 4 hello goodbye hihi goodnight where the first number indicates the n number of words while other words are separated by newlines. I want to store these words into a 2D array so I can further work on these. and there are fewer words in the word file than specified by the number in the first line of the file, then...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT