Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.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,我不想在链表中插入新元素。为什么我的代码不能正常工作 这里我搜索一个指针 Root* List::rod(int sk){ for(Root *d = p; d != NULL; d = d->next){ if(d->duom == sk){ return d; } } return NULL; } 这里我尝试插入一个新元素 //p - beginning of list g-end of list

我不想在链表中插入新元素。为什么我的代码不能正常工作

这里我搜索一个指针

Root* List::rod(int sk){
    for(Root *d = p; d != NULL; d = d->next){
        if(d->duom == sk){
            return d;
        }
    }
    return NULL;
}
这里我尝试插入一个新元素

//p - beginning of list g-end of list 

void List::Insert(Root *d){
    if(d != NULL){
        d->duom = 9000;
        if(d == p){
            d->next = p;
            p = d;
        }
        else if(d == g){
            g->next = d;
            g = d;
        }
        for(Mazgas *s = p; s->next != d; s = s->next){ 
            d->next = s->next;
            s->next = d;
        }
    }
}
这是我的主要任务

List *S = new List;

S->Set(5);
S->Set(4);
S->Set(8);
S->Set(90);

S->Insert(S->rod(4));

它不起作用了。我认为我的Insert方法是可以的,但是我的rod方法有什么问题吗?

您正在将duom设置为'9000'。(d->duom=9000),您正在使用S->rod(4)搜索“4”。感谢您的回复。嗯,我试着在列表中插入9000个。对于那些不懂立陶宛语的人,
duom
在这里意味着
data
,而
Mazgas
意味着
Node
。你错了,
rod
方法是可以的,但是你的
insert
非常奇怪。。。我不明白你想做什么。我猜第一部分用
Set
方法填充列表,所以您有
[5,4,8,90]
的列表。然后找到数据为
4
的节点并尝试插入它。但如果您这样做,您将断开列表的链接(因为
rod
返回指针)。但是我猜它不起作用,因为你从来没有进入循环,
s->next!=d
为false(
d
是列表中的第二个元素)。