验证输入是否与双向链表中已有的节点匹配

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

在下面的两种方法中(我的双向链表的插入和删除方法),我能够让它们正常工作,但是当我尝试实现它会在每个方法中返回 true 或 false 的部分时,取决于是否有无论是否匹配 ssn,它都不会正确地增加 count 或 itemCount。这是每个代码和说明:

// insert (ss, name) to the existing list
// the SSN values are each node are organized in INCREASING order
// if there is a node matching ss value, return false; otherwise true
// else create a node with (ss, name), insert the node to the proper position
// parameter count is the counter of number of valid insertion
// when you implement this method, consider the following situations:
// 1. list is empty
// 2. node should be inserted to the beginning of the list
// 3. node should be inserted to the middle of the list
// 4. node should be inserted to the end of the list
bool DLL::insert(string ss, string name, int & count){
    int num = 0;
    Node* newPtr = new Node;
    newPtr->ssn = ss;
    newPtr->name = name;
    newPtr->succ = NULL;
    if(headPtr == NULL){
        headPtr = newPtr;
        newPtr->pred = NULL;
    }
    else{
        Node* testTemp = headPtr;
        while(testTemp->succ != NULL){
            if(testTemp->ssn.compare(ss) == 0){
                num++;
            }
            testTemp = testTemp->succ;
        }
        Node* temp = headPtr;
        while(temp->succ != NULL){
            if(temp->ssn.compare(ss) > 0){
                break;
            }
            temp = temp->succ;
        }
        if(num == 1){
            return false;
        }
        if(temp->ssn.compare(ss) > 0 && temp->pred == NULL){
            Node* tempPrevious = temp->pred;
            newPtr->succ = temp;
            newPtr->pred = NULL;
            temp->pred = newPtr;
            headPtr = newPtr;
        }
        else if(temp->ssn.compare(ss) > 0){
            Node* tempPrevious = temp->pred;
            newPtr->succ = temp;
            newPtr->pred = temp->pred;
            temp->pred = newPtr;
            tempPrevious->succ = newPtr;
        }
        else{
            temp->succ = newPtr;
            newPtr->pred = temp;
            newPtr->succ = NULL;
        }
    }
    count++;
    itemCount++;
    return true;
}


// remove node containing ss value
// if there is no node containing ss, return false; otherwise true
// consider the following situations:
// 1. list is empty
// 2. node containing ss value is the first node
// 3. node containing ss value is in the middle of the list
// 4. node containing ss value is the last node of the list
bool DLL::remove(string ss, int & count){
    int num;
    if(headPtr == NULL){
        return false;
    }
    else{
        Node* testTemp = headPtr;
        while(testTemp->succ != NULL){
            if(testTemp->ssn.compare(ss) == 0){
                num++;
            }
            testTemp = testTemp->succ;
        }
        Node* temp = headPtr;
        while(temp->succ != NULL){
            if(temp->ssn.compare(ss) == 0){
                break;
            }
            temp = temp->succ;  
        }
        if(num == 1){
            itemCount--;
            count++;
            return true;
        }
        if(temp->pred == NULL && temp->ssn.compare(ss) == 0){
            headPtr = headPtr->succ;
            headPtr->pred = NULL;
            delete temp;
        }
        else if(temp->ssn.compare(ss) == 0 && temp->succ != NULL){
            temp->succ->pred = temp->succ;
            temp->pred->succ = temp->pred;
            delete temp;
        }
        else{
            temp->pred->succ = NULL;
            delete temp;
        }
    }
    return false;
}

我尝试在 if-else 语句之外实现计数器。我已经尝试将它实现到每个第二个 while 循环中,而我现在拥有的是一个 while 循环,如果条件匹配则递增 num 。不幸的是,这不起作用。我觉得我非常接近,我只是在这一点上想多了。

c++ doubly-linked-list
© www.soinside.com 2019 - 2024. All rights reserved.