C++ 无法将文本文件中的每个单词存储在c++;
我想从包含以下数据的文本文件中读取: “这是一个包含单词的小文件。” 我想存储每个节点中的每个单词 节点一:这个,节点二:是,节点三:a,节点四:小。。。等 现在,在运行下面的代码时,它只存储最后一个字符“”两次 谁能告诉我我在这里犯了什么错误 我是一个傻瓜,所以从基本上来说C++ 无法将文本文件中的每个单词存储在c++;,c++,file,data-structures,doubly-linked-list,C++,File,Data Structures,Doubly Linked List,我想从包含以下数据的文本文件中读取: “这是一个包含单词的小文件。” 我想存储每个节点中的每个单词 节点一:这个,节点二:是,节点三:a,节点四:小。。。等 现在,在运行下面的代码时,它只存储最后一个字符“”两次 谁能告诉我我在这里犯了什么错误 我是一个傻瓜,所以从基本上来说 struct node { struct node *next; string num; struct node *prev; }; struct node *create_ll(struct n
struct node
{
struct node *next;
string num;
struct node *prev;
};
struct node *create_ll(struct node *start)
{
string word;
ifstream file;
file.open("/Volumes/MAC Extended/Softwares/Xcode/DSA project/DSA project/file.txt",
ios::in);
if (!file)
{
cout << "File Does not Exist";
return 0;
}
struct node *new_node, *ptr;
while (file >> word)
{
if (start == NULL)
{
new_node = new node;
new_node->prev = NULL;
new_node->num = word;
new_node->next = NULL;
start = new_node;
}
else
{
ptr = start;
new_node = new node;
new_node->num = word;
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = new_node;
new_node->prev = ptr;
new_node->next = NULL;
}
}
return start;
}
struct节点
{
结构节点*下一步;
字符串数;
结构节点*prev;
};
结构节点*创建(结构节点*开始)
{
字符串字;
ifstream文件;
打开(“/Volumes/MAC Extended/Softwares/Xcode/DSA project/DSA project/file.txt”,
ios::in);
如果(!文件)
{
cout>word)
{
if(start==NULL)
{
新节点=新节点;
新建_节点->上一个=NULL;
新建_节点->num=word;
新建节点->下一步=空;
开始=新的_节点;
}
其他的
{
ptr=启动;
新节点=新节点;
新建_节点->num=word;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=新建_节点;
新建_节点->prev=ptr;
新建节点->下一步=空;
}
}
返回启动;
}
//
//main.cpp
//DSA项目
//
//由Vivank Sharma于2018年3月26日创建。
//版权所有©2018 Vivank Sharma。保留所有权利。
//
#包括
#包括
#包括
使用名称空间std;
结构节点
{
结构节点*下一步;
字符串数;
结构节点*prev;
};
结构节点*start=NULL;
结构节点*创建所有(结构节点*);
结构节点*显示(结构节点*);
int main()
{
int选项;
做
{
coutnext=新节点;
新建_节点->prev=ptr;
新建节点->下一步=空;
}
}
返回启动;
}
结构节点*显示(结构节点*开始)
{
结构节点*ptr;
ptr=启动;
while(ptr!=NULL)
{
不能包含
#包括
#包括
使用名称空间std;
结构节点
{
结构节点*下一步;
字符串数;
结构节点*prev;
};
结构节点*start=NULL;
结构节点*创建所有(结构节点*);
结构节点*显示(结构节点*);
int main()
{
int选项;
做
{
coutnext=新节点;
新建_节点->prev=ptr;
新建节点->下一步=空;
}
}
返回启动;
}
结构节点*显示(结构节点*开始)
{
结构节点*ptr;
ptr=启动;
while(ptr!=NULL)
{
但是,在C++中,在C++中,你不需要这个结构中的结构。在C++中,你可以考虑写一个链接表类,使用构造函数而不是<代码> CurATEYLL 和自由浮动节点。为什么你要重新实现这个轮并编写你自己的代码< STD::列表< /代码>?为什么不使用?一个std::vector
?它几乎总是表现得更好。整个更正过程将受到赞赏。但根据我@drescherjmw的说法,这并不影响任何事情。你为什么要在else
分支中分配两个新节点?我无法遵循你的逻辑。这是一个只包含代码的答案。它们会产生错误,因此会受到反对。Th答案可以通过指出你改变了什么,为什么改变它,以及它如何解决提问者的问题来挽救。
//
// main.cpp
// DSA project
//
// Created by Vivank Sharma on 26/03/18.
// Copyright © 2018 Vivank Sharma. All rights reserved.
//
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
struct node
{
struct node *next;
string num;
struct node *prev;
};
struct node *start = NULL;
struct node *create_ll(struct node *);
struct node *display(struct node *);
int main()
{
int option;
do
{
cout<<"\n\n *****MAIN MENU *****";
cout<<"\n 1: Create a list";
cout<<"\n 2: Display the list";
cout<<"\n\n Enter your option : ";
cin>>option;
switch(option)
{
case 1:
{ start = create_ll(start);
cout<<"\n DOUBLY LINKED LIST CREATED";
}
break;
case 2:
{
start = display(start);
}
break;
}
}while(option!=3);
}
struct node *create_ll(struct node *start)
{
string word;
ifstream file;
file.open("/Volumes/MAC Extended/Softwares/Xcode/DSA project/DSA project/file.txt",ios::in);
if (!file) {
cout << "File Does not Exist";
return 0;
}
struct node *new_node, *ptr;
while(file>>word)
{
if(start == NULL)
{
new_node = new node;
new_node -> prev = NULL;
new_node -> num = word;
new_node -> next = NULL;
start = new_node;
}
else {
ptr=start;
new_node = new node;
new_node->num=word;
while(ptr->next!=NULL)
ptr = ptr->next;
ptr->next = new_node;
new_node->prev=ptr;
new_node->next=NULL;
}
}
return start;
}
struct node *display(struct node *start)
{
struct node *ptr;
ptr=start;
while(ptr!=NULL)
{
cout<<ptr -> num<<endl;
ptr = ptr -> next;
}
return start;
}
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
struct node
{
struct node *next;
string num;
struct node *prev;
};
struct node *start = NULL;
struct node *create_ll(struct node *);
struct node *display(struct node *);
int main()
{
int option;
do
{
cout<<"\n\n *****MAIN MENU *****";
cout<<"\n 1: Create a list";
cout<<"\n 2: Display the list";
cout<<"\n\n Enter your option : ";
cin>>option;
switch(option)
{
case 1:
{ start = create_ll(start);
cout<<"\n DOUBLY LINKED LIST CREATED";
}
break;
case 2:
{
start = display(start);
}
break;
}
}while(option!=3);
}
struct node *create_ll(struct node *start)
{
string word;
ifstream file;
file.open("/Volumes/MAC Extended/Softwares/Xcode/DSA project/DSA
project/file.txt",ios::in);
if (!file) {
cout << "File Does not Exist";
return 0;
}
struct node *new_node, *ptr;
while(file>>word)
{
if(start == NULL)
{
new_node = new node;
new_node -> prev = NULL;
new_node -> num = word;
new_node -> next = NULL;
start = new_node;
}
else {
ptr=start;
new_node = new node;
new_node->num=word;
while(ptr->next!=NULL)
ptr = ptr->next;
ptr->next = new_node;
new_node->prev=ptr;
new_node->next=NULL;
}
}
return start;
}
struct node *display(struct node *start)
{
struct node *ptr;
ptr=start;
while(ptr!=NULL)
{
cout<<ptr -> num<<endl;
ptr = ptr -> next;
}
return start;
}