-1

Hello everyone can someone help me to delete the shortest word from the linked list without array?

I'am a first course student and learning pointers with linked list etc. This is my last task to do and I'am stuck.

This code finds the shortest word of the list and I need to delete that word.

I don't know how to delete that specific word.

My problem is that "del" function deletes wrong word:

void del (Node *shorter)
    {
        Node* temp;
        temp = shorter->next;
        shorter->next = temp->next;
        cout<<"Deleted: "<<temp->data<<endl;
        delete temp;
    }

This is my full code:

#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

std::vector<std::string> split(const std::string& source, const std::string& delimiters = " ") {
    std::size_t prev = 0;
    std::size_t currentPos = 0;
    std::vector<std::string> results;

    while ((currentPos = source.find_first_of(delimiters, prev)) != std::string::npos) {
        if (currentPos > prev) {
            results.push_back(source.substr(prev, currentPos - prev));
        }
        prev = currentPos + 1;
    }
    if (prev < source.length()) {
        results.push_back(source.substr(prev));
    }
    return results;
}

struct Node {
    std::string data;
    Node* next;
};
struct Node* head = NULL;

Node* createList() {
    string text;
    cout << "Write text: ";
    getline(cin, text);

    Node *head = new Node();
    head->next = NULL;
    Node *current = head;

    string delimiters = " ,.-':;?()+*/%$#!\"@^&";
    auto results = split(text, delimiters);

    bool isFirst = true;
    for (const auto& word : results) {
        if (isFirst) {
            current->data = word;
            isFirst = false;
        } else {
            Node *newNode = new Node();
            newNode->data = word;
            current->next = newNode;
            current = newNode;
        }
    }

    return head;
}

void del (Node *shorter)
{
    Node* temp;
    temp = shorter->next;
    shorter->next = temp->next;
    cout<<"Deleted: "<<temp->data<<endl;
    delete temp;
}


void findShortestWord(Node* head) {
    Node *current = head;
    Node *shorter = head;

    while (current != NULL) {
        if (current->data.size() < shorter->data.size()) {
            shorter->data = current->data;
        } else if (current->data.size() > shorter->data.size()) {
            current = current->next;
        } else if (current->data.size() == shorter->data.size()) {
            current = current->next;
        }
    }
    cout << "_____________________________________________________________" << endl;
    cout << "Shortest word: " << shorter->data << "                                |" <<endl;
    cout << "_____________________________________________________________|" << endl;
    del(shorter);
}

void print(Node* head)
{
    if (head == NULL and cout << endl)
        return;
    cout<<"\nThe word you entered: ";
    cout << head->data << ' ';
    print(head->next);
}

int main() {

     Node *head = createList();
     print(head);
     findShortestWord(head);
     print(head);

    return 0;
}
Gronila
  • 11
  • 1
  • 1
    I'm not sure there's a single thing in here that relies on a C++ feature; it's all legal C. Why are you writing C++ and using none of the niceties of C++? – ShadowRanger May 30 '22 at 16:54
  • @ShadowRanger I don't know. I'am a beginner and trying to do last university task and I found this example but it is with "int" elements. – Gronila May 30 '22 at 16:59
  • 3
    This is a bad starting point if you're not *required* to rebuild linked lists from scratch. In C++ code, you could just use `std::list` or (even more closely related to this code, but less flexible) `std::forward_list` and drop the majority of the custom code required here. Making this even vaguely compatible with C++ would require replacing a *ton* of the code (e.g. you can't safely `malloc`/`free` structs containing non-POD data like `std::string`, so simply swapping `int` to `std::string` won't work), to the point where you'd be better off starting from scratch. – ShadowRanger May 30 '22 at 17:04
  • Mandatory comment in cases like this: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Paul Sanders May 30 '22 at 17:16
  • 1
    Do you **need** optimized code? Is your program missing events? Are your customers complaining about your program being very slow? First rule of optimization: don't, work on robustness and quality. Second rule of optimization: benchmark and profile. Only optimize the areas that need to be optimized. – Thomas Matthews May 30 '22 at 17:24
  • If you want to optimize, replace `puts` with `fwrite`. The `puts` slows down because it has to compare each character to find the end of the string (text). The `fwrite` writes the amount of characters, no need for comparisons. Some `fwrite` can be optimized to use hardware assistance. – Thomas Matthews May 30 '22 at 17:28
  • "trying to do last university task" What course is it? What language is being used in the course? What are the restrictions placed on language features that you are allowed to use? All of this may influence the answer. – n. 1.8e9-where's-my-share m. May 30 '22 at 17:37
  • Translated to C++ it becomes something like this: `std::List list; ... list.erase(std::min_element(list.begin(), list.end(), [](const std::string &lhs, const std::string &rhs) { return lhs.length() < rhs.length();}));` – Goswin von Brederlow May 30 '22 at 17:47
  • @n.1.8e9-where's-my-sharem. First course. We use C / C ++ language. We are taught how to use pointers. As I know the only limitation is not to use an array – Gronila May 31 '22 at 05:54
  • There is no such thing as "C/C++ language". – n. 1.8e9-where's-my-share m. May 31 '22 at 08:08
  • @ShadowRanger I updated my code and now it can found the shortest word in the list. But now my problem that it deletes the wrong word and not the shortest. – Gronila May 31 '22 at 08:40
  • _the only limitation is not to use an array_ Use [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) then! After all, rules are made to be broken, these dinosaur courses are a menace. – Paul Sanders May 31 '22 at 09:08

0 Answers0