无法将文本文件中的每个单词存储在c ++中的双向链表中

时间:2018-03-26 19:07:23

标签: c++ file data-structures doubly-linked-list

我想从包含以下数据的文本文件中读取:

"这是一个包含单词的小文件。"

我想在每个节点中存储每个单词,例如。

node1:this,node2:is,node3:a,node4:small ... etc

现在,在运行下面的代码时,它只是存储最后一个字符" "两次。

任何人都可以提出我在这里犯的错误。

我是一个菜鸟,所以只是从基础知识。

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;
}

2 个答案:

答案 0 :(得分:0)

//
//  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;
}

这样做。发现了错误。

答案 1 :(得分:-1)

#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;
}
  

试试这段代码