如何在c++中将文件的每一行都分配给双链表的节点



我有一个单独的类,它有这个赋值方法,我需要使用我的双链表实现来实现这个类的方法

typedef string Elem;
class A{
private:
public:
method assign {
ifstream infile("initial_text.txt",ios::in);
string current;
string temp=" ";
int count=0;
while(getline(infile, current))
{
for(int i=0;i<current.size();i++) {
temp+=current[i];
if(current[i]=='.' || current[i]=='?' || current[i]=='!') {
cout<<temp<<"n";
count++;
temp="";
}

}
}
cout<<"Total Sentences: "<<count<<"n";

}
class Node {
public:
Node* next;
Node* prev;
Elem elem;
friend class Linkedlist;
Node(): next(NULL), prev(NULL)
{}
Node(Elem elem) : elem(elem)
{}
};
class Linkedlist { 
private:
Node *head;
Node *tail;
int N;
public:
Linkedlist();//
~Linkedlist();//

Linkedlist::Linkedlist() {
N = 0;                  
head = new Node;              
tail  = new Node;
head->next = tail;         
tail->prev = head;
}
Linkedlist::~Linkedlist() {
Node *current = head;
while (current)
{
Node* next = current->next;
delete current;
current = next;
}
}

目前,它只是通过将每个句子分配给一行来过滤文本文件,在这个循环中,我需要将其分配给链表的节点,但我不确定如何实现它

谢谢!

可能是这样的:

void assign() {
ifstream infile("initial_text.txt",ios::in);
string current;
string temp=" ";
int count=0;
LinkedList list;  // create a linked list
while(getline(infile, current))
{
for(int i=0;i<current.size();i++) {
temp+=current[i];
if(current[i]=='.' || current[i]=='?' || current[i]=='!') {
cout<<temp<<"n";
// add `temp` to the end of the list
list.add(temp);
count++;
temp="";
}
}
}
cout<<"Total Sentences: "<<count<<"n";
}
class Linkedlist { 
private:
Node *head;
Node *tail;
int N;
public:
Linkedlist();//
~Linkedlist();//
void add(Elem elem);  // TODO: implement this!!!
/* These may be useful as well, but what functions you want to provide depend
* on your application
*/
void print();       // print entire list
void remove(...);   // remove an element
void at(int i);     // retrieve the element at index i
void size() { return N; }
// alternatively, iterator allows standard container access
class iterator : public std::iterator<bidirectional_iterator_tag, Elem> {
Node *node_;
public:
explicit iterator(Node *node) : node_(node) {}
iterator& operator++() { ... }           // prefix operator, i.e. ++it
iterator operator++(int) { ... }         // postfix operator, i.e. it++
bool operator==(iterator other) { ... }  // equality operator, i.e. it1 == it2
bool operator!=(iterator other) { ... }  // not equal operator, i.e. it1 != it2
Elem& operator*() { ... }                // dereference operator, i.e. string &tmp = *it
}
iterator begin() { return iterator(head->next); }
iterator end() { return iterator(tail); }
}

在这里,我们封装了添加到LinkedList的功能,这样您只需实现一次,如果操作得当,就不必担心内存泄漏。如果您愿意,iterator还提供标准的c++风格的双向容器访问,这样您就可以轻松地遍历链表,并在STL 中使用预先制作的算法

LinkedList list;
... // populate list
// print list
for (auto elem : list)
std::cout << elem << std::endl;;
/*** STL pre-defined functions ***/
// reverse the list in place
reverse(list);
// remove duplicates in the list
auto last = std::unique(list.begin(), list.end());
list.erase(last, list.end());
// finds location of "hello" in list, or N if it is not in the list
auto index = std::find(list.begin(), list.end(), "hello") - list.begin();
// count the number of elements beginning with a capital letter
int numCaps = std::count_if(list.begin(), list.end(),
[](int elem){ return isupper(elem[0]); });
// append newline to every element
std::for_earch(list.begin(), list.end(), [](string &elem) { elem += "n"; });

注意:我没有测试这个代码,但它应该让你知道如何继续。

最新更新