Link to home
Start Free TrialLog in
Avatar of PALKTA
PALKTA

asked on

Singly linked list

I need some help with this,

i have a program whereby i read in a sentence from a data file,

The problem is that i need to read this sentence into a singly or doubly linked list but i am not all that sure on how to use these

The sentence should be read in word by word but i should also be able to print this list out one word at a time!!!

Thanks in advance.
ASKER CERTIFIED SOLUTION
Avatar of Dariuzk
Dariuzk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mayank S
1. Declare a structure/ class which will implement a linked list (holding string data type).

2. Read the statement from the file using ifstream and the getline () function.

3. Separate the statement into words using strtok () function.

4. Store these words into nodes in the linked list, creating as many nodes as needed.

Please post a comment if you need more help.

Mayank.
Avatar of nietod
nietod

Dariuzk, this is opviously a homework question.   Do not violate EE's guidelines for accademic honesty.

***************************************

We cannot do your schoolwork for you.  That is unethical and is grounds for removal from this site.  (for both you and the experts involved.)  

We can provide only limitied help in accademic assignments.    We can answer specific (direct) questions, like you might ask your teacher.  We can review your work and post suggestions, again, like your teacher might do.

Do you have specific questions?
Do you have any work on this (incomplete even) that we can review?
Sorry nietod, it's my foult.My english is bad, so it is more easy for me write an example...
Dariuzk:

methink your destructor crash with 0 and 1 element
and leaks with 2 or more elements.

I wonder if the teacher caught the problem ?

Danderson
List::~List(){
    if (this->data){
        node *i;
        while(this->data){
                i = this->data;
                this->data = this->data->next;
                delete i;
        }
    }
}

this is better version of destructor :)
That's really not the best code.  Yes its better but.....

You know, its very possible that you are not ready to be an expert in C++.
List::~List(){
    if (this->data){
        node *i;
        while(this->data){
                i = this->data;
                this->data = this->data->next;
                delete i;
        }
    }
}

this is better version of destructor :)
Dariuzk:
2 things:

1. Prefer passing const ref instead of whole object as in:
   add(string item)
Replace with:
   add(const string& item)

you will save ctor/dtor for temporary object

2. when you add a new node, if string copy fail (throw) you will end up with memory leak!!! use RAII

Even simple link list can be difficult to write.
I use to write this exact code 15 years ago in C, but in C++, since they added exception (1992) lots of old habits had to go!
I can't look at that code and think that exception safety is the biggest issue!
Nietod:

what is the biggest issue ?

Well, the code doesn't work when exceptions don't occur, its hard to worry about what it does when exceptions do occur...  Even if it did work, its unnecessarily compplex and innefficient.  The class design is very questionable.  (why doesn't the node class have any responsibilities, why does the list class perform some of node's work.  why does the the list have a constructor that adds an item (unsafe if not declared explicit, questionable if declared explicit))  The classes are not safe under copy construction and assignment and there is no provision to prevent these operations..    That's just to start!
Avatar of PALKTA

ASKER

Thank you Dariuzk this was very helpful and very much appreciated!!!!
PLAKTA, you DON"T wnat to use that code.  Its full of bugs and the parts that work are poorly designed!
Yes, I did bad code this time, I also I have larned some stuff, thanks for showing my error's. But can you nietod show good "node" and "list" class design?