Answer to Question #289030 in C++ for Olivia

Question #289030

For this assignment, students are required to implement a word counting program in C++.


The program should accept the name of a text file as command line arguments (arguments to main) and then perform the following tasks:


Implement a special word counter linked list. Each node of the list will contain following:


next pointer : pointer to next element in the list


previous pointer : pointer to previous elements in the list


character: the data value (char)


words : a pointer to a linked list containing words starting from the character (the data value).



Read the strings from the file (separated by spaces). Identify the starting character (alphabet) of the word (string).


Store the word in the words linked list of the node corresponding to the starting alphabet.


At the end, you should end up with a linked list, with alphabets (a-z) for example, with each node also containing a pointer to another linked list that has all the words starting from the alphabet.

1
Expert's answer
2022-01-20T01:45:57-0500
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
template <class T>
class List {

private:
    class Node {
    public:
        Node* next;
        T value;
        Node(T data = T(), Node* ptr = nullptr){
            this->next = ptr;
            this->value = data;
        }
    };
public:
    Node* head;
    int size = 0;
    List();
    ~List();
    
    void display();
    void popfront();
    void popback();
    void pushfront(T value);
    void pushback(T value);
    void insert_at(T value, int index);
    void remove_at(int index);
    void clear();
    T& operator[] (int c);
    int Getsize() {
        return size;
    }
};

template <class T>
List<T>::List(){
    head = nullptr;
}

template <class T>
void List<T>::popfront() {
    --size;
    Node* temp = head;
    head = head -> next;
    delete temp;
}

template <class T>
void List<T>::popback() {
    remove_at(size-1);
}

template <class T>
void List<T>::pushfront(T value) {
    head = new Node(value, head);
    ++size;
}

template <class T>
void List<T>::insert_at(T value, int index) {
    
    if(index == 0) {
        pushfront(value);
        return;
    }
    Node* previous = head;
    for(int i = 0; i!=index-1; ++i) {
        previous = previous->next;
    }
    Node* newnode = new Node(value, previous->next);
    previous->next = newnode;
    size++;
}

template <class T>
void List<T>::remove_at(int index) {
    if(index == 0) {
        popfront();
        return;
    }
    Node* current = head;
    Node* prevCurrent = head;
    int i = 0;
       while(i<index) {
           prevCurrent = current;
           current = current->next;
           ++i;
       }
    Node* nextCurrent = current->next;
    delete current;
    prevCurrent->next = nextCurrent;
    --size;
}

template <class T>
void List<T>::pushback(T value) {
    Node* newNode = new Node(value);
    if(!head) {
        head = newNode;
    }
    else {
        Node* current = head;
        while(current->next) {
            current = current->next;
        }
        current->next = newNode;
    }
    ++size;
}

template <class T>
void List<T>::clear() {
    while(size) {
        this->popfront();
    }
}

template <class T>
T& List<T>::operator[](int c){
    Node* current = head;
    int count = 0;
    while (count < c) {
        current = current->next;
        ++count;
    }
    return current->value;
}

template <class T>
void List<T>::display() {
    Node* current = head;
    while(current) {
        cout<<current<<"\t"<<current->value<<"\t"<<current->next<<endl;
        current = current->next;
    }
}

template <class T>
List<T>::~List(){
    this->clear();
}
int main()
{
    List<std::string> l;
    std::ifstream infile("thefile.txt");
    std::string line;
    while (std::getline(infile, line))
    {
        l.pushback(line);

        
    }
    l.display();
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog