需要帮助从链表c ++中删除节点

时间:2016-12-11 23:07:33

标签: c++ linked-list

大家好我正在尝试从任何位置删除链接列表中的节点,我可以从头开始成功删除节点,似乎无法弄清楚如何删除其他任何内容。

这是我的删除功能,非常感谢任何帮助。

void PartList::removePart(string partNum){

Part * walker = firstPointer;
Part * stalker = NULL;
while (walker != NULL)
{
    stalker = walker;
    if (partNum == walker->partNo)
        break;
    walker = walker->link;
}

    if (walker == NULL)
    {
        cout << "Part Number Not Found..." << endl;
    }
    else {
        if(stalker == NULL){
            cout << "Not Found.."<< endl;
        }
        else {
            stalker->link = walker->link;
        }
    }
    if (firstPointer->partNo == partNum)
    {
        firstPointer = walker;
        walker->link = NULL;
    }
    if (stalker->partNo == partNum)
    {
        stalker = NULL;
        walker->link = NULL;
    }
    walker->link = firstPointer;


        numParts--;
        delete stalker;
        stalker = NULL;

}

1 个答案:

答案 0 :(得分:1)

您需要编写更易读的代码。它比它需要的更复杂。让我们假设你的列表是正确构建的。删除节点:

options cmplib=(MY_FUNCS.PURE_MATH);
libname NUMBERS '/folders/myfolders';
option noquotelenmax; * Turn off warning when string length exceeds 256 bytes;
data    mycats (keep=Name Status Gender) 
    myxmaslist (keep=Name Status Gender Budget);
    length Name $ 8 Status $ 8 Gender $ 10 Budget 8.;
    put 'Making a Christmas list:';
    j = 1;
    do while(scan("&morecats",j,' ') ~= '');
        if mod(j, 10) = 1 & mod(j,100) ~= 11 then suffix = 'st';
        else if mod(j, 10) = 2 & mod(j,100) ~= 12 then suffix = 'nd'; 
        else if mod(j, 10) = 3 & mod(j,100) ~= 13 then suffix = 'rd'; 
        else suffix = 'th';

        Name = scan("&morecats", j, ' ');

        if isprime(j) then Status = 'naughty';
        else Status = 'nice';

        if rand("uniform") < 0.5 then Gender = 'male he';
        else Gender = 'female she';

        msg = "My "||strip(j)||suffix||" cat's name is "||strip(Name)||
                ", and "||scan(Gender, 2)||" is usually "||Status;
        put msg;

        Gender = scan(Gender, 1);

        output mycats;
        if Status = 'nice' then do;
            Budget = 10 * phi(j);
            output myxmaslist;
        end;
        j = j + 1;
    end;
run;
option quotelenmax;