文本文件中的单词链表
linked list from the text file by words
我想按单词读取文本文件,然后将每个单词应用于链表
但当我应用时,整个内容将转到链接列表的第一个节点
知道我在代码中需要修改什么吗
更新
我不知道如何在链表上迭代单词我知道我需要在这段时间内再循环一次,但我不知道该怎么做
它是用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;
}
接下来,如注释中所述,对addValue
和popValue
使用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>?
相关文章:
- 反向给定链表中的K节点
- 如果没有malloc,链表实现将失败
- 文本文件中的单词链表
- 努力将整数转换为链表。不知道我在这里做错了什么
- 链表,反向函数,数据结构
- 使用std::list创建循环链表
- 链表的泛型函数remove()与成员函数remove)
- 为什么不能修改对象中的值?另外,我如何改进此链表?
- 我们可以删除链表中静态内存中的节点吗
- C++,指针数组,指向双链表中的条目
- 链表中写入访问冲突的未知原因
- 如何修复链表类实现的未处理异常0xDDDDDDDD
- 打印所有链表的元素 (C++)
- 错误:需要类名(链表c++)
- 为什么C中的通用链表中存储的数据已损坏
- 在C++中拆分带有链表的单词
- 将地址和布尔值存储在一个单词中,以实现无锁双链表
- 需要修改堆栈/链表程序以确定单词是否为回文
- 将单词插入链表时出错
- 对字符串链表中特定单词的出现次数进行计数.如何在log(n)时间内完成