Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 插入到已排序的链表中_C++_Linked List - Fatal编程技术网

C++ 插入到已排序的链表中

C++ 插入到已排序的链表中,c++,linked-list,C++,Linked List,看起来在“SortedInsert”中,头部始终为零,然后代码仍然会出错。。。真令人沮丧。你知道为什么头总是零吗,即使我把它设置为某个值,然后为什么代码通常会出错吗? 谢谢 #包括 #包括 #包括 #包括 #包括 使用名称空间std; 结构节点{ 节点*next=0; int数据; ~Node(){ 如果(下一步!=0){ 删除下一步; } } }; 空分拣机(节点*头部,int值){ 如果(头==0){ 节点*头=新节点; 标题->数据=值; 头部=头部; 回来 } cout(数据){ 节点*

看起来在“SortedInsert”中,头部始终为零,然后代码仍然会出错。。。真令人沮丧。你知道为什么头总是零吗,即使我把它设置为某个值,然后为什么代码通常会出错吗? 谢谢

#包括
#包括
#包括
#包括
#包括
使用名称空间std;
结构节点{
节点*next=0;
int数据;
~Node(){
如果(下一步!=0){
删除下一步;
}
}
};
空分拣机(节点*头部,int值){
如果(头==0){
节点*头=新节点;
标题->数据=值;
头部=头部;
回来
}
cout(数据){
节点*insert=temp->next;
Node*otherTemp=新节点;
其他温度->数据=值;
温度->下一步=其他温度;
临时->下一步->下一步=插入;
}
温度=温度->下一步;
}
回来
}
int main(){
srand(32);
节点*sortedList=0;
对于(int i=0;i<10;i++){
SortedInsert(sortedList,rand()%100);
}
节点*温度=分类列表;
对于(int i=0;i<9;i++){
断言(临时->数据下一步->数据);
温度=温度->下一步;
}
删除分类列表;
}

SortedInsert
有自己的头部指针副本。当您在函数中更改head时,它不会影响main中的值。解决方案是通过引用或通过传递地址来传递head

void SortedInsert(Node** head, int value) {
    //Use *head to refer to the head of the list
}
int main() {
    ...
    Node* sortedList = 0;
    SortedInsert(&sortedList, ...);
    ...
}

试试下面的方法

void SortedInsert( Node* &head, int value )
{
    if ( head == nullptr || value < head->data )
    {
        head = new Node { head, value };
    }
    else
    {
        Node *current = head;

        while ( current->next != nullptr && !( value < current->next->data ) )
        {
            current = current->next;
        }

        Node *tmp = new Node { current->next, value };
        current->next = tmp;
    }
}
void SortedInsert(节点*&头部,int值)
{
if(head==nullptr | | valuedata)
{
head=新节点{head,value};
}
其他的
{
节点*电流=头部;
而(当前->下一步!=nullptr&&!(值<当前->下一步->数据))
{
当前=当前->下一步;
}
Node*tmp=新节点{current->next,value};
当前->下一步=tmp;
}
}

至于你的函数实现,那么函数处理的是头部的一个副本。副本的任何更改都不会影响参数本身。您应该通过引用传递head或从函数返回head。

您可能正在取消对未初始化指针的引用。使用调试器逐步查找错误源。
void SortedInsert(Node*& head, int value) {
    //Use head to refer to the head of the list
}
int main() {
    ...
    Node* sortedList = 0;
    SortedInsert(sortedList, ...);
    ...
}
void SortedInsert( Node* &head, int value )
{
    if ( head == nullptr || value < head->data )
    {
        head = new Node { head, value };
    }
    else
    {
        Node *current = head;

        while ( current->next != nullptr && !( value < current->next->data ) )
        {
            current = current->next;
        }

        Node *tmp = new Node { current->next, value };
        current->next = tmp;
    }
}