简体   繁体   English

C ++打印链表

[英]C++ print a linked list

I'm new to c++ and am having problems with my print function. 我是c ++的新手,我的打印功能有问题。 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. EDIT2。

Solved, thx alot. 解决了,很多。

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: 您需要first显式初始化,以便按预期执行操作:

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++. 你的链表代码首先是脆弱的,因为你并不是真的在编写(惯用语)C ++。 Even without using the STL, your list can (and should) provide some degree of abstraction. 即使不使用STL,您的列表也可以(并且应该)提供一定程度的抽象。 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: 请注意,如果您不允许使用class ,则这是相同的:

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: 如果你不允许使用构造函数和方法,那么一个好的C风格的等价物可能是:

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. 这仍然将您的数据结构代码分组到一个地方,远离业务逻辑/问题域/测试工具代码。

Initialize first to zero. 首先初始化为零。 You're getting bad characters because the first link isn't initialized. 你得到了不好的字符,因为第一个链接没有初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM