无法将文本文件中的每个单词存储在C ++中的双向链表中
Unable to store each word from text file in doubly linked list in c++
我想从包含以下数据的文本文件中读取:
"This is a small file containing words ."
我想将它的每个单词存储在每个节点中。
node1:这个,node2:是,node3:a,node4:小...等等
现在,在下面的 运行 代码中,它只是将最后一个字符“ .” 存储了两次。
任何人都可以指出我在这里犯了什么错误。
我是菜鸟,所以只是从基础开始。
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;
}
//
// 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;
}
try this code
我想从包含以下数据的文本文件中读取:
"This is a small file containing words ."
我想将它的每个单词存储在每个节点中。
node1:这个,node2:是,node3:a,node4:小...等等
现在,在下面的 运行 代码中,它只是将最后一个字符“ .” 存储了两次。
任何人都可以指出我在这里犯了什么错误。
我是菜鸟,所以只是从基础开始。
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;
}
//
// 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;
}
try this code