如何从没有数组的链表中删除最短的单词?C / c++



如何从没有数组的链表中删除最短的单词?我是第一门课程的学生和学习指针与链表等。这是我要做的最后一项任务,我被卡住了。

这段代码找到列表中最短的单词,我需要删除这个单词。我不知道如何删除那个特定的单词。

我的问题是del函数删除了错误的单词:

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

这是我的完整代码:

#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;
}

由于这是家庭作业,我不会给出一个完整的答案,但是这是一个链表的例子。"你;是最短的单词。如果我们画出这个节点,它看起来像这样:

(head)           (shorter prev)   (shorter)        (shorter next)
┌──────────┐     ┌──────────┐     ┌──────────┐     ┌──────────┐
│  hello   │────>│  world   │────>│    it    │────>│  other   │
└──────────┘     └──────────┘     └──────────┘     └──────────┘
Unlink "it"
┌──────────┐     ┌──────────┐     ┌──────────┐     ┌──────────┐
│  hello   │────>│  world   │───┐ │    it    │ ┌──>│  other   │
└──────────┘     └──────────┘   ↓ └──────────┘ ↑   └──────────┘
└──────────────┘

New list
┌──────────┐     ┌──────────┐     ┌──────────┐
│  hello   │────>│  world   │────>│  other   │
└──────────┘     └──────────┘     └──────────┘

现在你可以更短的delete

在不知道前一个节点的地址的情况下,我们不能取消一个节点的链接。(如果前一个节点是nullptr,我们将删除head。)因此,在findShortestWord()中,当遍历列表以找到最短的单词时,请跟踪前一个节点并在del()中使用它。

另一种方法是修改结构体,使其包含prev成员(双链表)。

struct Node {
std::string data;
Node* prev;
Node* next;
};

注意:您可以在没有任何temp变量的情况下取消节点链接。

这里有一些你错过的边缘情况。如果你删除的单词是列表中的最后一个单词怎么办?在这种情况下,您的代码将无法正常工作,因为请注意:temp=short ->nextbutshort ->next=NULL,因此当您执行代码行时最短->next=temp->next编译器抛出异常tempnullptr.

最新更新