在链表c++中归档
Filing in linked list c++
我正在尝试通过链表进行归档。我想做的是将文本文件中的每个单词放在一个新节点上,但这个程序将所有单词放在单个节点上。例如,如果我的文本文件中有一行"我的名字是ahsan",那么它的输出应该是:
我的
名称
是
ahsan
而我的程序正在按原样打印这行。
#include <iostream>
#include<fstream>
using namespace std;
class node
{
public:
string data;
node*next;
};
class mylist
{
public:
node*head;
mylist()
{
head=NULL;
}
void insertion(string item)
{
node* temp= new node;
temp->data=item;
temp->next=NULL;
temp->next=head;
head=temp;
}
void print()
{
node*ptr;
if(head==NULL)
{
cout<<"List empty :"<<endl;
return;
}
else
{
ptr=head;
while(ptr!=NULL)
{
cout<<ptr->data<<endl<<endl;
ptr=ptr->next;
}
}
}
};
int main()
{
ofstream myfile;
ifstream infile;
string mystring;
mylist l;
// myfile.open ("ahsan.txt");
// myfile << "Ahsan's first file.n";
// myfile.close();
string lol;
infile.open("ahsan.txt");
while(!infile.eof())
{
getline(infile,lol);
l.insertion(lol);
}
l.print();
infile.close();
}
这是因为您使用了getline。getline按行阅读文本,而不是按单词阅读。相反,您可以使用输入流来完成此操作。
infile >> myString;
它将读取每个单词,假设你想按空格分割。。。
相关文章:
- 反向给定链表中的K节点
- 如果没有malloc,链表实现将失败
- 文本文件中的单词链表
- 努力将整数转换为链表。不知道我在这里做错了什么
- 链表,反向函数,数据结构
- 使用std::list创建循环链表
- 链表的泛型函数remove()与成员函数remove)
- 为什么不能修改对象中的值?另外,我如何改进此链表?
- 我们可以删除链表中静态内存中的节点吗
- C++,指针数组,指向双链表中的条目
- 链表中写入访问冲突的未知原因
- 如何修复链表类实现的未处理异常0xDDDDDDDD
- 打印所有链表的元素 (C++)
- 错误:需要类名(链表c++)
- 为什么C中的通用链表中存储的数据已损坏
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 链表c++插入,所有情况都已检查,但没有任何工作
- 链表,将列表复制到另一个列表
- 为什么C++对链表中的下一个节点使用指针,而像 C# 或 Java 这样的语言只使用类 Node 的名称?
- 对单向链表进行排序时出现运行时错误