C++ 如何在不使用“添加”的情况下向链接列表添加值-&燃气轮机&引用;? void CommunicationNetwork::buildNetwork(){ 字符串城市列表[]={“凤凰城”、“丹佛”、“达拉斯”、“圣路易斯”、“芝加哥”、“亚特兰大”、“华盛顿特区”、“纽约”、“波士顿”}; head=(“洛杉矶”,空,空); 城市*temp2=新城; 城市*临时=新城市; 温度=水头; 对于(int i=0;icityName=citylist[i];//=Pheonix) temp->next=temp2;//此处出现分段错误 temp=temp2; } }

C++ 如何在不使用“添加”的情况下向链接列表添加值-&燃气轮机&引用;? void CommunicationNetwork::buildNetwork(){ 字符串城市列表[]={“凤凰城”、“丹佛”、“达拉斯”、“圣路易斯”、“芝加哥”、“亚特兰大”、“华盛顿特区”、“纽约”、“波士顿”}; head=(“洛杉矶”,空,空); 城市*temp2=新城; 城市*临时=新城市; 温度=水头; 对于(int i=0;icityName=citylist[i];//=Pheonix) temp->next=temp2;//此处出现分段错误 temp=temp2; } },c++,C++,City是链表的结构。next指向下一个节点。我遇到了一个分段错误,因为我试图为temp取消对NULLnext值的引用。这是我唯一能想到的向temp添加值的方法。是否有其他方法可以用temp2替换NULLestemp->next=temp2?这不是实现链表的正确方法。它应该看起来更像这样: void CommunicationNetwork:: buildNetwork(){ string citylist [] = {"Phoenix", "Denver", "Dallas", "S

City
是链表的结构。
next
指向下一个节点。我遇到了一个分段错误,因为我试图为
temp
取消对NULL
next
值的引用。这是我唯一能想到的向
temp
添加值的方法。是否有其他方法可以用
temp2
替换NULLes
temp->next=temp2

这不是实现链表的正确方法。它应该看起来更像这样:

void CommunicationNetwork:: buildNetwork(){

    string citylist [] = {"Phoenix", "Denver", "Dallas", "St. Louis", "Chicago", "Atlanta", "Washington D.C.", "New York", "Boston"};

    head = ("Los Angeles", NULL, NULL);
    City *temp2 = new City;

    City *temp = new City;

    temp=head;

    for(int i=0;i<9;i++){

        temp2->cityName=citylist[i]; //=Pheonix
        temp->next=temp2;            //segmentation fault here
        temp=temp2;
    }
}

您需要在循环中移动“temp2=new City”并将head指向temp1。head不是用“temp=head”行指向temp吗?“最小、完整且可验证的示例”。我不能只是将其粘贴到文件中并尝试帮助您。
->
不是问题所在…欢迎使用堆栈溢出。请提供一个完整的示例,我们可以自己编译和运行。它应该会产生与您得到的完全相同的错误。非常全面。我欠您一个有趣的消息。这在昨天非常有效。我没有遇到任何问题没有任何代码,现在它不工作了。
private:
    City *head;

CommunicationNetwork::CommunicationNetwork()
    : head(NULL)
{
}

CommunicationNetwork::~CommunicationNetwork()
{
    City *temp = head;
    while (temp)
    {
        City *next = temp->next;
        delete temp;
        temp = next;
    }
}

void CommunicationNetwork::buildNetwork()
{
    std::string citylist [] = {"Los Angeles", "Phoenix", "Denver", "Dallas", "St. Louis", "Chicago", "Atlanta", "Washington D.C.", "New York", "Boston"};

    // find the last node in the list...
    City *last = NULL;
    if (head)
    {
        last = head;
        while (last->next)
            last = last->next;        
    }

    // add the cities to the end of the list...
    for(int i = 0; i < 10; ++i)
    {
        City *temp = new City;
        temp->cityName = citylist[i];
        temp->next = NULL;

        if (!head) head = temp;
        if (last) last->next = temp;
        last = temp;
    }
}
private:
    City *head, *tail;

CommunicationNetwork::CommunicationNetwork()
    : head(NULL), tail(NULL)
{
}

CommunicationNetwork::~CommunicationNetwork()
{
    City *temp = head;
    while (temp)
    {
        City *next = temp->next;
        delete temp;
        temp = next;
    }
}

void CommunicationNetwork::addCity(const std::string &cityName)
{
    City *temp = new City;
    temp->cityName = citylist[i];
    temp->next = NULL;

    if (!head) head = temp;
    if (tail) tail->next = temp;
    tail = temp;
}

void CommunicationNetwork::buildNetwork()
{
    std::string citylist [] = {"Los Angeles", "Phoenix", "Denver", "Dallas", "St. Louis", "Chicago", "Atlanta", "Washington D.C.", "New York", "Boston"};

    for(int i = 0; i < 10; ++i)
        addCity(citylist[i]);
}
private:
    std::list<std::string> cities;

void CommunicationNetwork::addCity(const string &cityName)
{
    cities.push_back(cityName);
}

void CommunicationNetwork::buildNetwork()
{
    string citylist [] = {"Los Angeles", "Phoenix", "Denver", "Dallas", "St. Louis", "Chicago", "Atlanta", "Washington D.C.", "New York", "Boston"};

    for(int i = 0; i < 10; ++i)
        addCity(citylist[i]);
}