?
Solved

Singly linked list

Posted on 2003-03-20
15
Medium Priority
?
2,908 Views
Last Modified: 2013-12-14
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
Comment
Question by:PALKTA
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 3
  • +2
15 Comments
 
LVL 2

Accepted Solution

by:
Dariuzk earned 1000 total points
ID: 8172993
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8173004
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
 
LVL 22

Expert Comment

by:nietod
ID: 8173217
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Expert Comment

by:Dariuzk
ID: 8173367
Sorry nietod, it's my foult.My english is bad, so it is more easy for me write an example...
0
 
LVL 1

Expert Comment

by:DAnderson
ID: 8174039
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
 
LVL 2

Expert Comment

by:Dariuzk
ID: 8179550
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
 
LVL 22

Expert Comment

by:nietod
ID: 8180222
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
 
LVL 2

Expert Comment

by:Dariuzk
ID: 8180675
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
 
LVL 1

Expert Comment

by:DAnderson
ID: 8180782
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
 
LVL 22

Expert Comment

by:nietod
ID: 8181237
I can't look at that code and think that exception safety is the biggest issue!
0
 
LVL 1

Expert Comment

by:DAnderson
ID: 8181293
Nietod:

what is the biggest issue ?

0
 
LVL 22

Expert Comment

by:nietod
ID: 8181452
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
 

Author Comment

by:PALKTA
ID: 8201652
Thank you Dariuzk this was very helpful and very much appreciated!!!!
0
 
LVL 22

Expert Comment

by:nietod
ID: 8202097
PLAKTA, you DON"T wnat to use that code.  Its full of bugs and the parts that work are poorly designed!
0
 
LVL 2

Expert Comment

by:Dariuzk
ID: 8212786
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

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question