Pooths
Pooths

Reputation: 3

C++ print a linked list

I'm new to c++ and am having problems with my print function. This might be a really simple problem but I can't find out how to solve it.

Before I start to write the code I might add that this is supposed to be a circular list.

First of all, here is my linked lists structure

struct CL;

typedef CL* list_type;

struct person
{
    string last_name;
    string first_name;
    string tel_nr;
};

struct CL
{
    person data;
    list_type next;
};

As you can see, I want the list to contain a data and a pointer. The data is a person(last name, first name and telephone number(as a string)).

My main program look like this

int main ()
{
    list_type list;
    list_type first;
    string line;
    person info;
    ifstream myfile ("INFILE.TXT");
    if (myfile.is_open())
    {
        while (myfile.good())
        {
            getline (myfile,line,',');
            info.last_name=line;
            getline(myfile,line,' ');
            getline(myfile,line,':');
            info.first_name=line;
            getline(myfile,line);
            info.tel_nr=line;
            if(first==0)
            {
                list = new CL;
                first = list;
                list->data = info;
                list->next = 0;
            }
            else
            {
                list->next = new CL;
                list = list->next;
                list->data = info;
                list->next = 0;
            }

        }

        list->next = first;
        print(list);
        myfile.close();
    }
    else cout<<"Unable to open file.";
    return 0;
}

and now to the part where I am having problems, the print function.

void print(CL* cur)
{
    list_type first;
    first=cur;
    int x;
    do
    {
        cout<<"\n"<<"Your Data is: ";
        cout<<cur->data.last_name<<cur->data.first_name<<cur->data.tel_nr;
        //I guess this is where the fault lies ^.
        cur = cur->next;
    }
    while(cur != first);
}

If possible I would love an explanation, not just the correct code.

Thanks

Edit. The result I am getting is alot of weird characters like:

ê,(,?,ý and alot of other characters I don't know how to type.

The result I am expecting is something like this

Robertson Linda 0838-2345
Brown Charles 068-24567
etc until the end of list

Edit2.

Solved, thx alot.

Upvotes: 0

Views: 1669

Answers (2)

Useless
Useless

Reputation: 67713

First problem

int main ()
{
    list_type list;
    list_type first; // uninitialized value
    // ...
            if(first==0) // ???
            {

You need to initialize first explicitly for this to do what you expect:

int main ()
{
    list_type list;
    list_type first = 0;
    // ...
            if(first==0)
            {

Second problem

Your linked list code is fragile in the first place because you're not really writing (idiomatic) C++. Even without using the STL, your list can (and should) provide some degree of abstraction. It's actually easier to write it properly, because your data structure logic doesn't get all mixed up with your test-harness or problem domain logic.

For example,

class PersonList {
    CL *head;
    CL *tail;

public:
    PersonList(); // think about how to initialize an empty list

    void push_back(Person const&); // head & tail modified in here
    // etc.
};

Note that if you're not allowed to use class, this is identical:

struct PersonList {
private:
    CL *head;
    // ... as above ...

and if you're not allowed to use constructors & methods at all, a good C-style equivalent might be:

struct PersonList {
    CL *head;
    CL *tail;
};
struct PersonList * new_list();
void push_back(struct PersonList *, Person const &);

this still groups your data structure code in one place, away from your business logic/problem domain/test harness code.

Upvotes: 2

Digikata
Digikata

Reputation: 1959

Initialize first to zero. You're getting bad characters because the first link isn't initialized.

Upvotes: 0

Related Questions