【问题标题】:Linked List for loop printing garbage c++用于循环打印垃圾c ++的链表
【发布时间】:2018-02-05 02:05:47
【问题描述】:

我正在使用链表来模拟计算机实验室,并且我正在尝试打印链表。使用 cout 打印存在问题,它会产生垃圾,直到终端放弃并发送分段错误 11。

#include <iostream>
#include <string>

using namespace std;

struct lab {
    string current_ID;
    int station_number;
    lab *link;
};

typedef lab* lab_ptr;


void print_status(lab_ptr& head1, lab_ptr& head2, lab_ptr& head3, lab_ptr& head4);

int main()
{
    lab_ptr head_lab_1;
    head_lab_1 = new lab;

    lab_ptr head_lab_2;
    head_lab_2 = new lab;

    lab_ptr head_lab_3;
    head_lab_3 = new lab;

    lab_ptr head_lab_4;
    head_lab_4 = new lab;

    set_up_linked_list_for_n_stations(head_lab_1, 5);
    set_up_linked_list_for_n_stations(head_lab_2, 6);
    set_up_linked_list_for_n_stations(head_lab_3, 4);
    set_up_linked_list_for_n_stations(head_lab_4, 3);


    return 0;
}



void set_up_linked_list_for_n_stations(lab_ptr& head, int n)
{
    lab_ptr curr;
    curr = new lab;

    for(int j = 0; j < n; j++)
    {
        lab_ptr temp = new lab;
        temp->link = NULL;
        temp->station_number = n+1;
        temp->current_ID = 'empty';

        cout << temp->station_number << " " << temp->current_ID << endl;

        if(head != NULL)
        {
            curr = head;
            while(curr->link != NULL)
            {
                curr = curr->link;
            }
            curr->link = temp;
        } else
        {
            head = temp;
        }
    }
}

这实际上是我第一次使用链表,所以错误可能非常明显,我只是错过了。如果这是一个愚蠢的错误,请见谅。

【问题讨论】:

    标签: c++ printing linked-list


    【解决方案1】:

    对于字符串成员 current_ID,您需要使用双引号传递值:

    temp->current_ID = 'empty';
    

    变成

    temp->current_ID = "empty";
    

    您还需要将函数 void set_up_linked_list_for_n_stations(lab_ptr& head, int n) 移到 main() 之前

    【讨论】:

    • 我也在尝试用这样的函数打印整个东西,但它仍然给出了垃圾 lab_ptr 迭代器; cout link) { cout station_number current_ID
    【解决方案2】:

    您正在进行额外分配,其他地方列表未正确初始化。将head_lab_1 初始化为nullptr(或只是NULL

    在函数set_up_linked_list_for_n_stations中删除curr = new lab

    void set_up_linked_list_for_n_stations(lab_ptr& head, int n)
    {
        for(int j = 0; j < n; j++)
        {
            lab_ptr temp = new lab;
            temp->link = NULL;
            temp->station_number = n + 1;
            temp->current_ID = "empty";
            cout << temp->station_number << " " << temp->current_ID << endl;
    
            if(head != NULL)
            {
                lab_ptr curr = head;
                while(curr->link != NULL)
                {
                    curr = curr->link;
                }
                curr->link = temp;
            }
            else
            {
                head = temp;
            }
        }
    }
    
    int main()
    {
        lab_ptr head_lab_1 = nullptr;
        lab_ptr head_lab_2 = nullptr;
        lab_ptr head_lab_3 = nullptr;
        lab_ptr head_lab_4 = nullptr;
    
        set_up_linked_list_for_n_stations(head_lab_1, 5);
        set_up_linked_list_for_n_stations(head_lab_2, 6);
        set_up_linked_list_for_n_stations(head_lab_3, 4);
        set_up_linked_list_for_n_stations(head_lab_4, 3);
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2022-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多