【问题标题】:The output screen goes blank输出屏幕变为空白
【发布时间】:2019-08-14 11:53:50
【问题描述】:

我在 leetcode 上学习链表时编写了这段代码。在这个问题中,最后,在使用索引 6 调用 deleteAtIndex 函数的地方,屏幕变为空白。它适用于索引有效的所有其他值。但是对于索引的无效值,没有输出。

/*Delete the index-th node in the linked list, if the index is valid.
Below is the function I wrote.*/

void deleteAtIndex(int index) {

    Node* current =start;
    int x=0;
    int i;

    while(current != NULL){
        x++;
        current = current->next;
    }

    if(index == 0){
            current =start;
        start = current->next;
    }
    else if(index >0 && index <=x ){
        current =start;
        for(i=0; i<index-1; i++){
            current = current ->next;
        }
        current->next= current->next->next;
    }


    if (index > x || index < 0) {
        cout << "Invalid Index" << endl;
    }
    return;
}

【问题讨论】:

  • 在 C++ 标准库中有很多类似列表的容器,所以你不需要自己实现。
  • 尝试调试代码
  • 显示minimal reproducible example,包括最小输入(如果有)以及实际和预期输出问题可能出在deleteAtIndex 函数之外的其他地方。
  • 尝试删除列表的last节点时出现问题。拿一张纸和一支铅笔,画一个包含例如 2 个元素的链表。您应该能够弄清楚为什么删除列表的最后一个元素不起作用。

标签: c++ list function singly-linked-list definition


【解决方案1】:

对于初学者来说,这个函数没有意义,因为它什么也没删除。事实上,它试图从列表中排除一个节点。

但是该函数具有未定义的行为。

假设start 等于NULL 并且index 也等于0。在这种情况下,由于这些陈述

if(index == 0){
        current =start;
    start = current->next;
            ^^^^^^^^^^^^^
}

使用空指针访问内存。

现在让我们假设start 不等于NULL,并且它是列表中的唯一节点。而index 等于1

在这种情况下,x 将等于 1。由于这些陈述

else if(index >0 && index <=x ){
    current =start;
    for(i=0; i<index-1; i++){
        current = current ->next;
    }
    current->next= current->next->next;
                   ^^^^^^^^^^^^^^^^^^^^
}

再次使用空指针访问内存。

所以函数是完全错误的。

注意函数参数应该是无符号整数类型。最合适的类型是size_t

如果有一个函数通过索引访问节点来使用方法,那么列表应该保留其中的节点数。

如果程序确实是作为 C++ 程序编写的,那么函数可以如下所示,如下面的简化演示程序所示。调查一下。

#include <iostream>

class List
{
public:
    explicit List() = default;

    List( const List & ) = delete;
    List & operator =( const List & ) = delete;

    void push_front( int value )
    {
        head = new Node { value, head };
    }

    std::ostream & out( std::ostream &os = std::cout ) const
    {
        for ( Node *current = head; current != nullptr; current = current->next )
        {
            os << current->value << " -> ";
        }
        os << "nullptr";

        return os;
    }

    bool deleteAtIndex( size_t n )
    {
        Node **current = &head;

        while ( *current != nullptr && n )
        {
            current = &( *current )->next;
            --n;
        }

        bool success = *current != nullptr;

        if ( success )
        {
            Node *tmp = *current;
            *current = ( *current )->next;
            delete tmp;
        }

        return success;
    }

protected:
    struct Node
    {
        int value;
        Node *next;
    } *head = nullptr;
};

std::ostream & operator <<( std::ostream &os, const List &lst )
{
    return lst.out( os );
}

int main() 
{
    const size_t N = 10;

    List lst;

    for ( size_t i = N; i != 0; --i ) lst.push_front( int( i - 1 ) );

    std::cout << lst << '\n';

    std::cout << '\n';

    for ( size_t i = N; i != 0; --i ) 
    {
        lst.deleteAtIndex( i - 1 );
        std::cout << lst << '\n';
    }

    return 0;
}

程序输出是

0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> nullptr

0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> nullptr
0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> nullptr
0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> nullptr
0 -> 1 -> 2 -> 3 -> 4 -> 5 -> nullptr
0 -> 1 -> 2 -> 3 -> 4 -> nullptr
0 -> 1 -> 2 -> 3 -> nullptr
0 -> 1 -> 2 -> nullptr
0 -> 1 -> nullptr
0 -> nullptr
nullptr

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-18
    • 1970-01-01
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    • 2018-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多