• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2910
  • Last Modified:

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.
0
PALKTA
Asked:
PALKTA
  • 5
  • 5
  • 3
  • +2
1 Solution
 
DariuzkCommented:
Hi, I wrote for you simple program, I hope it will be good starting point for you...

#include <string>
#include <iostream>
#include <fstream>

using namespace std;

struct node{
    string str;
    node   *next;
};

class List {
    node *data;
public:
    List(): data(NULL){};
    List(string item);
    ~List();
    void add(string item);
    void printout();
};

List::List(string item)
{
//add first node
   node *newnode = new node;
   newnode->str = item;
   newnode->next = NULL;
   data = newnode;
}

List::~List(){
    node *i;
    node *temp=this->data;
    for(i=this->data->next; i->next != NULL; i = i->next){
        delete temp;
        temp = i;
    }  
}

void List::add(string item){
    //if list is empty
    if (data == NULL){
        //add first node
        node *newnode = new node;
        newnode->str = item;
        newnode->next = NULL;
        data = newnode;
    }
    else{
        //get to the end of list  
        node *i;
        for(i=this->data; i->next != NULL; i = i->next);
        //insert new item
        node *newnode = new node;
        newnode->str = item;
        newnode->next = NULL;
        i->next = newnode;
    }
}

void List::printout(){
    node *i=this->data;
    for(i=this->data; i != NULL; i=i->next)
        cout << i->str << endl;
}
int main() {
    List *words = new List();
       
    ifstream in("main.cpp");
    string word;
   
    while(in >> word)//word is detected by white space
        words->add(word);
   
    words->printout();
   
    delete words;
       
system("PAUSE");
return 0;
}
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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.
0
 
nietodCommented:
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?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
DariuzkCommented:
Sorry nietod, it's my foult.My english is bad, so it is more easy for me write an example...
0
 
DAndersonCommented:
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
0
 
DariuzkCommented:
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 :)
0
 
nietodCommented:
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++.
0
 
DariuzkCommented:
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 :)
0
 
DAndersonCommented:
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!
0
 
nietodCommented:
I can't look at that code and think that exception safety is the biggest issue!
0
 
DAndersonCommented:
Nietod:

what is the biggest issue ?

0
 
nietodCommented:
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!
0
 
PALKTAAuthor Commented:
Thank you Dariuzk this was very helpful and very much appreciated!!!!
0
 
nietodCommented:
PLAKTA, you DON"T wnat to use that code.  Its full of bugs and the parts that work are poorly designed!
0
 
DariuzkCommented:
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?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 5
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now