在C ++中插入链接列表

问题描述 投票:0回答:1

我需要在链接列表的两个负值之间插入0。但是我的功能不起作用。怎么了?

P。 S. listLength传递有关链表长度的信息。

插入功能:

void insert(int new_value, int preceed_num)
{
    struct List1* last;
    struct List1 *brand_new;
    brand_new = new(struct List1);
    last = getAddress(preceed_num - 1);
    brand_new -> Next = last -> Next;
    last -> Next = brand_new;   
    brand_new -> Info = new_value;
    amount++;
}

在两个值之间插入零功能:

void insert_zero_unction()
{
    int length = listLength();
    int first, second;
    for(int count = 1; count < length; count++)
    {
        first = getValue(count);
        second = getValue(count + 1);
        if(first < 0 && second < 0)
        {
            insert(0, first);
            length++;
        } 
    } 
}
c++ structure
1个回答
0
投票

如@molbdnilo和@ user4581301所说,遍历列表并在算法中使用指针解决了该问题。这是重写的代码:

void Zeroed_Stack_Function()
{
    int length = listLength();
    struct List1* first, *second;
    int count = 1;
    int to_past;
    while(count <= length)
    {
        first = getAddress(count - 1);
        second = getAddress(count);
        if(first -> Info < 0 && second -> Info < 0)
        {
            insert(0, count);
        }
        length = listLength(); 
        count++;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.