-3

This is a small part of my project at school. I have stored my data into a text file in this form:

bike
dock
car

Now, I would like to read the data from this file into a doubly linked list. This is the code I used.

#include <iostream>
#include <strings.h>
#include <fstream>

using namespace std;

class item
{
public:
    string name;
    item *next;
    item *previous;
};

class list:private item
{
    item * first;
public:
    list();
    void load();
    void display();
};

list::list()
{
    first = NULL;
}

void list::load()
{
    item *current;
    ifstream fin;

    fin.open("List.txt");

    current=new item;

    while(!fin.eof())
    {
        fin>>current->name;
        current->previous=NULL;
        current->next=NULL;
    }

    fin.close();

    first=current;
}

Now, the problem is I am unable to store each word of the file into a new node. This code stores the last word of the file to the first node of the list. I have no clue how to do this. I am not that good in linked lists. Can anyone help me resolve this problem?

Vipanchith Reddy
  • 484
  • 7
  • 14
  • 2
    You allocate only one single item with `new` outside the loop. You should allocate a new item for each word read from the file. – Jabberwocky Aug 24 '15 at 07:21
  • 4
    Any reason you aren't using `std::list`? – user657267 Aug 24 '15 at 07:21
  • You are trying to do too much at once. You should try constructing a list from hard-coded data, before you worry about file I/O. – Beta Aug 24 '15 at 07:23
  • 1
    You do the `new item` outside of the loop, so you only get one node in the list. Also, using `while(!fin.eof)` is [almost always the wrong way](http://stackoverflow.com/questions/5764579/eof-problem-c). – Bo Persson Aug 24 '15 at 07:24
  • @Beta he probably wants to practice. – Jabberwocky Aug 24 '15 at 07:25
  • 1
    I am still learning C++. Our teacher just started teaching us linked lists. We were not taught anything about std::list . That's the reason I am not using it. – Vipanchith Reddy Aug 24 '15 at 07:26
  • @VipanchithReddy In that case you might want to make it clear in the question that this is homework, otherwise you'll just get a 4-line solution using the library. – user657267 Aug 24 '15 at 07:26
  • Sorry. I forgot to mention that. I have now added it to the question. – Vipanchith Reddy Aug 24 '15 at 07:29
  • @Michael Walz I know I am creating only a single node. That's the problem. I have no clue how to store each word in a separate node. – Vipanchith Reddy Aug 24 '15 at 07:32
  • @VipanchithReddy you need one node for each word. Take a pice of paper and a pencil and draw your linked list. I think you are missing the very basics. – Jabberwocky Aug 24 '15 at 07:42

1 Answers1

1

Well, try something like this for the load function (not tested):

void list::load()
{
    item *current;
    item *prev;
    ifstream fin;

    fin.open("List.txt");


    prev = NULL;
    while(!fin.eof())
    {
        current=new item;
        if (first == NULL) // we set first the first time
            first = current;
        fin>>current->name;
        current->previous=prev;
        current->next = NULL;
        if (prev != NULL)
            prev->next=current;
        prev = current; // we store the current as the prev for next iteration
    }

    fin.close();
}
Guillaume Kiz
  • 443
  • 4
  • 10
  • 1
    the (prev != NULL) should be setting prev->next, whereas current->previous will always = prev (even if its NULL) – softwarenewbie7331 Aug 24 '15 at 07:33
  • It worked after doing that change. The problem was I was actually testing the sample code in a new project which didn't have the text file in it. So, it had no file to load. Thank you for your help. – Vipanchith Reddy Aug 24 '15 at 08:25