文本文件中的单词链表

linked list from the text file by words

本文关键字:单词 链表 文件 文本      更新时间:2023-10-16

我想按单词读取文本文件,然后将每个单词应用于链表

但当我应用时,整个内容将转到链接列表的第一个节点

知道我在代码中需要修改什么吗

更新

我不知道如何在链表上迭代单词我知道我需要在这段时间内再循环一次,但我不知道该怎么做

它是用C++构建的

该文件正在通过文字显示,但我不明白的是如何将文字链接到其他

代码:

#include <bits/stdc++.h>
#include <iostream>
#include<string>
using namespace std; 

class LinkedList{
// Struct inside the class LinkedList
// This is one node which is not needed by the caller. It is just
// for internal work.
struct Node {
string x;
Node *next;
};
// public member
public:
// constructor
LinkedList(){
head = NULL; // set head to NULL
}
// destructor
~LinkedList(){
Node *next = head;

while(next) {              // iterate over all elements
Node *deleteMe = next;
next = next->next;     // save pointer to the next element
delete deleteMe;       // delete the current entry
}
}

// This prepends a new value at the beginning of the list
void addValue(string val){
Node *n = new Node();   // create new Node
n->x = val;             // set value
n->next = head;         // make the node point to the next node.
//  If the list is empty, this is NULL, so the end of the list --> OK
head = n;               // last but not least, make the head point at the new node.
}
// returns the first element in the list and deletes the Node.
// caution, no error-checking here!
string popValue(){
Node *n = head;
string ret = n->x;
head = head->next;
delete n;
return ret;
}
// private member
private:
Node *head; // this is the private member variable. It is just a pointer to the first Node
};
int main() { //linkedlist
LinkedList list;
//string usama="usama";
//list.addValue(usama);
//list.addValue("h");
//list.addValue("u");
//cout << list.popValue() << endl;
//cout << list.popValue() << endl;
//cout << list.popValue() << endl;
// because there is no error checking in popValue(), the following
// is undefined behavior. Probably the program will crash, because
// there are no more values in the list.
// cout << list.popValue() << endl;

//file
// filestream variable file 
fstream file; 
string word, t, q, filename; 

// filename of the file 
filename = "file.txt"; 

// opening file 
file.open(filename.c_str()); 

// extracting words from the file 
while (file >> word) 
{ 
list.addValue(word);
cout<<list.popValue()<<endl;
// displaying content 
//cout << word << endl; 
} 
return 0;
}

我知道while循环可能有问题,但我被卡住了

您有几个问题。第一种是CCD_ 1不处理CCD_。当您尝试string ret = n->x;时,这可能会导致segfault。您可以在head上添加检查并初始化ret以避免此问题(稍后将使用空的ret终止迭代(

string popValue(){
Node *n = head;
string ret {};

if (!head)          /* validate head not nullptr */
return ret;

ret = n->x;
head = head->next;
delete n;
return ret;
}

接下来,如注释中所述,对addValuepopValue使用1个循环。这违背了列表的目的,因为当popValue()0在循环结束时将列表留空时,add的每个节点都会被删除。使用2个循环,例如:

// extracting words from the file 
while (file >> word)
list.addValue(word);
while ((t = list.popValue()).length())
cout << t << 'n';

(注意:(t = list.popValue()).length()在达到空字符串时终止迭代——最好让popValue()返回一个节点,而不是string

将要读取的文件名作为第一个参数的一个简短示例是:

#include <iostream>
#include <fstream>
#include <string>
using namespace std; 
class LinkedList {
struct Node {
string x;
Node *next;
};
public:
LinkedList(){
head = NULL; // set head to NULL
}
~LinkedList(){
Node *next = head;

while(next) {              // iterate over all elements
Node *deleteMe = next;
next = next->next;     // save pointer to the next element
delete deleteMe;       // delete the current entry
}
}

void addValue(string val){
Node *n = new Node();   // create new Node
n->x = val;             // set value
n->next = head;         // make the node point to the next node.
//  If the list is empty, this is NULL, so the end of the list --> OK
head = n;               // last but not least, make the head point at the new node.
}
string popValue(){
Node *n = head;
string ret {};

if (!head)          /* validate head not nullptr */
return ret;

ret = n->x;
head = head->next;
delete n;
return ret;
}
private:
Node *head; // this is the private member variable. It is just a pointer to the first Node
};
int main (int argc, char **argv) { //linkedlist

LinkedList list;
// filestream variable file 
fstream file; 
string word, t, q, filename; 
// opening file 
if (argc < 2)
return 1;

file.open(argv[1]); 
if (!file.is_open()) {
cerr << "file open failed.n";
return 1;
}

// extracting words from the file 
while (file >> word)
list.addValue(word);
while ((t = list.popValue()).length())
cout << t << 'n';
return 0;
}

示例输入文件

$ cat dat/captnjack.txt
This is a tale
Of Captain Jack Sparrow
A Pirate So Brave
On the Seven Seas.

示例使用/输出

$ ./bin/llwords dat/captnjack.txt
Seas.
Seven
the
On
Brave
So
Pirate
A
Sparrow
Jack
Captain
Of
tale
a
is
This

最后请参阅为什么"使用命名空间std;"被认为是不好的做法?以及为什么我不应该#include<bits/stdc++.h>?