简体   繁体   English

从单链通函列表中删除特定节点

[英]Deleting specific node(s) from Singly Linked Circular List

I have been figuring out how to eliminate all 'A' nodes from the text file, "AHHHHHAAAAHHAAHAHHAHAHAAHAAHHA" I have been able to write the delete node function, but it focuses on removing head only, but I want this function to be able to read the file and remove all 'A' and print out only all H's. 我一直在研究如何从文本文件“ AHHHHHAAAAHHAAHAHHAHAHAHAAHAAHHA”中消除所有“ A”节点删除所有“ A”并仅打印所有“ H”。 Here is my delete node function 这是我的删除节点功能

struct Node
{
    char data;
    Node* pPrev;
    Node* pNext;
};    

void deleteNode(Node * android)
{
   Node * pTemp = android->pNext;
   android->data = pTemp->data;
   android->pNext = pTemp->pNext;
   free(pTemp);
}

Here is the another delete node function I found here that I tried to modify, I think it will not work. 这是我在此处尝试修改的另一个删除节点功能,我认为它不起作用。

void deleteNode2(Node * android)
{
   Node * pNext;
   Node * pHead;
   Node * pTail;
   Node * pTemp = android->pNext;

   if(android->pNext == 'A')
   {
       pTemp = pNext;

       if(pHead = pTail)
       {
           pHead = pTail = NULL;
       }
       else
       {
           pHead = pHead->pNext;
       }
   } 
   free(pTemp);
}

in the int main() 在int main()中

while( fscanf(pInFile, "%c", &c) != EOF)
    {
        appendNode( pTail, c);

        // display the list
        displayList( pTail->pNext);

    };

    cout << endl;
    cout << "'A' got removed from the List." << endl << endl;

    deleteNode2(pTail->pNext);
    displayList(pTail->pNext);

The line if(android->pNext == 'A') will give you a compilation error: pNext is a Node* , and you want to compare data. if(android->pNext == 'A')会给您一个编译错误:pNext是一个Node* ,并且您想比较数据。 Replace that with if (android->pNext.data == 'A') . 将其替换为if (android->pNext.data == 'A')

Another issue is that you are making an assignment in an if conditional expression, rather than a comparison: if(pHead = pTail) will not compare the pointers, but instead assign pHead to pTail and evaluate to true (as in, enter the if statement) if pTail was not null. 另一个问题是您要在if条件表达式中进行赋值,而不是进行比较: if(pHead = pTail)不会比较指针,而是将pHead赋给pTail并赋值为true(例如,输入if语句)(如果pTail不为null)。 Just fix that by using the == operator. 只需使用==运算符即可解决。

And yet, there are more issues in your code. 但是,您的代码中还有更多问题。 deleteNode will not work if there's only one element left. 如果仅剩一个元素,则deleteNode将不起作用。 And there's nothing in the program traversing the list in order to find all nodes that you want to remove. 程序中没有任何遍历列表的操作以查找要删除的所有节点。

Assuming you'd put this snippet in your main function, you should try something like this: 假设您将此代码段放入了主函数中,则应尝试如下操作:

Node* deleteNode(Node* android)
{
   if (android->next == android) { // only one element
       free(android);
       return NULL;
   } else {
       Node * pTemp = android->pNext;
       android->data = pTemp->data;
       android->pNext = pTemp->pNext;
       free(pTemp);
       return android;
   }
}

size_t listSize(const Node* pNode) {
    if (!pNode) return 0;
    const Node* pStart = pNode;
    size_t n = 0;
    do {
        pNode = pNode->pNext;
        n++;
    } while (pNode != pStart);
    return n;
}

Node * pTemp = pTail;
size_t len = listSize(pTail);
for (unsigned int i = 0 ; i < len ; i++) {
   if (pTemp.data == 'A') {
       pTemp = deleteNode(pTemp);
   } else {
       pTemp = pTemp->next;
   }
}

This is a tougher challenge than it seems, because we're dealing with a circular list. 这是一个比看起来困难的挑战,因为我们正在处理一份循环清单。 It's a matter of knowing when to stop the loop. 知道何时停止循环是一个问题。 In this case, I just counted the number of nodes before the program starts removing them. 在这种情况下,我只计算了程序开始删除它们之前的节点数。 The first traversal is safe, because it does not modify the list. 第一次遍历是安全的,因为它不会修改列表。 I've already done significant changes to this, but if anyone notices something wrong, I will be pleased to know. 我已经对此进行了重大更改,但是如果有人发现错误,我将很高兴知道。

Just to finish the answer: the circular linked list might be a requirement, but you can make things much easier if you simply prevent the program from appending 'A' characters. 只是为了回答这个问题:循环链接列表可能是必需的,但是如果您只是阻止程序附加'A'字符,则可以使事情变得容易得多 Again, it depends on what your final goal is and what you are being demanded. 同样,这取决于您的最终目标是什么,以及您的要求是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM