【发布时间】:2020-11-13 10:04:54
【问题描述】:
我有一个单独的类有这个assign方法,我需要使用我的双链表实现这个类的方法
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;
}
}
现在它只是通过将每个句子分配到一个新行来过滤文本文件,在这个循环中我需要将它分配给链表的节点,但我不确定如何实现它。
谢谢!
【问题讨论】:
-
请尝试提出您的问题,就好像读者最初不知道您在说什么一样。花更多时间解释,强迫自己写出细节。 示例:“单独的类”与什么分开?我读了“我的双链表”并想“你有一个链表吗?我不知道。”
-
您需要做的是将一个复杂的问题拆分为两个较小的问题。问题 1 是编写一些方法来将项目添加到您的链表中。目前链表没有方法,所以你不能用它做任何事情。当你完成问题 1(而不是之前)后,开始问题 2,即使用你在问题 1 中编写的方法将句子添加到链表中。这就是所有编程的工作方式,处理一个复杂的问题并分解成更小的部分。
标签: c++ linked-list doubly-linked-list